POLYGENE-152 : Disallow certain classes as Property types, i.e Vector, Dictionary, Hashtable, java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp, org.joda.time
Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/68f2ea56 Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/68f2ea56 Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/68f2ea56 Branch: refs/heads/develop Commit: 68f2ea567989e3c302272c11adcb70d75cb4d152 Parents: 4fa29e2 Author: niclas <[email protected]> Authored: Tue Apr 11 20:54:04 2017 +0800 Committer: niclas <[email protected]> Committed: Tue Apr 11 20:54:04 2017 +0800 ---------------------------------------------------------------------- .../property/InvalidPropertyTypeException.java | 5 ++ .../runtime/property/PropertyModel.java | 49 +++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/polygene-java/blob/68f2ea56/core/api/src/main/java/org/apache/polygene/api/property/InvalidPropertyTypeException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/polygene/api/property/InvalidPropertyTypeException.java b/core/api/src/main/java/org/apache/polygene/api/property/InvalidPropertyTypeException.java index 1addfae..c566011 100644 --- a/core/api/src/main/java/org/apache/polygene/api/property/InvalidPropertyTypeException.java +++ b/core/api/src/main/java/org/apache/polygene/api/property/InvalidPropertyTypeException.java @@ -27,6 +27,11 @@ import org.apache.polygene.api.common.ConstructionException; */ public class InvalidPropertyTypeException extends ConstructionException { + public InvalidPropertyTypeException( String message ) + { + super(message); + } + public InvalidPropertyTypeException( AccessibleObject accessor ) { super( createMessage( accessor ) ); http://git-wip-us.apache.org/repos/asf/polygene-java/blob/68f2ea56/core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyModel.java b/core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyModel.java index 29b1f61..2c7ead6 100644 --- a/core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyModel.java +++ b/core/runtime/src/main/java/org/apache/polygene/runtime/property/PropertyModel.java @@ -39,9 +39,9 @@ import org.apache.polygene.api.property.InitialValueProvider; import org.apache.polygene.api.property.InvalidPropertyTypeException; import org.apache.polygene.api.property.Property; import org.apache.polygene.api.property.PropertyDescriptor; -import org.apache.polygene.api.structure.Module; import org.apache.polygene.api.serialization.Deserializer; import org.apache.polygene.api.service.ServiceFinder; +import org.apache.polygene.api.structure.Module; import org.apache.polygene.api.structure.ModuleDescriptor; import org.apache.polygene.api.type.ValueCompositeType; import org.apache.polygene.api.type.ValueType; @@ -56,13 +56,14 @@ import org.apache.polygene.runtime.type.ValueTypeFactoryInstance; /** * Model for a Property. - * + * <p> * <p>Equality is based on the Property accessor object (property type and name), not on the QualifiedName.</p> */ public class PropertyModel implements PropertyDescriptor, PropertyInfo, Binder, Visitable<PropertyModel> { private Type type; + private transient AccessibleObject accessor; // Interface accessor private final QualifiedName qualifiedName; @@ -88,7 +89,7 @@ public class PropertyModel MetaInfo metaInfo, Object initialValue, InitialValueProvider initialValueProvider - ) + ) { if( accessor instanceof Method ) { @@ -100,8 +101,9 @@ public class PropertyModel } this.immutable = immutable; this.metaInfo = metaInfo; - type = GenericPropertyInfo.propertyTypeOf( accessor ); this.accessor = accessor; + type = GenericPropertyInfo.propertyTypeOf( accessor ); + checkTypeValidity( type ); qualifiedName = QualifiedName.fromAccessor( accessor ); if( initialValueProvider != null ) { @@ -109,13 +111,37 @@ public class PropertyModel } else { - this.initialValueProvider = new DefaultInitialValueProvider(useDefaults, initialValue); + this.initialValueProvider = new DefaultInitialValueProvider( useDefaults, initialValue ); } this.constraints = constraints; final Queryable queryable = accessor.getAnnotation( Queryable.class ); this.queryable = queryable == null || queryable.value(); } + private void checkTypeValidity( Type type ) + { + // Make sure certain data types doesn't take hold in Polygene applications. + String typeName = type.getTypeName(); + if( typeName.contains( "java.util.Date" ) + || typeName.contains( "java.util.Calendar" ) + || typeName.contains( "java.util.GregorianCalendar" ) + || typeName.contains( "java.sql.Date" ) + || typeName.contains( "java.sql.Time" ) + || typeName.contains( "java.sql.Timestamp" ) + || typeName.contains( "org.joda.time" ) + ) + { + throw new InvalidPropertyTypeException( type + " is not allowed in Polygene. Please use Java Time API instead." ); + } + if( typeName.contains( "java.util.Dictionary" ) + || typeName.contains( "java.util.Hashtable" ) + || typeName.contains( "java.util.Vector" ) + ) + { + throw new InvalidPropertyTypeException( type + " is not allowed in Polygene. Please use the modern Java Collection API instead." ); + } + } + @Override public <T> T metaInfo( Class<T> infoType ) { @@ -175,12 +201,11 @@ public class PropertyModel } @Override - public Object resolveInitialValue(ModuleDescriptor moduleDescriptor) + public Object resolveInitialValue( ModuleDescriptor moduleDescriptor ) { - return initialValueProvider.apply(moduleDescriptor.instance(), this); + return initialValueProvider.apply( moduleDescriptor.instance(), this ); } - @Override public void bind( Resolution resolution ) throws BindingException @@ -289,7 +314,7 @@ public class PropertyModel implements InitialValueProvider { @Override - public Object apply(Module module, PropertyDescriptor property) + public Object apply( Module module, PropertyDescriptor property ) { return null; } @@ -301,16 +326,16 @@ public class PropertyModel private final boolean useDefaults; private final Object initialValue; - private DefaultInitialValueProvider(boolean useDefaults, Object initialValue) + private DefaultInitialValueProvider( boolean useDefaults, Object initialValue ) { this.useDefaults = useDefaults; this.initialValue = initialValue; } @Override - public Object apply(Module module, PropertyDescriptor property) + public Object apply( Module module, PropertyDescriptor property ) { - return initialValue(module.descriptor(), initialValue, useDefaults); + return initialValue( module.descriptor(), initialValue, useDefaults ); } private Object initialValue( ModuleDescriptor module, Object initialValue, boolean useDefaults )
