Search in sources :

Example 1 with Constraint

use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method constraintDeclarations.

private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(ArrayList<Type> allTypes) {
    // Find all constraints and flatten them into an iterable
    Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {

        @Override
        public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
            Constraints constraints = Annotations.annotationOn(type, Constraints.class);
            if (constraints == null) {
                return empty();
            } else {
                return iterable(constraints.value());
            }
        }
    };
    Iterable<Class<? extends Constraint<?, ?>>> flatten = flattenIterables(map(function, allTypes));
    return toList(flatten);
}
Also used : Function(org.qi4j.functional.Function) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) Constraints(org.qi4j.api.constraint.Constraints) Constraint(org.qi4j.api.constraint.Constraint) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass)

Example 2 with Constraint

use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method buildComposite.

protected void buildComposite(AssemblyHelper helper, StateDeclarations stateDeclarations) {
    this.stateDeclarations = stateDeclarations;
    this.helper = helper;
    for (Class<?> compositeType : types) {
        metaInfo = new MetaInfo(metaInfo).withAnnotations(compositeType);
        addAnnotationsMetaInfo(compositeType, metaInfo);
    }
    immutable = metaInfo.get(Immutable.class) != null;
    propertiesModel = new PropertiesModel();
    stateModel = createStateModel();
    mixinsModel = createMixinsModel();
    compositeMethodsModel = new CompositeMethodsModel(mixinsModel);
    // Implement composite methods
    Iterable<Class<? extends Constraint<?, ?>>> constraintClasses = constraintDeclarations(this.types);
    Iterable<Class<?>> concernClasses = Iterables.flatten(concerns, concernDeclarations(this.types));
    Iterable<Class<?>> sideEffectClasses = Iterables.flatten(sideEffects, sideEffectDeclarations(this.types));
    Iterable<Class<?>> mixinClasses = Iterables.flatten(mixins, mixinDeclarations(this.types));
    implementMixinType(types, constraintClasses, concernClasses, sideEffectClasses, mixinClasses);
    // Add state from methods and fields
    addState(constraintClasses);
}
Also used : CompositeMethodsModel(org.qi4j.runtime.composite.CompositeMethodsModel) Constraint(org.qi4j.api.constraint.Constraint) MetaInfo(org.qi4j.api.common.MetaInfo) PropertiesModel(org.qi4j.runtime.property.PropertiesModel)

Example 3 with Constraint

use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method constraintsFor.

protected ValueConstraintsModel constraintsFor(Iterable<Annotation> constraintAnnotations, Type valueType, String name, boolean optional, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses, AccessibleObject accessor) {
    valueType = wrapperClass(valueType);
    List<AbstractConstraintModel> constraintModels = new ArrayList<AbstractConstraintModel>();
    nextConstraint: for (Annotation constraintAnnotation : filter(translate(type(), hasAnnotation(ConstraintDeclaration.class)), constraintAnnotations)) {
        // Check composite declarations first
        Class<? extends Annotation> annotationType = constraintAnnotation.annotationType();
        for (Class<? extends Constraint<?, ?>> constraint : constraintClasses) {
            if (helper.appliesTo(constraint, annotationType, valueType)) {
                constraintModels.add(new ConstraintModel(constraintAnnotation, constraint));
                continue nextConstraint;
            }
        }
        // Check the annotation itself
        Constraints constraints = annotationType.getAnnotation(Constraints.class);
        if (constraints != null) {
            for (Class<? extends Constraint<?, ?>> constraintClass : constraints.value()) {
                if (helper.appliesTo(constraintClass, annotationType, valueType)) {
                    constraintModels.add(new ConstraintModel(constraintAnnotation, constraintClass));
                    continue nextConstraint;
                }
            }
        }
        // No implementation found!
        // Check if if it's a composite constraints
        Iterable<Annotation> annotations = iterable(annotationType.getAnnotations());
        if (Iterables.matchesAny(translate(type(), hasAnnotation(ConstraintDeclaration.class)), annotations)) {
            ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, valueType, name, optional, constraintClasses, accessor);
            CompositeConstraintModel compositeConstraintModel = new CompositeConstraintModel(constraintAnnotation, valueConstraintsModel);
            constraintModels.add(compositeConstraintModel);
            continue nextConstraint;
        }
        throw new InvalidCompositeException("Cannot find implementation of constraint @" + annotationType.getSimpleName() + " for " + valueType + " in method " + ((Member) accessor).getName() + " of composite " + types);
    }
    return new ValueConstraintsModel(constraintModels, name, optional);
}
Also used : InvalidCompositeException(org.qi4j.api.composite.InvalidCompositeException) Constraint(org.qi4j.api.constraint.Constraint) ArrayList(java.util.ArrayList) ConstraintModel(org.qi4j.runtime.composite.ConstraintModel) CompositeConstraintModel(org.qi4j.runtime.composite.CompositeConstraintModel) AbstractConstraintModel(org.qi4j.runtime.composite.AbstractConstraintModel) Annotation(java.lang.annotation.Annotation) AbstractConstraintModel(org.qi4j.runtime.composite.AbstractConstraintModel) CompositeConstraintModel(org.qi4j.runtime.composite.CompositeConstraintModel) Constraints(org.qi4j.api.constraint.Constraints) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ConstraintDeclaration(org.qi4j.api.constraint.ConstraintDeclaration)

