Search in sources :

Example 1 with MixinModel

use of org.qi4j.runtime.composite.MixinModel in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method implementMethod.

protected MixinModel implementMethod(Method method, Iterable<Class<?>> mixinDeclarations) {
    MixinModel implementationModel = mixinsModel.mixinFor(method);
    if (implementationModel != null) {
        return implementationModel;
    }
    Class mixinClass = findTypedImplementation(method, mixinDeclarations);
    if (mixinClass != null) {
        return implementMethodWithClass(method, mixinClass);
    }
    // Check generic implementations
    mixinClass = findGenericImplementation(method, mixinDeclarations);
    if (mixinClass != null) {
        return implementMethodWithClass(method, mixinClass);
    }
    throw new InvalidCompositeException("No implementation found for method \n    " + method.toGenericString() + "\nin\n    " + types);
}
Also used : InvalidCompositeException(org.qi4j.api.composite.InvalidCompositeException) MixinModel(org.qi4j.runtime.composite.MixinModel)

Example 2 with MixinModel

use of org.qi4j.runtime.composite.MixinModel in project qi4j-sdk by Qi4j.

the class EntityMixinsModel method newMixin.

public Object newMixin(EntityInstance entityInstance, StateHolder state, Object[] mixins, Method method) {
    MixinModel model = methodImplementation.get(method);
    InjectionContext injectionContext = new InjectionContext(entityInstance, UsesInstance.EMPTY_USES, state);
    Object mixin = model.newInstance(injectionContext);
    mixins[methodIndex.get(method)] = mixin;
    return mixin;
}
Also used : MixinModel(org.qi4j.runtime.composite.MixinModel) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 3 with MixinModel

use of org.qi4j.runtime.composite.MixinModel in project qi4j-sdk by Qi4j.

the class ServiceModel method newInstance.

public ServiceInstance newInstance(final ModuleInstance module) {
    Object[] mixins = mixinsModel.newMixinHolder();
    Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
    for (PropertyModel propertyModel : stateModel.properties()) {
        Object initialValue = propertyModel.initialValue(module);
        if (propertyModel.accessor().equals(identityMethod)) {
            initialValue = identity;
        }
        Property property = new PropertyInstance<Object>(propertyModel, initialValue);
        properties.put(propertyModel.accessor(), property);
    }
    TransientStateInstance state = new TransientStateInstance(properties);
    ServiceInstance compositeInstance = new ServiceInstance(this, module, mixins, state);
    // Instantiate all mixins
    int i = 0;
    UsesInstance uses = UsesInstance.EMPTY_USES.use(this);
    InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
    for (MixinModel mixinModel : mixinsModel.mixinModels()) {
        mixins[i++] = mixinModel.newInstance(injectionContext);
    }
    return compositeInstance;
}
Also used : UsesInstance(org.qi4j.runtime.composite.UsesInstance) MixinModel(org.qi4j.runtime.composite.MixinModel) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) InjectionContext(org.qi4j.runtime.injection.InjectionContext) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) Property(org.qi4j.api.property.Property)

Example 4 with MixinModel

use of org.qi4j.runtime.composite.MixinModel in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method implementMethodWithClass.

private MixinModel implementMethodWithClass(Method method, Class mixinClass) {
    MixinModel mixinModel = mixinsModel.getMixinModel(mixinClass);
    if (mixinModel == null) {
        mixinModel = helper.getMixinModel(mixinClass);
        mixinsModel.addMixinModel(mixinModel);
    }
    mixinsModel.addMethodMixin(method, mixinModel);
    return mixinModel;
}
Also used : MixinModel(org.qi4j.runtime.composite.MixinModel)

Example 5 with MixinModel

use of org.qi4j.runtime.composite.MixinModel in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method implementMixinType.

