Search in sources :

Example 1 with ConstraintModel

use of org.qi4j.runtime.composite.ConstraintModel 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)

Aggregations

Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)1 Constraint (org.qi4j.api.constraint.Constraint)1 ConstraintDeclaration (org.qi4j.api.constraint.ConstraintDeclaration)1 Constraints (org.qi4j.api.constraint.Constraints)1 AbstractConstraintModel (org.qi4j.runtime.composite.AbstractConstraintModel)1 CompositeConstraintModel (org.qi4j.runtime.composite.CompositeConstraintModel)1 ConstraintModel (org.qi4j.runtime.composite.ConstraintModel)1 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)1