Example 4 with Constraint

use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method constraintDeclarations.

private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(Iterable<? extends Class<?>> typess) {
    // Find constraint declarations
    List<Type> allTypes = new ArrayList<Type>();
    for (Class<?> type : typess) {
        Iterable<Type> types = typesOf(type);
        Iterables.addAll(allTypes, types);
    }
    // Find all constraints and flatten them into an iterable
    Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {

        @Override
        public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
            Constraints constraints = Annotations.annotationOn(type, Constraints.class);
            if (constraints == null) {
                return Iterables.empty();
            } else {
                return iterable(constraints.value());
            }
        }
    };
    Iterable<Class<? extends Constraint<?, ?>>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
    return Iterables.toList(flatten);
}
Also used : Function(org.qi4j.functional.Function) Type(java.lang.reflect.Type) Constraints(org.qi4j.api.constraint.Constraints) Constraint(org.qi4j.api.constraint.Constraint) ArrayList(java.util.ArrayList)

Example 5 with Constraint

use of org.qi4j.api.constraint.Constraint 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) Iterables.first(Iterables.filter(isType(Name.class), iterable(parameterAnnotation)));
        String name = nameAnnotation == null ? "param" + (i + 1) : nameAnnotation.value();
        boolean optional = Iterables.first(Iterables.filter(isType(Optional.class), iterable(parameterAnnotation))) != null;
        ValueConstraintsModel parameterConstraintsModel = constraintsFor(Arrays.asList(parameterAnnotation), parameterTypes[i], name, optional, constraintClasses, method);
        if (parameterConstraintsModel.isConstrained()) {
            constrained = true;
        }
        if (parameterConstraintModels.isEmpty()) {
            parameterConstraintModels = new ArrayList<ValueConstraintsModel>();
        }
        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) Annotation(java.lang.annotation.Annotation) QualifiedName(org.qi4j.api.common.QualifiedName) Name(org.qi4j.api.constraint.Name) Type(java.lang.reflect.Type) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel)

Aggregations

Constraint (org.qi4j.api.constraint.Constraint)8 Constraints (org.qi4j.api.constraint.Constraints)5 Annotation (java.lang.annotation.Annotation)4 ArrayList (java.util.ArrayList)4 Type (java.lang.reflect.Type)3 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)3 ConstraintDeclaration (org.qi4j.api.constraint.ConstraintDeclaration)2 NoSuchObjectException (org.qi4j.api.object.NoSuchObjectException)2 Function (org.qi4j.functional.Function)2 InteractionConstraint (org.qi4j.library.rest.server.api.constraint.InteractionConstraint)2 InteractionConstraintDeclaration (org.qi4j.library.rest.server.api.constraint.InteractionConstraintDeclaration)2 ConstraintsModel (org.qi4j.runtime.composite.ConstraintsModel)2 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Proxy (java.lang.reflect.Proxy)1 HashSet (java.util.HashSet)1 MetaInfo (org.qi4j.api.common.MetaInfo)1 Optional (org.qi4j.api.common.Optional)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)1