protected void implementMixinType(Iterable<? extends Class<?>> types, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses, Iterable<Class<?>> concernClasses, Iterable<Class<?>> sideEffectClasses, Iterable<Class<?>> mixinClasses) {
    Set<Class<?>> thisDependencies = new HashSet<Class<?>>();
    for (Class<?> mixinType : types) {
        for (Method method : mixinType.getMethods()) {
            if (!compositeMethodsModel.isImplemented(method) && !Proxy.class.equals(method.getDeclaringClass().getSuperclass()) && !Proxy.class.equals(method.getDeclaringClass()) && !Modifier.isStatic(method.getModifiers())) {
                MixinModel mixinModel = implementMethod(method, mixinClasses);
                ConcernsModel concernsModel = concernsFor(method, mixinModel.mixinClass(), Iterables.<Class<?>>flatten(concernDeclarations(mixinModel.mixinClass()), concernClasses));
                SideEffectsModel sideEffectsModel = sideEffectsFor(method, mixinModel.mixinClass(), Iterables.<Class<?>>flatten(sideEffectDeclarations(mixinModel.mixinClass()), sideEffectClasses));
                method.setAccessible(true);
                ConstraintsModel constraints = constraintsFor(method, Iterables.<Class<? extends Constraint<?, ?>>>flatten(constraintDeclarations(mixinModel.mixinClass()), constraintClasses));
                CompositeMethodModel methodComposite = new CompositeMethodModel(method, constraints, concernsModel, sideEffectsModel, mixinsModel);
                // Implement @This references
                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()));
                Iterable<Class<?>> filter = filter(not(in(Initializable.class, Lifecycle.class, InvocationHandler.class)), map(Classes.RAW_CLASS, interfacesOf(mixinModel.mixinClass())));
                Iterable<? extends Class<?>> flatten = flatten(map, map1, filter);
                Iterables.addAll(thisDependencies, flatten);
                compositeMethodsModel.addMethod(methodComposite);
            }
        }
        // Add type to set of mixin types
        mixinsModel.addMixinType(mixinType);
    }
    // Implement all @This dependencies that were found
    for (Class<?> thisDependency : thisDependencies) {
        // Add additional declarations from the @This type
        Iterable<Class<? extends Constraint<?, ?>>> typeConstraintClasses = Iterables.flatten(constraintClasses, constraintDeclarations(thisDependency));
        Iterable<Class<?>> typeConcernClasses = Iterables.flatten(concernClasses, concernDeclarations(thisDependency));
        Iterable<Class<?>> typeSideEffectClasses = Iterables.flatten(sideEffectClasses, sideEffectDeclarations(thisDependency));
        Iterable<Class<?>> typeMixinClasses = Iterables.flatten(mixinClasses, mixinDeclarations(thisDependency));
        Iterable<? extends Class<?>> singleton = Iterables.iterable(thisDependency);
        implementMixinType(singleton, typeConstraintClasses, typeConcernClasses, typeSideEffectClasses, typeMixinClasses);
    }
}
Also used : MixinModel(org.qi4j.runtime.composite.MixinModel) DependencyModel(org.qi4j.runtime.injection.DependencyModel) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ConstraintsModel(org.qi4j.runtime.composite.ConstraintsModel) Constraint(org.qi4j.api.constraint.Constraint) ConcernsModel(org.qi4j.runtime.composite.ConcernsModel) SideEffectsModel(org.qi4j.runtime.composite.SideEffectsModel) Method(java.lang.reflect.Method) CompositeMethodModel(org.qi4j.runtime.composite.CompositeMethodModel) Proxy(java.lang.reflect.Proxy) HashSet(java.util.HashSet)

Aggregations

MixinModel (org.qi4j.runtime.composite.MixinModel)6 InjectionContext (org.qi4j.runtime.injection.InjectionContext)3 AccessibleObject (java.lang.reflect.AccessibleObject)1 Method (java.lang.reflect.Method)1 Proxy (java.lang.reflect.Proxy)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)1 Constraint (org.qi4j.api.constraint.Constraint)1 Property (org.qi4j.api.property.Property)1 CompositeMethodModel (org.qi4j.runtime.composite.CompositeMethodModel)1 ConcernsModel (org.qi4j.runtime.composite.ConcernsModel)1 ConstraintsModel (org.qi4j.runtime.composite.ConstraintsModel)1 SideEffectsModel (org.qi4j.runtime.composite.SideEffectsModel)1 TransientStateInstance (org.qi4j.runtime.composite.TransientStateInstance)1 UsesInstance (org.qi4j.runtime.composite.UsesInstance)1 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)1 DependencyModel (org.qi4j.runtime.injection.DependencyModel)1 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)1 PropertyModel (org.qi4j.runtime.property.PropertyModel)1