Search in sources :

Example 1 with ConstraintsModel

use of org.qi4j.runtime.composite.ConstraintsModel 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<>();
    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()));
                @SuppressWarnings("unchecked") 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);
                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 = flatten(constraintClasses, constraintDeclarations(thisDependency));
        Iterable<Class<?>> typeConcernClasses = flatten(concernClasses, concernDeclarations(thisDependency));
        Iterable<Class<?>> typeSideEffectClasses = flatten(sideEffectClasses, sideEffectDeclarations(thisDependency));
        Iterable<Class<?>> typeMixinClasses = flatten(mixinClasses, mixinDeclarations(thisDependency));
        @SuppressWarnings("unchecked") Iterable<? extends Class<?>> singleton = 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) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass) HashSet(java.util.HashSet)

Example 2 with ConstraintsModel

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

the class CompositeAssemblyImpl method constraintsFor.

// Model
private ConstraintsModel constraintsFor(Method method, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    List<ValueConstraintsModel> parameterConstraintModels = Collections.emptyList();
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    Type[] parameterTypes = method.getGenericParameterTypes();
    boolean constrained = false;
    for (int i = 0; i < parameterAnnotations.length; i++) {
        Annotation[] parameterAnnotation = parameterAnnotations[i];
        Name nameAnnotation = (Name) first(filter(isType(Name.class), iterable(parameterAnnotation)));
        String name = nameAnnotation == null ? "param" + (i + 1) : nameAnnotation.value();
        boolean optional = first(filter(isType(Optional.class), iterable(parameterAnnotation))) != null;
        ValueConstraintsModel parameterConstraintsModel = constraintsFor(asList(parameterAnnotation), parameterTypes[i], name, optional, constraintClasses, method);
        if (parameterConstraintsModel.isConstrained()) {
            constrained = true;
        }
        if (parameterConstraintModels.isEmpty()) {
            parameterConstraintModels = new ArrayList<>();
        }
        parameterConstraintModels.add(parameterConstraintsModel);
    }
    if (!constrained) {
        return new ConstraintsModel(Collections.<ValueConstraintsModel>emptyList());
    } else {
        return new ConstraintsModel(parameterConstraintModels);
    }
}
Also used : Optional(org.qi4j.api.common.Optional) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ConstraintsModel(org.qi4j.runtime.composite.ConstraintsModel) Constraint(org.qi4j.api.constraint.Constraint) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) Annotation(java.lang.annotation.Annotation) QualifiedName(org.qi4j.api.common.QualifiedName) Name(org.qi4j.api.constraint.Name) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel)

Aggregations

Constraint (org.qi4j.api.constraint.Constraint)2 ConstraintsModel (org.qi4j.runtime.composite.ConstraintsModel)2 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Proxy (java.lang.reflect.Proxy)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Optional (org.qi4j.api.common.Optional)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 Name (org.qi4j.api.constraint.Name)1 Annotations.hasAnnotation (org.qi4j.api.util.Annotations.hasAnnotation)1 Annotations.isType (org.qi4j.api.util.Annotations.isType)1 Classes.wrapperClass (org.qi4j.api.util.Classes.wrapperClass)1 CompositeMethodModel (org.qi4j.runtime.composite.CompositeMethodModel)1 ConcernsModel (org.qi4j.runtime.composite.ConcernsModel)1 MixinModel (org.qi4j.runtime.composite.MixinModel)1 SideEffectsModel (org.qi4j.runtime.composite.SideEffectsModel)1 DependencyModel (org.qi4j.runtime.injection.DependencyModel)1