Search in sources :

Example 1 with DependencyModel

use of org.qi4j.runtime.injection.DependencyModel in project qi4j-sdk by Qi4j.

the class ServiceModel method calculateConfigurationType.

@SuppressWarnings({ "raw", "unchecked" })
public Class calculateConfigurationType() {
    Class injectionClass = null;
    Iterable<DependencyModel> configurationThisDependencies = filter(and(translate(new DependencyModel.InjectionTypeFunction(), Specifications.<Class<?>>in(Configuration.class)), new DependencyModel.ScopeSpecification(This.class)), dependencies());
    for (DependencyModel dependencyModel : configurationThisDependencies) {
        if (dependencyModel.rawInjectionType().equals(Configuration.class) && dependencyModel.injectionType() instanceof ParameterizedType) {
            Class<?> type = Classes.RAW_CLASS.map(((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0]);
            if (injectionClass == null) {
                injectionClass = type;
            } else {
                if (injectionClass.isAssignableFrom(type)) {
                    injectionClass = type;
                }
            }
        }
    }
    return injectionClass;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) DependencyModel(org.qi4j.runtime.injection.DependencyModel) Configuration(org.qi4j.api.configuration.Configuration)

Example 2 with DependencyModel

use of org.qi4j.runtime.injection.DependencyModel in project qi4j-sdk by Qi4j.

the class ConstructorsModel method newConstructorModel.

@SuppressWarnings("raw")
private ConstructorModel newConstructorModel(Class fragmentClass, Constructor realConstructor, Constructor injectedConstructor) {
    int idx = 0;
    InjectedParametersModel parameters = new InjectedParametersModel();
    Annotation[][] parameterAnnotations = injectedConstructor.getParameterAnnotations();
    for (Type type : injectedConstructor.getGenericParameterTypes()) {
        Annotation injectionAnnotation = first(filter(Specifications.translate(Annotations.type(), Annotations.hasAnnotation(InjectionScope.class)), iterable(parameterAnnotations[idx])));
        if (injectionAnnotation == null) {
            if (fragmentClass.getSuperclass().isMemberClass()) {
                injectionAnnotation = new Uses() {

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        return Uses.class;
                    }
                };
            } else {
                // invalid constructor parameter
                return null;
            }
        }
        boolean optional = DependencyModel.isOptional(injectionAnnotation, parameterAnnotations[idx]);
        Type genericType = type;
        if (genericType instanceof ParameterizedType) {
            genericType = new ParameterizedTypeInstance(((ParameterizedType) genericType).getActualTypeArguments(), ((ParameterizedType) genericType).getRawType(), ((ParameterizedType) genericType).getOwnerType());
            for (int i = 0; i < ((ParameterizedType) genericType).getActualTypeArguments().length; i++) {
                Type typeArg = ((ParameterizedType) genericType).getActualTypeArguments()[i];
                if (typeArg instanceof TypeVariable) {
                    typeArg = Classes.resolveTypeVariable((TypeVariable) typeArg, realConstructor.getDeclaringClass(), fragmentClass);
                    ((ParameterizedType) genericType).getActualTypeArguments()[i] = typeArg;
                }
            }
        }
        DependencyModel dependencyModel = new DependencyModel(injectionAnnotation, genericType, fragmentClass, optional, parameterAnnotations[idx]);
        parameters.addDependency(dependencyModel);
        idx++;
    }
    return new ConstructorModel(realConstructor, parameters);
}
Also used : DependencyModel(org.qi4j.runtime.injection.DependencyModel) Uses(org.qi4j.api.injection.scope.Uses) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterizedTypeInstance(org.qi4j.runtime.injection.ParameterizedTypeInstance) TypeVariable(java.lang.reflect.TypeVariable) InjectionScope(org.qi4j.api.injection.InjectionScope) InjectedParametersModel(org.qi4j.runtime.injection.InjectedParametersModel)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)2 DependencyModel (org.qi4j.runtime.injection.DependencyModel)2 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 Configuration (org.qi4j.api.configuration.Configuration)1 InjectionScope (org.qi4j.api.injection.InjectionScope)1 Uses (org.qi4j.api.injection.scope.Uses)1 InjectedParametersModel (org.qi4j.runtime.injection.InjectedParametersModel)1 ParameterizedTypeInstance (org.qi4j.runtime.injection.ParameterizedTypeInstance)1