Search in sources :

Example 1 with InvalidCompositeException

use of org.qi4j.api.composite.InvalidCompositeException 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 InvalidCompositeException

use of org.qi4j.api.composite.InvalidCompositeException 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 3 with InvalidCompositeException

use of org.qi4j.api.composite.InvalidCompositeException in project qi4j-sdk by Qi4j.

the class CompositeModel method createProxyClass.

@SuppressWarnings("unchecked")
private void createProxyClass() {
    Class<?> mainType = first(types);
    if (mainType.isInterface()) {
        ClassLoader proxyClassloader = mainType.getClassLoader();
        Class<?>[] interfaces = Iterables.toArray(Class.class, Iterables.<Class>cast(types));
        proxyClass = (Class<? extends Composite>) ProxyGenerator.createProxyClass(proxyClassloader, interfaces);
        try {
            proxyConstructor = proxyClass.getConstructor(InvocationHandler.class);
        } catch (NoSuchMethodException e) {
            throw (InvalidCompositeException) new InvalidCompositeException("Could not get proxy constructor").initCause(e);
        }
        proxyConstructor.setAccessible(true);
    } else {
        try {
            proxyClass = new TransientClassLoader(getClass().getClassLoader()).loadFragmentClass(mainType);
            proxyConstructor = (Constructor<? extends Composite>) proxyClass.getConstructors()[0];
        } catch (ClassNotFoundException e) {
            throw (InvalidCompositeException) new InvalidCompositeException("Could not get proxy constructor").initCause(e);
        }
    }
}
Also used : InvalidCompositeException(org.qi4j.api.composite.InvalidCompositeException) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)3 Annotation (java.lang.annotation.Annotation)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 ArrayList (java.util.ArrayList)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 MixinModel (org.qi4j.runtime.composite.MixinModel)1 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)1