Search in sources :

Example 1 with Constraints

use of org.qi4j.api.constraint.Constraints 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 Constraints

use of org.qi4j.api.constraint.Constraints 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<>();
    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 (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) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) 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) Classes.wrapperClass(org.qi4j.api.util.Classes.wrapperClass)

Example 3 with Constraints

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

the class InteractionConstraintsService method findConstraints.

private InteractionConstraintsBinding findConstraints(Class aClass, Module module) {
    List<Binding> classConstraintBindings = new ArrayList<Binding>();
    for (Annotation annotation : aClass.getAnnotations()) {
        if (annotation.annotationType().getAnnotation(ConstraintDeclaration.class) != null) {
            Constraints constraints = annotation.annotationType().getAnnotation(Constraints.class);
            for (Class<? extends Constraint<?, ?>> constraintClass : constraints.value()) {
                try {
                    Constraint<Annotation, Object> constraint = (Constraint<Annotation, Object>) constraintClass.newInstance();
                    Class roleClass = (Class) ((ParameterizedType) constraint.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[1];
                    ConstraintBinding constraintBinding = new ConstraintBinding(constraint, annotation, roleClass);
                    classConstraintBindings.add(constraintBinding);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        } else if (annotation.annotationType().getAnnotation(InteractionConstraintDeclaration.class) != null) {
            Class<? extends InteractionConstraint> constraintClass = annotation.annotationType().getAnnotation(InteractionConstraintDeclaration.class).value();
            InteractionConstraint<Annotation> constraint = null;
            try {
                try {
                    constraint = module.newObject(constraintClass);
                } catch (NoSuchObjectException e) {
                    constraint = constraintClass.newInstance();
                }
            } catch (Exception e) {
                // Skip this constraint
                continue;
            }
            InteractionConstraintBinding constraintBinding = new InteractionConstraintBinding(constraint, annotation);
            classConstraintBindings.add(constraintBinding);
        }
    }
    if (classConstraintBindings.isEmpty()) {
        classConstraintBindings = null;
    }
    return new InteractionConstraintsBinding(classConstraintBindings);
}
Also used : InteractionConstraint(org.qi4j.library.rest.server.api.constraint.InteractionConstraint) Constraint(org.qi4j.api.constraint.Constraint) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) ParameterizedType(java.lang.reflect.ParameterizedType) Constraints(org.qi4j.api.constraint.Constraints) ConstraintDeclaration(org.qi4j.api.constraint.ConstraintDeclaration) InteractionConstraintDeclaration(org.qi4j.library.rest.server.api.constraint.InteractionConstraintDeclaration) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) InteractionConstraint(org.qi4j.library.rest.server.api.constraint.InteractionConstraint)

Example 4 with Constraints

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

the class InteractionConstraintsService method findConstraints.

private InteractionConstraintsBinding findConstraints(Method method, Module module) {
    List<Binding> methodConstraintBindings = new ArrayList<Binding>();
    for (Annotation annotation : method.getAnnotations()) {
        if (annotation.annotationType().equals(RequiresValid.class)) {
            RequiresValid requiresValid = (RequiresValid) annotation;
            Class contextClass = method.getDeclaringClass();
            if (InteractionValidation.class.isAssignableFrom(contextClass)) {
                InteractionValidation validation = null;
                if (TransientComposite.class.isAssignableFrom(contextClass)) {
                    validation = (InteractionValidation) module.newTransient(contextClass);
                } else {
                    validation = (InteractionValidation) module.newObject(contextClass);
                }
                methodConstraintBindings.add(new RequiresValidBinding(requiresValid, validation));
            }
        } else if (annotation.annotationType().getAnnotation(ConstraintDeclaration.class) != null) {
            Constraints constraints = annotation.annotationType().getAnnotation(Constraints.class);
            for (Class<? extends Constraint<?, ?>> aClass : constraints.value()) {
                try {
                    Constraint<Annotation, Object> constraint = (Constraint<Annotation, Object>) aClass.newInstance();
                    Class roleClass = (Class) ((ParameterizedType) aClass.getGenericInterfaces()[0]).getActualTypeArguments()[1];
                    ConstraintBinding constraintBinding = new ConstraintBinding(constraint, annotation, roleClass);
                    methodConstraintBindings.add(constraintBinding);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        } else if (annotation.annotationType().getAnnotation(InteractionConstraintDeclaration.class) != null) {
            Class<? extends InteractionConstraint> constraintClass = annotation.annotationType().getAnnotation(InteractionConstraintDeclaration.class).value();
            InteractionConstraint<Annotation> constraint = null;
            try {
                try {
                    constraint = module.newObject(constraintClass);
                } catch (NoSuchObjectException e) {
                    constraint = constraintClass.newInstance();
                }
            } catch (Exception e) {
                // Skip this constraint
                continue;
            }
            InteractionConstraintBinding constraintBinding = new InteractionConstraintBinding(constraint, annotation);
            methodConstraintBindings.add(constraintBinding);
        }
    }
    if (methodConstraintBindings.isEmpty()) {
        methodConstraintBindings = null;
    }
    return new InteractionConstraintsBinding(methodConstraintBindings);
}
Also used : InteractionConstraintDeclaration(org.qi4j.library.rest.server.api.constraint.InteractionConstraintDeclaration) InteractionConstraint(org.qi4j.library.rest.server.api.constraint.InteractionConstraint) Constraint(org.qi4j.api.constraint.Constraint) ArrayList(java.util.ArrayList) RequiresValid(org.qi4j.library.rest.server.api.constraint.RequiresValid) Annotation(java.lang.annotation.Annotation) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) Constraints(org.qi4j.api.constraint.Constraints) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) InteractionValidation(org.qi4j.library.rest.server.api.constraint.InteractionValidation)

Aggregations

Constraint (org.qi4j.api.constraint.Constraint)4 Constraints (org.qi4j.api.constraint.Constraints)4 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 ConstraintDeclaration (org.qi4j.api.constraint.ConstraintDeclaration)2 NoSuchObjectException (org.qi4j.api.object.NoSuchObjectException)2 Classes.wrapperClass (org.qi4j.api.util.Classes.wrapperClass)2 InteractionConstraint (org.qi4j.library.rest.server.api.constraint.InteractionConstraint)2 InteractionConstraintDeclaration (org.qi4j.library.rest.server.api.constraint.InteractionConstraintDeclaration)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)1 Annotations.hasAnnotation (org.qi4j.api.util.Annotations.hasAnnotation)1 Annotations.isType (org.qi4j.api.util.Annotations.isType)1 Function (org.qi4j.functional.Function)1 InteractionValidation (org.qi4j.library.rest.server.api.constraint.InteractionValidation)1 RequiresValid (org.qi4j.library.rest.server.api.constraint.RequiresValid)1 AbstractConstraintModel (org.qi4j.runtime.composite.AbstractConstraintModel)1 CompositeConstraintModel (org.qi4j.runtime.composite.CompositeConstraintModel)1 ConstraintModel (org.qi4j.runtime.composite.ConstraintModel)1