Search in sources :

Example 1 with NoSuchObjectException

use of org.qi4j.api.object.NoSuchObjectException in project qi4j-sdk by Qi4j.

the class ModuleInstance method newObject.

// Implementation of ObjectFactory
@Override
public <T> T newObject(Class<T> mixinType, Object... uses) throws NoSuchObjectException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<ObjectModel> modelModule = typeLookup.lookupObjectModel(mixinType);
    if (modelModule == null) {
        throw new NoSuchObjectException(mixinType.getName(), name());
    }
    InjectionContext injectionContext = new InjectionContext(modelModule.module(), UsesInstance.EMPTY_USES.use(uses));
    return mixinType.cast(modelModule.model().newInstance(injectionContext));
}
Also used : ObjectModel(org.qi4j.runtime.object.ObjectModel) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 2 with NoSuchObjectException

use of org.qi4j.api.object.NoSuchObjectException in project qi4j-sdk by Qi4j.

the class ModuleInstance method injectTo.

@Override
public void injectTo(Object instance, Object... uses) throws ConstructionException {
    NullArgumentException.validateNotNull("instance", instance);
    ModelModule<ObjectModel> modelModule = typeLookup.lookupObjectModel(instance.getClass());
    if (modelModule == null) {
        throw new NoSuchObjectException(instance.getClass().getName(), name());
    }
    InjectionContext injectionContext = new InjectionContext(modelModule.module(), UsesInstance.EMPTY_USES.use(uses));
    modelModule.model().inject(injectionContext, instance);
}
Also used : ObjectModel(org.qi4j.runtime.object.ObjectModel) NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 3 with NoSuchObjectException

use of org.qi4j.api.object.NoSuchObjectException 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 NoSuchObjectException

use of org.qi4j.api.object.NoSuchObjectException 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)

Example 5 with NoSuchObjectException

use of org.qi4j.api.object.NoSuchObjectException in project qi4j-sdk by Qi4j.

the class Qi4jObjectFactory method createQi4jObject.

@SuppressWarnings("unchecked")
private Object createQi4jObject(Class aClass, boolean isAddToTypes) {
    if (objectFactory == null) {
        return null;
    }
    ConstructionException exception = null;
    Object obj = null;
    try {
        obj = objectFactory.newObject(aClass);
    } catch (NoSuchObjectException e) {
        return null;
    } catch (ConstructionException e) {
        exception = e;
    }
    if (isAddToTypes) {
        addToType(aClass, qi4jObject);
    }
    if (exception != null) {
        throw exception;
    }
    return obj;
}
Also used : NoSuchObjectException(org.qi4j.api.object.NoSuchObjectException) ConstructionException(org.qi4j.api.common.ConstructionException)

Aggregations

NoSuchObjectException (org.qi4j.api.object.NoSuchObjectException)5 Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 Constraint (org.qi4j.api.constraint.Constraint)2 Constraints (org.qi4j.api.constraint.Constraints)2 InteractionConstraint (org.qi4j.library.rest.server.api.constraint.InteractionConstraint)2 InteractionConstraintDeclaration (org.qi4j.library.rest.server.api.constraint.InteractionConstraintDeclaration)2 InjectionContext (org.qi4j.runtime.injection.InjectionContext)2 ObjectModel (org.qi4j.runtime.object.ObjectModel)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 ConstructionException (org.qi4j.api.common.ConstructionException)1 ConstraintDeclaration (org.qi4j.api.constraint.ConstraintDeclaration)1 InteractionValidation (org.qi4j.library.rest.server.api.constraint.InteractionValidation)1 RequiresValid (org.qi4j.library.rest.server.api.constraint.RequiresValid)1