Repository: zest-java Updated Branches: refs/heads/develop 9580d06fa -> 511b37cab
ZEST-118; Rewritten TypeLookup and adjusted all the missing typedDescriptors. Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/511b37ca Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/511b37ca Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/511b37ca Branch: refs/heads/develop Commit: 511b37cabfb23f661f33d90a6fb8e30ba5f5d009 Parents: 9580d06 Author: Niclas Hedhman <[email protected]> Authored: Sat Aug 29 23:40:30 2015 +0800 Committer: Niclas Hedhman <[email protected]> Committed: Sat Aug 29 23:40:30 2015 +0800 ---------------------------------------------------------------------- .../bootstrap/CompositeAssemblyImpl.java | 9 +- .../composite/TransientBuilderInstance.java | 29 +- .../zest/runtime/service/ServiceModel.java | 1 - .../service/ServiceReferenceInstance.java | 16 +- .../zest/runtime/structure/LayerInstance.java | 8 +- .../zest/runtime/structure/ModuleInstance.java | 61 +-- .../runtime/structure/ModuleUnitOfWork.java | 11 +- .../zest/runtime/structure/TypeLookup.java | 481 +++++++------------ .../runtime/structure/UsedLayersInstance.java | 8 +- .../unitofwork/EntityBuilderInstance.java | 22 +- .../runtime/unitofwork/UnitOfWorkInstance.java | 5 +- .../runtime/value/ValueBuilderInstance.java | 8 +- .../value/ValueBuilderWithPrototype.java | 5 +- .../runtime/value/ValueBuilderWithState.java | 10 +- .../zest/runtime/value/ValueStateInstance.java | 5 +- .../transients/TransientBuilderFactoryTest.java | 6 +- 16 files changed, 298 insertions(+), 387 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/CompositeAssemblyImpl.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/CompositeAssemblyImpl.java b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/CompositeAssemblyImpl.java index 0eed62f..2dd3b20 100755 --- a/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/CompositeAssemblyImpl.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/bootstrap/CompositeAssemblyImpl.java @@ -219,9 +219,12 @@ public abstract class CompositeAssemblyImpl // Iterable<Class<?>> map = map( new DependencyModel.InjectionTypeFunction(), // filter( new DependencyModel.ScopeSpecification( This.class ), // methodComposite.dependencies() ) ); + + // Iterable<Class<?>> map1 = map( new DependencyModel.InjectionTypeFunction(), // filter( new DependencyModel.ScopeSpecification( This.class ), // mixinModel.dependencies() ) ); + // @SuppressWarnings( "unchecked" ) // Iterable<Class<?>> filter = filter( // not( in( Initializable.class, Lifecycle.class, InvocationHandler.class ) ), @@ -692,7 +695,7 @@ public abstract class CompositeAssemblyImpl protected Stream<Class<?>> mixinDeclarations( Class<?> type ) { - Stream<? extends Type> types = getTypes( type ); + Stream<? extends Type> types = typesOf( type ); return mixinDeclarations( types ); } @@ -715,11 +718,11 @@ public abstract class CompositeAssemblyImpl Class<?> clazz = (Class<?>) type; if( clazz.isInterface() ) { - return typesOf( clazz ); + return typesOf( clazz ).map( Classes.RAW_CLASS ); } else { - return classHierarchy( clazz ); + return classHierarchy( clazz ).map( Classes.RAW_CLASS ); } } throw new UnsupportedOperationException( "Unable to handle type " + type.getTypeName() ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/composite/TransientBuilderInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/composite/TransientBuilderInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/composite/TransientBuilderInstance.java index 6e84bf0..78be044 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/composite/TransientBuilderInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/composite/TransientBuilderInstance.java @@ -17,10 +17,10 @@ package org.apache.zest.runtime.composite; import org.apache.zest.api.common.ConstructionException; import org.apache.zest.api.composite.CompositeInstance; import org.apache.zest.api.composite.TransientBuilder; -import org.apache.zest.api.property.PropertyDescriptor; -import org.apache.zest.runtime.property.PropertyInfo; +import org.apache.zest.api.composite.TransientDescriptor; import org.apache.zest.runtime.property.PropertyInstance; import org.apache.zest.spi.module.ModelModule; +import org.apache.zest.spi.module.ModuleSpi; /** * JAVADOC @@ -28,7 +28,8 @@ import org.apache.zest.spi.module.ModelModule; public final class TransientBuilderInstance<T> implements TransientBuilder<T> { - private ModelModule<TransientModel> model; + private ModuleSpi module; + private TransientModel model; // lazy initialized in accessor private UsesInstance uses = UsesInstance.EMPTY_USES; @@ -38,12 +39,13 @@ public final class TransientBuilderInstance<T> private TransientStateInstance state; - public TransientBuilderInstance( ModelModule<TransientModel> model, + public TransientBuilderInstance( ModelModule<TransientDescriptor> modelModule, TransientStateInstance state, UsesInstance uses ) { - this.model = model; + this.model = (TransientModel) modelModule.model(); + this.module = modelModule.module(); this.state = state; this.uses = uses; } @@ -61,7 +63,7 @@ public final class TransientBuilderInstance<T> // Instantiate given value type if( prototypeInstance == null ) { - prototypeInstance = model.model().newInstance( model.module(), uses, state ); + prototypeInstance = model.newInstance( module, uses, state ); } return prototypeInstance.<T>proxy(); @@ -73,7 +75,7 @@ public final class TransientBuilderInstance<T> // Instantiate given value type if( prototypeInstance == null ) { - prototypeInstance = model.model().newInstance( model.module(), uses, state ); + prototypeInstance = model.newInstance( module, uses, state ); } return prototypeInstance.newProxy( mixinType ); @@ -84,15 +86,16 @@ public final class TransientBuilderInstance<T> throws ConstructionException { // Set correct info's (immutable) on the state - model.model().state().properties().forEach( propertyDescriptor -> - { - ( (PropertyInstance<Object>) state.propertyFor( propertyDescriptor.accessor() ) ).setPropertyInfo( propertyDescriptor ); - } ); + model.state().properties() + .forEach( + propertyDescriptor -> + ( (PropertyInstance<Object>) state.propertyFor( propertyDescriptor.accessor() ) ) + .setPropertyInfo( propertyDescriptor ) ); - model.model().checkConstraints( state ); + model.checkConstraints( state ); CompositeInstance compositeInstance = - model.model().newInstance( model.module(), uses, state ); + model.newInstance( module, uses, state ); return compositeInstance.<T>proxy(); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceModel.java index 2842ada..7a3d3d8 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceModel.java @@ -75,7 +75,6 @@ public final class ServiceModel extends CompositeModel private final String identity; private final boolean instantiateOnStartup; private final ActivatorsModel<?> activatorsModel; - @SuppressWarnings( "raw" ) private final Class configurationType; public ServiceModel( List<Class<?>> types, http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java index 70541b3..e7eb325 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/service/ServiceReferenceInstance.java @@ -25,8 +25,10 @@ import org.apache.zest.api.activation.Activation; import org.apache.zest.api.activation.ActivationEventListener; import org.apache.zest.api.activation.ActivationException; import org.apache.zest.api.activation.PassivationException; +import org.apache.zest.api.common.Visibility; import org.apache.zest.api.composite.CompositeDescriptor; import org.apache.zest.api.composite.CompositeInstance; +import org.apache.zest.api.composite.ModelDescriptor; import org.apache.zest.api.property.StateHolder; import org.apache.zest.api.service.ServiceDescriptor; import org.apache.zest.api.service.ServiceImporterException; @@ -48,7 +50,7 @@ import org.apache.zest.runtime.structure.ModuleInstance; * @param <T> Service Type */ public final class ServiceReferenceInstance<T> - implements ServiceReference<T>, Activation + implements ServiceReference<T>, Activation, ModelDescriptor { private volatile ServiceInstance instance; private final T serviceProxy; @@ -178,6 +180,18 @@ public final class ServiceReferenceInstance<T> return serviceModel; } + @Override + public Visibility visibility() + { + return serviceModel.visibility(); + } + + @Override + public boolean isAssignableTo( Class<?> type ) + { + return serviceModel.isAssignableTo( type ); + } + public final class ServiceInvocationHandler implements CompositeInstance { http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/structure/LayerInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/LayerInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/LayerInstance.java index 5b206d8..338f8cf 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/LayerInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/LayerInstance.java @@ -137,22 +137,22 @@ public class LayerInstance return usedLayersInstance; } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleObjects( final Visibility visibility ) + /* package */ Stream<ModelModule<ObjectDescriptor>> visibleObjects( final Visibility visibility ) { return moduleInstances.stream().flatMap( module -> module.visibleObjects( visibility ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleTransients( final Visibility visibility ) + /* package */ Stream<ModelModule<TransientDescriptor>> visibleTransients( final Visibility visibility ) { return moduleInstances.stream().flatMap( module -> module.visibleTransients( visibility ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleEntities( final Visibility visibility ) + /* package */ Stream<ModelModule<EntityDescriptor>> visibleEntities( final Visibility visibility ) { return moduleInstances.stream().flatMap( module -> module.visibleEntities( visibility ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleValues( final Visibility visibility ) + /* package */ Stream<ModelModule<ValueDescriptor>> visibleValues( final Visibility visibility ) { return moduleInstances.stream().flatMap( module -> module.visibleValues( visibility ) ); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java index 2bd8496..2bd4bf4 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleInstance.java @@ -84,6 +84,7 @@ import org.apache.zest.runtime.injection.InjectionContext; import org.apache.zest.runtime.object.ObjectModel; import org.apache.zest.runtime.object.ObjectsModel; import org.apache.zest.runtime.property.PropertyInstance; +import org.apache.zest.runtime.property.PropertyModel; import org.apache.zest.runtime.query.QueryBuilderFactoryImpl; import org.apache.zest.runtime.service.ImportedServicesInstance; import org.apache.zest.runtime.service.ImportedServicesModel; @@ -188,7 +189,7 @@ public class ModuleInstance try { Class<?> type = classLoader().loadClass( name ); - ModelModule<EntityModel> entityModel = typeLookup.lookupEntityModel( type ); + ModelModule<EntityDescriptor> entityModel = typeLookup.lookupEntityModel( type ); if( entityModel == null ) { return null; @@ -207,7 +208,7 @@ public class ModuleInstance try { Class<?> type = classLoader().loadClass( typeName ); - ModelModule<ObjectModel> objectModel = typeLookup.lookupObjectModel( type ); + ModelModule<ObjectDescriptor> objectModel = typeLookup.lookupObjectModel( type ); if( objectModel == null ) { return null; @@ -226,7 +227,7 @@ public class ModuleInstance try { Class<?> type = classLoader().loadClass( name ); - ModelModule<TransientModel> transientModel = typeLookup.lookupTransientModel( type ); + ModelModule<TransientDescriptor> transientModel = typeLookup.lookupTransientModel( type ); if( transientModel == null ) { return null; @@ -245,7 +246,7 @@ public class ModuleInstance try { Class<?> type = classLoader().loadClass( name ); - ModelModule<ValueModel> valueModel = typeLookup.lookupValueModel( type ); + ModelModule<ValueDescriptor> valueModel = typeLookup.lookupValueModel( type ); if( valueModel == null ) { return null; @@ -307,7 +308,7 @@ public class ModuleInstance throws NoSuchObjectException { NullArgumentException.validateNotNull( "mixinType", mixinType ); - ModelModule<ObjectModel> modelModule = typeLookup.lookupObjectModel( mixinType ); + ModelModule<ObjectDescriptor> modelModule = typeLookup.lookupObjectModel( mixinType ); if( modelModule == null ) { @@ -315,7 +316,7 @@ public class ModuleInstance } InjectionContext injectionContext = new InjectionContext( modelModule.module(), UsesInstance.EMPTY_USES.use( uses ) ); - return mixinType.cast( modelModule.model().newInstance( injectionContext ) ); + return mixinType.cast( ((ObjectModel) modelModule.model()).newInstance( injectionContext ) ); } @Override @@ -323,7 +324,7 @@ public class ModuleInstance throws ConstructionException { NullArgumentException.validateNotNull( "instance", instance ); - ModelModule<ObjectModel> modelModule = typeLookup.lookupObjectModel( instance.getClass() ); + ModelModule<ObjectDescriptor> modelModule = typeLookup.lookupObjectModel( instance.getClass() ); if( modelModule == null ) { @@ -331,7 +332,7 @@ public class ModuleInstance } InjectionContext injectionContext = new InjectionContext( modelModule.module(), UsesInstance.EMPTY_USES.use( uses ) ); - modelModule.model().inject( injectionContext, instance ); + ((ObjectModel) modelModule.model()).inject( injectionContext, instance ); } // Implementation of TransientBuilderFactory @@ -340,7 +341,7 @@ public class ModuleInstance throws NoSuchTransientException { NullArgumentException.validateNotNull( "mixinType", mixinType ); - ModelModule<TransientModel> modelModule = typeLookup.lookupTransientModel( mixinType ); + ModelModule<TransientDescriptor> modelModule = typeLookup.lookupTransientModel( mixinType ); if( modelModule == null ) { @@ -348,15 +349,13 @@ public class ModuleInstance } Map<AccessibleObject, Property<?>> properties = new HashMap<>(); - modelModule.model().state().properties().forEach( propertyModel -> - { - Property<?> property = new PropertyInstance<>( propertyModel - .getBuilderInfo(), - propertyModel - .initialValue( modelModule - .module() ) ); - properties.put( propertyModel.accessor(), property ); - } ); + modelModule.model().state().properties().forEach( + propertyModel -> + { + Property<?> property = new PropertyInstance<>( ((PropertyModel) propertyModel).getBuilderInfo(), + propertyModel.initialValue( modelModule.module() ) ); + properties.put( propertyModel.accessor(), property ); + } ); TransientStateInstance state = new TransientStateInstance( properties ); @@ -383,7 +382,7 @@ public class ModuleInstance throws NoSuchValueException { NullArgumentException.validateNotNull( "mixinType", mixinType ); - ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel( mixinType ); + ModelModule<ValueDescriptor> compositeModelModule = typeLookup.lookupValueModel( mixinType ); if( compositeModelModule == null ) { @@ -407,7 +406,7 @@ public class ModuleInstance NullArgumentException.validateNotNull( "manyAssociationFunction", manyAssociationFunction ); NullArgumentException.validateNotNull( "namedAssociationFunction", namedAssociationFunction ); - ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel( mixinType ); + ModelModule<ValueDescriptor> compositeModelModule = typeLookup.lookupValueModel( mixinType ); if( compositeModelModule == null ) { @@ -464,7 +463,7 @@ public class ModuleInstance ValueInstance valueInstance = ValueInstance.valueInstanceOf( (ValueComposite) prototype ); Class<Composite> valueType = (Class<Composite>) valueInstance.types().findFirst().orElse( null ); - ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel( valueType ); + ModelModule<ValueDescriptor> modelModule = typeLookup.lookupValueModel( valueType ); if( modelModule == null ) { @@ -479,7 +478,7 @@ public class ModuleInstance throws NoSuchValueException, ConstructionException { NullArgumentException.validateNotNull( "mixinType", mixinType ); - ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel( mixinType ); + ModelModule<ValueDescriptor> modelModule = typeLookup.lookupValueModel( mixinType ); if( modelModule == null ) { @@ -558,25 +557,27 @@ public class ModuleInstance @Override public <T> ServiceReference<T> findService( Class<T> serviceType ) { - return typeLookup.lookupServiceReference( (Type) serviceType ); + return typeLookup.lookupServiceReference( serviceType ); } @Override public <T> ServiceReference<T> findService( Type serviceType ) { - return typeLookup.lookupServiceReference( serviceType ); + Class<T> clazz = (Class<T>) serviceType; + return typeLookup.lookupServiceReference( clazz ); } @Override public <T> Iterable<ServiceReference<T>> findServices( Class<T> serviceType ) { - return typeLookup.lookupServiceReferences( (Type) serviceType ); + return typeLookup.lookupServiceReferences( serviceType ); } @Override public <T> Iterable<ServiceReference<T>> findServices( Type serviceType ) { - return typeLookup.lookupServiceReferences( serviceType ); + Class<T> clazz = (Class<T>) serviceType; + return typeLookup.lookupServiceReferences( clazz ); } // Implementation of Activation @@ -716,28 +717,28 @@ public class ModuleInstance return metrics; } - public Stream<ModelModule<? extends ModelDescriptor>> visibleObjects( Visibility visibility ) + public Stream<ModelModule<ObjectDescriptor>> visibleObjects( Visibility visibility ) { return objects.stream() .filter( new Visibilitypredicate( visibility ) ) .map( ModelModule.<ObjectDescriptor>modelModuleFunction( this ) ); } - public Stream<ModelModule<? extends ModelDescriptor>> visibleTransients( Visibility visibility ) + public Stream<ModelModule<TransientDescriptor>> visibleTransients( Visibility visibility ) { return transients.models() .filter( new Visibilitypredicate( visibility ) ) .map( ModelModule.<TransientDescriptor>modelModuleFunction( this ) ); } - public Stream<ModelModule<? extends ModelDescriptor>> visibleEntities( Visibility visibility ) + public Stream<ModelModule<EntityDescriptor>> visibleEntities( Visibility visibility ) { return entities.models() .filter( new Visibilitypredicate( visibility ) ) .map( ModelModule.<EntityDescriptor>modelModuleFunction( this ) ); } - public Stream<ModelModule<? extends ModelDescriptor>> visibleValues( Visibility visibility ) + public Stream<ModelModule<ValueDescriptor>> visibleValues( Visibility visibility ) { return values.models() .filter( new Visibilitypredicate( visibility ) ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleUnitOfWork.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleUnitOfWork.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleUnitOfWork.java index 5c89373..b7a3fe2 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleUnitOfWork.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/ModuleUnitOfWork.java @@ -27,6 +27,7 @@ import org.apache.zest.api.common.QualifiedName; import org.apache.zest.api.composite.Composite; import org.apache.zest.api.entity.EntityBuilder; import org.apache.zest.api.entity.EntityComposite; +import org.apache.zest.api.entity.EntityDescriptor; import org.apache.zest.api.entity.EntityReference; import org.apache.zest.api.entity.Identity; import org.apache.zest.api.entity.IdentityGenerator; @@ -176,7 +177,7 @@ public class ModuleUnitOfWork public <T> EntityBuilder<T> newEntityBuilder( Class<T> type, String identity ) throws EntityTypeNotFoundException { - ModelModule<EntityModel> model = module.typeLookup().lookupEntityModel( type ); + ModelModule<EntityDescriptor> model = module.typeLookup().lookupEntityModel( type ); if( model == null ) { @@ -239,7 +240,7 @@ public class ModuleUnitOfWork NullArgumentException.validateNotNull( "manyAssociationFunction", manyAssociationFunction ); NullArgumentException.validateNotNull( "namedAssociationFunction", namedAssociationFunction ); - ModelModule<EntityModel> model = module.typeLookup().lookupEntityModel( type ); + ModelModule<EntityDescriptor> model = module.typeLookup().lookupEntityModel( type ); if( model == null ) { @@ -258,7 +259,7 @@ public class ModuleUnitOfWork if( identity == null ) { // Use identity from StateResolver if available - PropertyModel identityModel = model.model().state().findPropertyModelByQualifiedName( IDENTITY_STATE_NAME ); + PropertyModel identityModel = (PropertyModel) model.model().state().findPropertyModelByQualifiedName( IDENTITY_STATE_NAME ); identity = (String) stateResolver.getPropertyState( identityModel ); if( identity == null ) { @@ -283,7 +284,7 @@ public class ModuleUnitOfWork public <T> T get( Class<T> type, String identity ) throws EntityTypeNotFoundException, NoSuchEntityException { - Iterable<ModelModule<EntityModel>> models = module.typeLookup().lookupEntityModels( type ); + Iterable<ModelModule<EntityDescriptor>> models = module.typeLookup().lookupEntityModels( type ); if( !models.iterator().hasNext() ) { @@ -303,7 +304,7 @@ public class ModuleUnitOfWork { EntityComposite entityComposite = (EntityComposite) entity; EntityInstance compositeInstance = EntityInstance.entityInstanceOf( entityComposite ); - ModelModule<EntityModel> model = new ModelModule<>( compositeInstance.module(), compositeInstance.entityModel() ); + ModelModule<EntityDescriptor> model = new ModelModule<>( compositeInstance.module(), compositeInstance.entityModel() ); Class<T> type = (Class<T>) compositeInstance.types().findFirst().orElse( null ); return uow.get( compositeInstance.identity(), this, Collections.singletonList( model ), type ); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookup.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookup.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookup.java index e2a5fa5..52447d2 100755 --- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookup.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/TypeLookup.java @@ -22,23 +22,22 @@ package org.apache.zest.runtime.structure; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; -import java.util.Collection; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.zest.api.common.Visibility; import org.apache.zest.api.composite.AmbiguousTypeException; import org.apache.zest.api.composite.ModelDescriptor; +import org.apache.zest.api.composite.TransientDescriptor; +import org.apache.zest.api.entity.EntityDescriptor; +import org.apache.zest.api.object.ObjectDescriptor; import org.apache.zest.api.service.NoSuchServiceException; import org.apache.zest.api.service.ServiceReference; -import org.apache.zest.runtime.composite.TransientModel; -import org.apache.zest.runtime.entity.EntityModel; -import org.apache.zest.runtime.legacy.Specifications; -import org.apache.zest.runtime.object.ObjectModel; -import org.apache.zest.runtime.value.ValueModel; +import org.apache.zest.api.value.ValueDescriptor; import org.apache.zest.spi.module.ModelModule; import static java.util.stream.Stream.concat; @@ -47,7 +46,6 @@ import static org.apache.zest.api.common.Visibility.layer; import static org.apache.zest.api.common.Visibility.module; import static org.apache.zest.api.util.Classes.RAW_CLASS; import static org.apache.zest.api.util.Classes.interfacesOf; -import static org.apache.zest.functional.Iterables.cast; import static org.apache.zest.functional.Iterables.first; /** @@ -59,20 +57,20 @@ public class TypeLookup // Constructor parameters private final ModuleInstance moduleInstance; // Eager instance objects - private final Map<Class<?>, ModelModule<ObjectModel>> objectModels; - private final Map<Class<?>, ModelModule<TransientModel>> transientModels; - private final Map<Class<?>, ModelModule<ValueModel>> valueModels; - private final Map<Class<?>, List<ModelModule<EntityModel>>> allEntityModels; - private final Map<Class<?>, ModelModule<EntityModel>> unambiguousEntityModels; - private final Map<Type, ServiceReference<?>> serviceReferences; - private final Map<Type, Iterable<ServiceReference<?>>> servicesReferences; + private final Map<Class<?>, ModelModule<ObjectDescriptor>> objectModels; + private final Map<Class<?>, ModelModule<TransientDescriptor>> transientModels; + private final Map<Class<?>, ModelModule<ValueDescriptor>> valueModels; + private final Map<Class<?>, List<ModelModule<EntityDescriptor>>> allEntityModels; + private final Map<Class<?>, ModelModule<EntityDescriptor>> unambiguousEntityModels; + private final Map<Class, ServiceReference<?>> serviceReferences; + private final Map<Class, List<ServiceReference<?>>> servicesReferences; /** * Create a new TypeLookup bound to the given ModuleInstance. * * @param moduleInstance ModuleInstance bound to this TypeLookup */ - /* package */ TypeLookup( ModuleInstance moduleInstance ) + TypeLookup( ModuleInstance moduleInstance ) { // Constructor parameters this.moduleInstance = moduleInstance; @@ -102,43 +100,22 @@ public class TypeLookup * * @return First matching Object Model */ - @SuppressWarnings( { "raw", "unchecked" } ) - /* package */ ModelModule<ObjectModel> lookupObjectModel( final Class type ) + ModelModule<ObjectDescriptor> lookupObjectModel( final Class type ) { - ModelModule<ObjectModel> model = objectModels.get( type ); - + ModelModule<ObjectDescriptor> model = objectModels.get( type ); if( model == null ) { - // Unambiguously and lazily resolve ObjectModel - Stream<ModelModule<? extends ModelDescriptor>> models = concat( - ambiguousTypeCheck( type, - findModels( new ExactTypeLookupSpecification( type ), - moduleInstance.visibleObjects( module ), - moduleInstance.layerInstance().visibleObjects( layer ), - moduleInstance.layerInstance().visibleObjects( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleObjects() ) ), - - ambiguousTypeCheck( type, - findModels( new AssignableTypeLookupSpecification( type ), - moduleInstance.visibleObjects( module ), - moduleInstance.layerInstance() - .visibleObjects( layer ), - moduleInstance.layerInstance() - .visibleObjects( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleObjects() ) ) ); - - model = (ModelModule<ObjectModel>) models.findFirst().orElse( null ); - + List<ModelModule<ObjectDescriptor>> allModels = allObjects().collect( Collectors.toList() ); + model = ambiguityMatching( type, allModels, new ExactTypeMatching<>( type ) ); + if( model == null ) + { + model = ambiguityMatching( type, allModels, new AssignableFromTypeMatching<>( type ) ); + } if( model != null ) { objectModels.put( type, model ); } } - return model; } @@ -157,37 +134,17 @@ public class TypeLookup * * @return First matching Transient Model */ - @SuppressWarnings( { "raw", "unchecked" } ) - /* package */ ModelModule<TransientModel> lookupTransientModel( final Class type ) + ModelModule<TransientDescriptor> lookupTransientModel( final Class type ) { - ModelModule<TransientModel> model = transientModels.get( type ); - + ModelModule<TransientDescriptor> model = transientModels.get( type ); if( model == null ) { - // Unambiguously and lazily resolve ObjectModel - Stream<ModelModule<? extends ModelDescriptor>> models = concat( - ambiguousTypeCheck( type, - findModels( new ExactTypeLookupSpecification( type ), - moduleInstance.visibleTransients( module ), - moduleInstance.layerInstance().visibleTransients( layer ), - moduleInstance.layerInstance().visibleTransients( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleTransients() ) ), - - ambiguousTypeCheck( type, - findModels( new AssignableTypeLookupSpecification( type ), - moduleInstance.visibleTransients( module ), - moduleInstance.layerInstance() - .visibleTransients( layer ), - moduleInstance.layerInstance() - .visibleTransients( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleTransients() ) ) ); - - model = (ModelModule<TransientModel>) models.findFirst().orElse( null ); - + List<ModelModule<TransientDescriptor>> allModels = allTransients().collect( Collectors.toList() ); + model = ambiguityMatching( type, allModels, new ExactTypeMatching<>( type ) ); + if( model == null ) + { + model = ambiguityMatching( type, allModels, new AssignableFromTypeMatching<>( type ) ); + } if( model != null ) { transientModels.put( type, model ); @@ -211,43 +168,22 @@ public class TypeLookup * * @return First matching Value Model */ - @SuppressWarnings( { "raw", "unchecked" } ) - public ModelModule<ValueModel> lookupValueModel( final Class type ) + public ModelModule<ValueDescriptor> lookupValueModel( final Class type ) { - ModelModule<ValueModel> model = valueModels.get( type ); - + ModelModule<ValueDescriptor> model = valueModels.get( type ); if( model == null ) { - // Unambiguously and lazily resolve ObjectModel - Stream<ModelModule<? extends ModelDescriptor>> models = concat( - ambiguousTypeCheck( type, - findModels( new ExactTypeLookupSpecification( type ), - moduleInstance.visibleValues( module ), - moduleInstance.layerInstance().visibleValues( layer ), - moduleInstance.layerInstance().visibleValues( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleValues() ) ), - - ambiguousTypeCheck( type, - findModels( new AssignableTypeLookupSpecification( type ), - moduleInstance.visibleValues( module ), - moduleInstance.layerInstance() - .visibleValues( layer ), - moduleInstance.layerInstance() - .visibleValues( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleValues() ) ) ); - - model = (ModelModule<ValueModel>) models.findFirst().orElse( null ); - + List<ModelModule<ValueDescriptor>> allModels = allValues().collect( Collectors.toList() ); + model = ambiguityMatching( type, allModels, new ExactTypeMatching<>( type ) ); + if( model == null ) + { + model = ambiguityMatching( type, allModels, new AssignableFromTypeMatching<>( type ) ); + } if( model != null ) { valueModels.put( type, model ); } } - return model; } @@ -269,43 +205,23 @@ public class TypeLookup * * @return First matching Entity Model */ - @SuppressWarnings( { "raw", "unchecked" } ) - /* package */ ModelModule<EntityModel> lookupEntityModel( final Class type ) + ModelModule<EntityDescriptor> lookupEntityModel( final Class type ) { - ModelModule<EntityModel> model = unambiguousEntityModels.get( type ); + ModelModule<EntityDescriptor> model = unambiguousEntityModels.get( type ); if( model == null ) { - // Unambiguously and lazily resolve ObjectModel - Stream<ModelModule<? extends ModelDescriptor>> models = concat( - ambiguousTypeCheck( type, - findModels( new ExactTypeLookupSpecification( type ), - moduleInstance.visibleEntities( module ), - moduleInstance.layerInstance().visibleEntities( layer ), - moduleInstance.layerInstance().visibleEntities( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleEntities() ) ), - - ambiguousTypeCheck( type, - findModels( new AssignableTypeLookupSpecification( type ), - moduleInstance.visibleEntities( module ), - moduleInstance.layerInstance() - .visibleEntities( layer ), - moduleInstance.layerInstance() - .visibleEntities( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleEntities() ) ) ); - - model = (ModelModule<EntityModel>) models.findFirst().orElse( null ); - + List<ModelModule<EntityDescriptor>> allModels = allEntities().collect( Collectors.toList() ); + model = ambiguityMatching( type, allModels, new ExactTypeMatching<>( type ) ); + if( model == null ) + { + model = ambiguityMatching( type, allModels, new AssignableFromTypeMatching<>( type ) ); + } if( model != null ) { unambiguousEntityModels.put( type, model ); } } - return model; } @@ -331,30 +247,15 @@ public class TypeLookup * * @return All matching Entity Models */ - /* package */ Iterable<ModelModule<EntityModel>> lookupEntityModels( final Class type ) + Iterable<ModelModule<EntityDescriptor>> lookupEntityModels( final Class type ) { - List<ModelModule<EntityModel>> result = allEntityModels.get( type ); + List<ModelModule<EntityDescriptor>> result = allEntityModels.get( type ); if( result == null ) { - // Unambiguously and lazily resolve ObjectModel - Stream<ModelModule<? extends ModelDescriptor>> models = concat( - ambiguousTypeCheck( type, - findModels( new ExactTypeLookupSpecification( type ), - moduleInstance.visibleEntities( module ), - moduleInstance.layerInstance().visibleEntities( layer ), - moduleInstance.layerInstance().visibleEntities( application ), - moduleInstance.layerInstance() - .usedLayersInstance() - .visibleEntities() ) ), - - findModels( new AssignableTypeLookupSpecification( type ), - moduleInstance.visibleEntities( module ), - moduleInstance.layerInstance().visibleEntities( layer ), - moduleInstance.layerInstance().visibleEntities( application ), - moduleInstance.layerInstance().usedLayersInstance().visibleEntities() ) - ).distinct(); - - result = models.map( m -> (ModelModule<EntityModel>) m ).collect( Collectors.toList() ); + result = concat( + allEntities().filter( new ExactTypeMatching<>( type ) ), + allEntities().filter( new AssignableFromTypeMatching<>( type ) ) + ).distinct().collect( Collectors.toList() ); allEntityModels.put( type, result ); } return result; @@ -365,18 +266,17 @@ public class TypeLookup * * <p>Type lookup is done lazily and cached.</p> * - * <p>See {@link #lookupServiceReferences(java.lang.reflect.Type)}.</p> + * <p>See {@link #lookupServiceReferences(Class)}.</p> * * @param <T> Service Type * @param serviceType Looked up Type * * @return First matching ServiceReference */ - /* package */ - @SuppressWarnings( "unchecked" ) - <T> ServiceReference<T> lookupServiceReference( Type serviceType ) + <T> ServiceReference<T> lookupServiceReference( Class<T> serviceType ) { - ServiceReference<?> serviceReference = serviceReferences.get( serviceType ); + @SuppressWarnings( "unchecked" ) + ServiceReference<T> serviceReference = (ServiceReference<T>) serviceReferences.get( serviceType ); if( serviceReference == null ) { // Lazily resolve ServiceReference @@ -391,8 +291,7 @@ public class TypeLookup { throw new NoSuchServiceException( RAW_CLASS.apply( serviceType ).getName(), moduleInstance.name() ); } - - return (ServiceReference<T>) serviceReference; + return serviceReference; } /** @@ -410,128 +309,141 @@ public class TypeLookup * * <p>Type lookup is done lazily and cached.</p> * - * @param <T> Service Type - * @param serviceType Looked up Type + * @param <T> Service Type + * @param type Looked up Type * * @return All matching ServiceReferences */ - @SuppressWarnings( "unchecked" ) - /* package */ <T> Iterable<ServiceReference<T>> lookupServiceReferences( final Type serviceType ) + <T> List<ServiceReference<T>> lookupServiceReferences( final Class<T> type ) { - Iterable<ServiceReference<?>> serviceRefs = servicesReferences.get( serviceType ); + List<ServiceReference<?>> serviceRefs = servicesReferences.get( type ); if( serviceRefs == null ) { - // Lazily resolve ServicesReferences - Stream<ServiceReference<?>> matchingServices = concat( - findServiceReferences( new ExactTypeLookupSpecification( serviceType ), - moduleInstance.visibleServices( module ), - moduleInstance.layerInstance().visibleServices( layer ), - moduleInstance.layerInstance().visibleServices( application ), - moduleInstance.layerInstance().usedLayersInstance().visibleServices() ), - findServiceReferences( new AssignableTypeLookupSpecification( serviceType ), - moduleInstance.visibleServices( module ), - moduleInstance.layerInstance().visibleServices( layer ), - moduleInstance.layerInstance().visibleServices( application ), - moduleInstance.layerInstance().usedLayersInstance().visibleServices() ) - ).distinct(); - serviceRefs = matchingServices.collect( Collectors.toList() ); - servicesReferences.put( serviceType, serviceRefs ); + serviceRefs = concat( + allServices() + .filter( ref -> ref.types().anyMatch( clazz -> clazz.equals( type ) ) ), + allServices() + .filter( ref -> ref.types().anyMatch( + t -> !( t.equals( type ) ) && type.isAssignableFrom( t ) ) + ) + ).distinct().collect( Collectors.toList() ); + servicesReferences.put( type, serviceRefs ); } - - return cast( serviceRefs ); + List<ServiceReference<T>> result = new ArrayList<>(); + //noinspection unchecked + serviceRefs.forEach( ref -> result.add( (ServiceReference<T>) ref ) ); + return result; } - @SafeVarargs - private static Stream<ModelModule<? extends ModelDescriptor>> findModels( Predicate<Stream<Class<?>>> specification, - Stream<ModelModule<? extends ModelDescriptor>>... models - ) + public Stream<Class<?>> allVisibleObjects() { - Function<ModelModule<? extends ModelDescriptor>, Stream<Class<?>>> function = new ModelModuleTypesFunction(); - Predicate<ModelModule<? extends ModelDescriptor>> spec = Specifications.translate( function, specification ); - Stream<ModelModule<? extends ModelDescriptor>> stream = Stream.of( models ).flatMap( flatten -> flatten ); - return stream.filter( spec ); + return allObjects().flatMap( model -> model.model().types() ); } - @SafeVarargs - private static Stream<ServiceReference<?>> findServiceReferences( Predicate<Stream<Class<?>>> specification, - Stream<ServiceReference<?>>... references - ) + private Stream<ModelModule<ObjectDescriptor>> allObjects() { - Predicate<ServiceReference<?>> spec = Specifications.translate( new ServiceReferenceTypesFunction(), specification ); - return Stream.of( references ).flatMap( flatten -> flatten ).filter( spec ); + return concat( moduleInstance.visibleObjects( module ), + concat( + moduleInstance.layerInstance().visibleObjects( layer ), + concat( + moduleInstance.layerInstance().visibleObjects( application ), + moduleInstance.layerInstance().usedLayersInstance().visibleObjects() + ) + ) + ); } - /** - * Check if the list of models contains several ones with the same visibility. If yes, then - * throw an AmbiguousTypeException - */ - private static Stream<ModelModule<? extends ModelDescriptor>> ambiguousTypeCheck( final Class<?> type, - Stream<ModelModule<? extends ModelDescriptor>> models - ) + private Stream<ModelModule<TransientDescriptor>> allTransients() { - // TODO: Figure out why AmbiguityFinder doesn't implement Function<ModelModule<T>, ModelModule<T>>, when it clearly says it does. Absurd. - Function<ModelModule<? extends ModelDescriptor>, ModelModule<? extends ModelDescriptor>> ambiguityFinder = new AmbiguityFinder( type ); - return models.map( ambiguityFinder ); + return concat( moduleInstance.visibleTransients( module ), + concat( + moduleInstance.layerInstance().visibleTransients( layer ), + concat( + moduleInstance.layerInstance().visibleTransients( application ), + moduleInstance.layerInstance().usedLayersInstance().visibleTransients() + ) + ) + ); } - public Stream<Class<?>> allVisibleObjects() + private Stream<ModelModule<ValueDescriptor>> allValues() { - return concat( moduleInstance.visibleObjects( module ), + return concat( moduleInstance.visibleValues( module ), concat( - moduleInstance.layerInstance().visibleObjects( layer ), + moduleInstance.layerInstance().visibleValues( layer ), concat( - moduleInstance.layerInstance().visibleObjects( application ), - moduleInstance.layerInstance().usedLayersInstance().visibleObjects() + moduleInstance.layerInstance().visibleValues( application ), + moduleInstance.layerInstance().usedLayersInstance().visibleValues() ) ) - ).flatMap( model -> model.model().types() ); + ); } - private static class ModelModuleTypesFunction<T extends ModelDescriptor> - implements Function<ModelModule<T>, Stream<Class<?>>> + private Stream<ModelModule<EntityDescriptor>> allEntities() { + return concat( moduleInstance.visibleEntities( module ), + concat( + moduleInstance.layerInstance().visibleEntities( layer ), + concat( + moduleInstance.layerInstance().visibleEntities( application ), + moduleInstance.layerInstance().usedLayersInstance().visibleEntities() + ) + ) + ); + } - @Override - public Stream<Class<?>> apply( ModelModule<T> modelModule ) - { - return modelModule.model().types(); - } + private Stream<ServiceReference<?>> allServices() + { + return concat( moduleInstance.visibleServices( module ), + concat( + moduleInstance.layerInstance().visibleServices( layer ), + concat( + moduleInstance.layerInstance().visibleServices( application ), + moduleInstance.layerInstance().usedLayersInstance().visibleServices() + ) + ) + ); } - private static class ServiceReferenceTypesFunction - implements Function<ServiceReference<?>, Stream<Class<?>>> + private <T extends ModelDescriptor> ModelModule<T> ambiguityMatching( + Class type, + List<ModelModule<T>> modelModules, + TypeMatching<T> matching + ) { + List<ModelModule<T>> models = modelModules.stream() + .filter( matching ) + .filter( new SameVisibility<>() ) + .distinct() + .collect( Collectors.toList() ); - @Override - public Stream<Class<?>> apply( ServiceReference<?> serviceReference ) + if( models.size() > 1 ) + { + throw new AmbiguousTypeException( "More than one type matches " + type.getName() + ": " + models + "]" ); + } + if( models.isEmpty() ) { - return serviceReference.types(); + return null; } + return models.get( 0 ); } - private static final class ExactTypeLookupSpecification - implements Predicate<Stream<Class<?>>> + private static abstract class TypeMatching<T extends ModelDescriptor> + implements Predicate<ModelModule<T>> { - protected final Type lookedUpType; - private ExactTypeLookupSpecification( Type lookedUpType ) + protected TypeMatching( Type lookedUpType ) { this.lookedUpType = lookedUpType; } - protected boolean checkClassMatch( Class<?> candidate, Class<?> lookedUpType ) - { - return candidate.equals( lookedUpType ); - } - @Override - public final boolean test( Stream<Class<?>> types ) + public final boolean test( ModelModule<T> model ) { if( lookedUpType instanceof Class ) { - // Straight class assignability check - return checkClassMatch( types, (Class) lookedUpType ); + return model.model().types().anyMatch( checkMatch( lookedUpType ) ); } else { @@ -540,13 +452,13 @@ public class TypeLookup // Foo<Bar> check // First check Foo ParameterizedType parameterizedType = (ParameterizedType) lookedUpType; - Collection asCollection = types.collect( Collectors.toList() ); - if( !checkClassMatch( asCollection.stream(), (Class) parameterizedType.getRawType() ) ) + Type rawType = parameterizedType.getRawType(); + if( !model.model().types().anyMatch( checkMatch( rawType ) ) ) { return false; } // Then check Bar - return interfacesOf( asCollection.stream() ).anyMatch( intf -> intf.equals( lookedUpType ) ); + return interfacesOf( model.model().types() ).anyMatch( intf -> intf.equals( lookedUpType ) ); } else if( lookedUpType instanceof WildcardType ) { @@ -556,92 +468,59 @@ public class TypeLookup } } - private boolean checkClassMatch( Stream<Class<?>> candidates, Class<?> lookedUpType ) - { - return candidates.anyMatch( candidate -> checkClassMatch( candidate, lookedUpType ) ); - } + protected abstract Predicate<Type> checkMatch( Type matchTo ); } - private static final class AssignableTypeLookupSpecification - implements Predicate<Stream<Class<?>>> + private static final class ExactTypeMatching<T extends ModelDescriptor> extends TypeMatching<T> { - - protected final Type lookedUpType; - - private AssignableTypeLookupSpecification( Type lookedUpType ) + private ExactTypeMatching( Type lookedUpType ) { - this.lookedUpType = lookedUpType; + super( lookedUpType ); } - protected boolean checkClassMatch( Class<?> candidate, Class<?> lookedUpType ) + protected Predicate<Type> checkMatch( Type matchTo ) { - return !candidate.equals( lookedUpType ) && lookedUpType.isAssignableFrom( candidate ); + return matchTo::equals; } + } - @Override - public final boolean test( Stream<Class<?>> types ) + private static final class AssignableFromTypeMatching<T extends ModelDescriptor> extends TypeMatching<T> + { + private AssignableFromTypeMatching( Type lookedUpType ) { - if( lookedUpType instanceof Class ) - { - // Straight class assignability check - return checkClassMatch( types, (Class) lookedUpType ); - } - else - { - if( lookedUpType instanceof ParameterizedType ) - { - // Foo<Bar> check - // First check Foo - ParameterizedType parameterizedType = (ParameterizedType) lookedUpType; - Collection asCollection = types.collect( Collectors.toList() ); - - if( !checkClassMatch( asCollection.stream(), (Class) parameterizedType.getRawType() ) ) - { - return false; - } - // Then check Bar - interfacesOf( asCollection.stream() ).anyMatch( intf -> intf.equals( lookedUpType ) ); - } - else if( lookedUpType instanceof WildcardType ) - { - return true; - } - return false; - } + super( lookedUpType ); } - private boolean checkClassMatch( Stream<Class<?>> candidates, Class<?> lookedUpType ) + protected Predicate<Type> checkMatch( Type matchTo ) { - return candidates.anyMatch( candidate -> checkClassMatch( candidate, lookedUpType ) ); + // TODO; what to do if there is ParameterizedType here?? Now set to ClassCastException and see if anything surfaces +// if( matchTo instanceof Class ) + { + Class<?> clazz = (Class<?>) matchTo; + return candidate -> + !candidate.equals( matchTo ) && clazz.isAssignableFrom( (Class<?>) candidate ); + } +// return candidate -> candidate.equals( matchTo ); } } - private static class AmbiguityFinder<T extends ModelDescriptor> - implements Function<ModelModule<T>, ModelModule<T>> + /** + * This Predicate will filter out all Models that doesn't have the same visisbility as the first one. + */ + private class SameVisibility<T extends ModelDescriptor> + implements Predicate<ModelModule<T>> { - private ModelModule<T> current = null; - private final Class<?> type; - - private AmbiguityFinder( Class<?> type ) - { - this.type = type; - } + private Visibility current = null; @Override - public ModelModule<T> apply( ModelModule<T> model ) + public boolean test( ModelModule<T> model ) { - if( current != null && !model.equals( current ) ) - { - if( model.model().visibility() == current.model().visibility() ) - { - throw new AmbiguousTypeException( "More than one type matches " + type.getName() + ": " + current + ", " + model + "]" ); - } - } - else + if( current == null ) { - current = model; + current = model.model().visibility(); + return true; } - return current; + return current == model.model().visibility(); } } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/structure/UsedLayersInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/structure/UsedLayersInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/structure/UsedLayersInstance.java index 3ebe0d8..5a9bc94 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/structure/UsedLayersInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/structure/UsedLayersInstance.java @@ -37,25 +37,25 @@ public final class UsedLayersInstance this.usedLayerInstances = usedLayerInstances; } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleObjects() + /* package */ Stream<ModelModule<ObjectDescriptor>> visibleObjects() { return usedLayerInstances.stream() .flatMap( layerInstance -> layerInstance.visibleObjects( Visibility.application ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleTransients() + /* package */ Stream<ModelModule<TransientDescriptor>> visibleTransients() { return usedLayerInstances.stream() .flatMap( layerInstance -> layerInstance.visibleTransients( Visibility.application ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleEntities() + /* package */ Stream<ModelModule<EntityDescriptor>> visibleEntities() { return usedLayerInstances.stream() .flatMap( layerInstance -> layerInstance.visibleEntities( Visibility.application ) ); } - /* package */ Stream<ModelModule<? extends ModelDescriptor>> visibleValues() + /* package */ Stream<ModelModule<ValueDescriptor>> visibleValues() { return usedLayerInstances.stream() .flatMap( layerInstance -> layerInstance.visibleValues( Visibility.application ) ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/EntityBuilderInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/EntityBuilderInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/EntityBuilderInstance.java index 014c28c..3219430 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/EntityBuilderInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/EntityBuilderInstance.java @@ -18,6 +18,7 @@ package org.apache.zest.runtime.unitofwork; import org.apache.zest.api.common.QualifiedName; import org.apache.zest.api.entity.EntityBuilder; +import org.apache.zest.api.entity.EntityDescriptor; import org.apache.zest.api.entity.EntityReference; import org.apache.zest.api.entity.Identity; import org.apache.zest.api.entity.LifecycleException; @@ -28,6 +29,7 @@ import org.apache.zest.spi.module.ModelModule; import org.apache.zest.runtime.structure.ModuleUnitOfWork; import org.apache.zest.spi.entity.EntityState; import org.apache.zest.spi.entitystore.EntityStoreUnitOfWork; +import org.apache.zest.spi.module.ModuleSpi; /** * Implementation of EntityBuilder. Maintains an instance of the entity which @@ -38,9 +40,10 @@ public final class EntityBuilderInstance<T> { private static final QualifiedName IDENTITY_STATE_NAME; - private final ModelModule<EntityModel> model; + private final EntityModel model; private final ModuleUnitOfWork uow; private final EntityStoreUnitOfWork store; + private final ModuleSpi module; private String identity; private final BuilderEntityState entityState; @@ -59,7 +62,7 @@ public final class EntityBuilderInstance<T> } public EntityBuilderInstance( - ModelModule<EntityModel> model, + ModelModule<EntityDescriptor> model, ModuleUnitOfWork uow, EntityStoreUnitOfWork store, String identity @@ -69,26 +72,27 @@ public final class EntityBuilderInstance<T> } public EntityBuilderInstance( - ModelModule<EntityModel> model, + ModelModule<EntityDescriptor> model, ModuleUnitOfWork uow, EntityStoreUnitOfWork store, String identity, FunctionStateResolver stateResolver ) { - this.model = model; + this.model = (EntityModel) model.model(); + this.module = model.module(); this.uow = uow; this.store = store; this.identity = identity; EntityReference reference = new EntityReference( identity ); entityState = new BuilderEntityState( model.model(), reference ); - model.model().initState( model.module(), entityState ); + this.model.initState( model.module(), entityState ); if( stateResolver != null ) { - stateResolver.populateState( model.model(), entityState ); + stateResolver.populateState( this.model, entityState ); } entityState.setPropertyValue( IDENTITY_STATE_NAME, identity ); - prototypeInstance = model.model().newInstance( uow, model.module(), entityState ); + prototypeInstance = this.model.newInstance( uow, model.module(), entityState ); } @SuppressWarnings( "unchecked" ) @@ -117,7 +121,7 @@ public final class EntityBuilderInstance<T> // Figure out whether to use given or generated identity identity = (String) entityState.propertyValueOf( IDENTITY_STATE_NAME ); - EntityState newEntityState = model.model().newEntityState( store, uow.module(), + EntityState newEntityState = model.newEntityState( store, uow.module(), EntityReference.parseEntityReference( identity ) ); prototypeInstance.invokeCreate(); @@ -127,7 +131,7 @@ public final class EntityBuilderInstance<T> entityState.copyTo( newEntityState ); - EntityInstance instance = model.model().newInstance( uow, model.module(), newEntityState ); + EntityInstance instance = model.newInstance( uow, module, newEntityState ); Object proxy = instance.proxy(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/UnitOfWorkInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/UnitOfWorkInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/UnitOfWorkInstance.java index f80f544..caf2c01 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/UnitOfWorkInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/unitofwork/UnitOfWorkInstance.java @@ -25,6 +25,7 @@ import java.util.Stack; import java.util.concurrent.TimeUnit; import org.apache.zest.api.common.MetaInfo; import org.apache.zest.api.entity.EntityComposite; +import org.apache.zest.api.entity.EntityDescriptor; import org.apache.zest.api.entity.EntityReference; import org.apache.zest.api.metrics.MetricsCounter; import org.apache.zest.api.metrics.MetricsCounterFactory; @@ -121,7 +122,7 @@ public final class UnitOfWorkInstance public <T> T get( EntityReference identity, ModuleUnitOfWork uow, - Iterable<ModelModule<EntityModel>> potentialModels, + Iterable<ModelModule<EntityDescriptor>> potentialModels, Class<T> mixinType ) throws EntityTypeNotFoundException, NoSuchEntityException @@ -137,7 +138,7 @@ public final class UnitOfWorkInstance EntityModel model = null; ModuleSpi module = null; // Figure out what EntityStore to use - for( ModelModule<EntityModel> potentialModel : potentialModels ) + for( ModelModule<EntityDescriptor> potentialModel : potentialModels ) { EntityStore store = potentialModel.module().entityStore(); EntityStoreUnitOfWork storeUow = getEntityStoreUnitOfWork( store, potentialModel.module() ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderInstance.java index 9375b50..9f72fe2 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderInstance.java @@ -18,6 +18,7 @@ import org.apache.zest.api.common.ConstructionException; import org.apache.zest.api.composite.Composite; import org.apache.zest.api.value.NoSuchValueException; import org.apache.zest.api.value.ValueBuilder; +import org.apache.zest.api.value.ValueDescriptor; import org.apache.zest.runtime.composite.StateResolver; import org.apache.zest.runtime.structure.ModuleInstance; import org.apache.zest.spi.module.ModelModule; @@ -32,13 +33,14 @@ public final class ValueBuilderInstance<T> private final ModuleInstance currentModule; private final ValueInstance prototypeInstance; - public ValueBuilderInstance( ModelModule<ValueModel> compositeModelModule, + public ValueBuilderInstance( ModelModule<ValueDescriptor> compositeModelModule, ModuleInstance currentModule, StateResolver stateResolver ) { ValueStateInstance state = new ValueStateInstance( compositeModelModule, currentModule, stateResolver ); - prototypeInstance = compositeModelModule.model().newValueInstance( compositeModelModule.module(), state ); + ValueModel model = (ValueModel) compositeModelModule.model(); + prototypeInstance = model.newValueInstance( compositeModelModule.module(), state ); prototypeInstance.prepareToBuild(); this.currentModule = currentModule; } @@ -68,7 +70,7 @@ public final class ValueBuilderInstance<T> { Class<Composite> valueType = (Class<Composite>) prototypeInstance.types().findFirst().orElse( null ); - ModelModule<ValueModel> valueModel = currentModule.typeLookup().lookupValueModel( valueType ); + ModelModule<ValueDescriptor> valueModel = currentModule.typeLookup().lookupValueModel( valueType ); if( valueModel == null ) { http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithPrototype.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithPrototype.java b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithPrototype.java index 3c9a280..a878ab2 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithPrototype.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithPrototype.java @@ -31,6 +31,7 @@ import org.apache.zest.api.entity.EntityReference; import org.apache.zest.api.property.PropertyDescriptor; import org.apache.zest.api.value.ValueBuilder; import org.apache.zest.api.value.ValueComposite; +import org.apache.zest.api.value.ValueDescriptor; import org.apache.zest.runtime.composite.FunctionStateResolver; import org.apache.zest.runtime.composite.MixinModel; import org.apache.zest.runtime.composite.MixinsModel; @@ -49,12 +50,12 @@ public class ValueBuilderWithPrototype<T> private ValueInstance prototypeInstance; private final ValueModel valueModel; - public ValueBuilderWithPrototype( ModelModule<ValueModel> compositeModelModule, + public ValueBuilderWithPrototype( ModelModule<ValueDescriptor> compositeModelModule, ModuleInstance currentModule, T prototype ) { - valueModel = compositeModelModule.model(); + valueModel = (ValueModel) compositeModelModule.model(); // Only shallow clone, as all generic types of the ValueComposites are expected to be Immutable. MixinsModel mixinsModel = valueModel.mixinsModel(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithState.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithState.java b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithState.java index 57413b5..db97002 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithState.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueBuilderWithState.java @@ -16,21 +16,23 @@ package org.apache.zest.runtime.value; import org.apache.zest.api.association.AssociationStateHolder; import org.apache.zest.api.common.ConstructionException; import org.apache.zest.api.value.ValueBuilder; +import org.apache.zest.api.value.ValueDescriptor; import org.apache.zest.runtime.composite.StateResolver; import org.apache.zest.spi.module.ModelModule; import org.apache.zest.runtime.structure.ModuleInstance; public class ValueBuilderWithState<T> implements ValueBuilder<T> { - private final ModelModule<ValueModel> model; + private final ModelModule<ValueDescriptor> model; private ValueInstance prototypeInstance; - public ValueBuilderWithState( ModelModule<ValueModel> compositeModelModule, + public ValueBuilderWithState( ModelModule<ValueDescriptor> compositeModelModule, ModuleInstance currentModule, StateResolver stateResolver ) { ValueStateInstance state = new ValueStateInstance( compositeModelModule, currentModule, stateResolver ); - ValueInstance instance = compositeModelModule.model().newValueInstance( compositeModelModule.module(), state ); + ValueModel model = (ValueModel) compositeModelModule.model(); + ValueInstance instance = model.newValueInstance( compositeModelModule.module(), state ); instance.prepareToBuild(); this.model = compositeModelModule; this.prototypeInstance = instance; @@ -68,7 +70,7 @@ public class ValueBuilderWithState<T> implements ValueBuilder<T> prototypeInstance.prepareBuilderState(); // Check that it is valid - model.model().checkConstraints( prototypeInstance.state() ); + ((ValueModel) model.model()).checkConstraints( prototypeInstance.state() ); try { http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueStateInstance.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueStateInstance.java b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueStateInstance.java index ef0c20b..ed0083b 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueStateInstance.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/value/ValueStateInstance.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.stream.Stream; import org.apache.zest.api.association.AssociationStateHolder; import org.apache.zest.api.entity.EntityReference; +import org.apache.zest.api.value.ValueDescriptor; import org.apache.zest.runtime.association.AssociationInfo; import org.apache.zest.runtime.association.AssociationInstance; import org.apache.zest.runtime.association.ManyAssociationInstance; @@ -59,12 +60,12 @@ public final class ValueStateInstance this.namedAssociations = namedAssociations; } - public ValueStateInstance( ModelModule<ValueModel> compositeModelModule, + public ValueStateInstance( ModelModule<ValueDescriptor> compositeModelModule, ModuleInstance currentModule, StateResolver stateResolver ) { - ValueModel valueModel = compositeModelModule.model(); + ValueModel valueModel = (ValueModel) compositeModelModule.model(); this.properties = new LinkedHashMap<>(); valueModel.state().properties().forEach( propertyDescriptor -> { PropertyInfo builderInfo = propertyDescriptor.getBuilderInfo(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/511b37ca/core/runtime/src/test/java/org/apache/zest/runtime/transients/TransientBuilderFactoryTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/transients/TransientBuilderFactoryTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/transients/TransientBuilderFactoryTest.java index cf9435f..26cb3c0 100644 --- a/core/runtime/src/test/java/org/apache/zest/runtime/transients/TransientBuilderFactoryTest.java +++ b/core/runtime/src/test/java/org/apache/zest/runtime/transients/TransientBuilderFactoryTest.java @@ -150,13 +150,13 @@ public class TransientBuilderFactoryTest }; AnyTransient anyTransient = assembler.module().newTransient( AnyTransient.class ); - assertThat( anyTransient.hello( "me" ), new IsEqual<String>( "Hello ME from Module 1" ) ); + assertThat( anyTransient.hello( "me" ), new IsEqual<>( "Hello ME from Module 1" ) ); - assertThat( anyTransient.hello( "World" ), new IsEqual<String>( "Hello WORLD from ME" ) ); + assertThat( anyTransient.hello( "World" ), new IsEqual<>( "Hello WORLD from ME" ) ); anyTransient.hello( "Universe" ); } - public static interface AnyComposite + public interface AnyComposite extends TransientComposite { }
