Search in sources :

Example 1 with Qualifier

use of javax.inject.Qualifier in project dagger by square.

the class ValidationProcessor method validateQualifiers.

private void validateQualifiers(Element element, Map<Element, Element> parametersToTheirMethods) {
    boolean suppressWarnings = element.getAnnotation(SuppressWarnings.class) != null && Arrays.asList(element.getAnnotation(SuppressWarnings.class).value()).contains("qualifiers");
    int numberOfQualifiersOnElement = 0;
    for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
        if (annotation.getAnnotationType().asElement().getAnnotation(Qualifier.class) == null) {
            continue;
        }
        switch(element.getKind()) {
            case FIELD:
                numberOfQualifiersOnElement++;
                if (element.getAnnotation(Inject.class) == null && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on fields that are not " + "annotated with @Inject: " + elementToString(element), element);
                }
                break;
            case METHOD:
                numberOfQualifiersOnElement++;
                if (!isProvidesMethod(element) && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on methods that are not " + "@Provides methods: " + elementToString(element), element);
                }
                break;
            case PARAMETER:
                numberOfQualifiersOnElement++;
                if (!isInjectableConstructorParameter(element, parametersToTheirMethods) && !isProvidesMethodParameter(element, parametersToTheirMethods) && !suppressWarnings) {
                    warning("Dagger will ignore qualifier annotations on parameters that are not " + "@Inject constructor parameters or @Provides method parameters: " + elementToString(element), element);
                }
                break;
            default:
                error("Qualifier annotations are only allowed on fields, methods, and parameters: " + elementToString(element), element);
        }
    }
    if (numberOfQualifiersOnElement > 1) {
        error("Only one qualifier annotation is allowed per element: " + elementToString(element), element);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Qualifier(javax.inject.Qualifier)

Example 2 with Qualifier

use of javax.inject.Qualifier in project kernel by exoplatform.

the class ConcurrentContainer method resolveType.

/**
 * Resolves the given type and generic type
 */
private Object resolveType(Class<?> type, Type genericType, Annotation[] annotations, String logMessagePrefix) {
    if (type.isPrimitive()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(logMessagePrefix + ": Primitive types are not supported");
        }
        return 1;
    }
    Named named = null;
    Class<?> qualifier = null;
    for (int i = 0, length = annotations.length; i < length; i++) {
        Annotation a = annotations[i];
        if (a instanceof Named) {
            named = (Named) a;
            break;
        } else if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
            qualifier = a.annotationType();
            break;
        }
    }
    if (type.isInterface() && type.equals(Provider.class)) {
        if (!(genericType instanceof ParameterizedType)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(logMessagePrefix + ": The generic type is not of type ParameterizedType");
            }
            return 2;
        }
        ParameterizedType aType = (ParameterizedType) genericType;
        Type[] typeVars = aType.getActualTypeArguments();
        Class<?> expectedType = (Class<?>) typeVars[0];
        final ComponentAdapter<?> adapter;
        if (named != null) {
            adapter = holder.getComponentAdapter(named.value(), expectedType);
        } else if (qualifier != null) {
            adapter = holder.getComponentAdapter(qualifier, expectedType);
        } else {
            adapter = holder.getComponentAdapterOfType(expectedType);
        }
        if (adapter == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(logMessagePrefix + ": We have no value to set so we skip it");
            }
            return 3;
        }
        return new Provider<Object>() {

            public Object get() {
                return adapter.getComponentInstance();
            }
        };
    } else {
        if (named != null) {
            return holder.getComponentInstance(named.value(), type);
        } else if (qualifier != null) {
            return holder.getComponentInstance(qualifier, type);
        } else {
            return holder.getComponentInstanceOfType(type);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Named(javax.inject.Named) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Qualifier(javax.inject.Qualifier) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider)

Example 3 with Qualifier

use of javax.inject.Qualifier in project kernel by exoplatform.

the class ConcurrentContainerMT method resolveType.

/**
 * Resolves the given type and generic type
 */
private Object resolveType(final Class<?> type, Type genericType, Annotation[] annotations, String logMessagePrefix, List<Dependency> dependencies) {
    if (type.isPrimitive()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(logMessagePrefix + ": Primitive types are not supported");
        }
        return 1;
    }
    Named named = null;
    Class<?> qualifier = null;
    for (int i = 0, length = annotations.length; i < length; i++) {
        Annotation a = annotations[i];
        if (a instanceof Named) {
            named = (Named) a;
            break;
        } else if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
            qualifier = a.annotationType();
            break;
        }
    }
    if (type.isInterface() && type.equals(Provider.class)) {
        if (!(genericType instanceof ParameterizedType)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(logMessagePrefix + ": The generic type is not of type ParameterizedType");
            }
            return 2;
        }
        ParameterizedType aType = (ParameterizedType) genericType;
        Type[] typeVars = aType.getActualTypeArguments();
        Class<?> expectedType = (Class<?>) typeVars[0];
        final ComponentAdapter<?> adapter;
        final Object key;
        if (named != null) {
            adapter = holder.getComponentAdapter(key = named.value(), expectedType);
        } else if (qualifier != null) {
            adapter = holder.getComponentAdapter(key = qualifier, expectedType);
        } else {
            key = expectedType;
            adapter = holder.getComponentAdapterOfType(expectedType);
        }
        if (adapter == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(logMessagePrefix + ": We have no value to set so we skip it");
            }
            return 3;
        }
        final Provider<Object> result = new Provider<Object>() {

            public Object get() {
                return adapter.getComponentInstance();
            }
        };
        dependencies.add(new DependencyByProvider(key, expectedType, result, adapter));
        return result;
    } else {
        if (named != null) {
            final String name = named.value();
            dependencies.add(new DependencyByName(name, type));
            return holder.getComponentAdapter(name, type);
        } else if (qualifier != null) {
            dependencies.add(new DependencyByQualifier(qualifier, type));
            return holder.getComponentAdapter(qualifier, type);
        } else {
            dependencies.add(new DependencyByType(type));
            return holder.getComponentAdapterOfType(type);
        }
    }
}
Also used : Named(javax.inject.Named) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Qualifier(javax.inject.Qualifier)

Example 4 with Qualifier

use of javax.inject.Qualifier in project auto by google.

the class Key method create.

/**
 * Constructs a key based on the type {@code type} and any {@link Qualifier}s in {@code
 * annotations}.
 *
 * <p>If {@code type} is a {@code Provider<T>}, the returned {@link Key}'s {@link #type()} is
 * {@code T}. If {@code type} is a primitive, the returned {@link Key}'s {@link #type()} is the
 * corresponding {@linkplain Types#boxedClass(PrimitiveType) boxed type}.
 *
 * <p>For example:
 * <table>
 *   <tr><th>Input type                <th>{@code Key.type()}
 *   <tr><td>{@code String}            <td>{@code String}
 *   <tr><td>{@code Provider<String>}  <td>{@code String}
 *   <tr><td>{@code int}               <td>{@code Integer}
 * </table>
 */
static Key create(TypeMirror type, Collection<AnnotationMirror> annotations, Types types) {
    // TODO(gak): check for only one qualifier rather than using the first
    Optional<AnnotationMirror> qualifier = annotations.stream().filter(annotation -> isAnnotationPresent(annotation.getAnnotationType().asElement(), Qualifier.class)).findFirst();
    TypeMirror keyType = isProvider(type) ? MoreTypes.asDeclared(type).getTypeArguments().get(0) : boxedType(type, types);
    return new AutoValue_Key(MoreTypes.equivalence().wrap(keyType), wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), qualifier));
}
Also used : MoreTypes(com.google.auto.common.MoreTypes) Equivalence(com.google.common.base.Equivalence) Mirrors.wrapOptionalInEquivalence(com.google.auto.factory.processor.Mirrors.wrapOptionalInEquivalence) Collection(java.util.Collection) Mirrors.isProvider(com.google.auto.factory.processor.Mirrors.isProvider) Types(javax.lang.model.util.Types) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Mirrors.unwrapOptionalEquivalence(com.google.auto.factory.processor.Mirrors.unwrapOptionalEquivalence) TypeMirror(javax.lang.model.type.TypeMirror) AutoValue(com.google.auto.value.AutoValue) Qualifier(javax.inject.Qualifier) Optional(java.util.Optional) MoreElements.isAnnotationPresent(com.google.auto.common.MoreElements.isAnnotationPresent) AnnotationMirrors(com.google.auto.common.AnnotationMirrors) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

Qualifier (javax.inject.Qualifier)4 Annotation (java.lang.annotation.Annotation)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 Named (javax.inject.Named)2 Provider (javax.inject.Provider)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 AnnotationMirrors (com.google.auto.common.AnnotationMirrors)1 MoreElements.isAnnotationPresent (com.google.auto.common.MoreElements.isAnnotationPresent)1 MoreTypes (com.google.auto.common.MoreTypes)1 Mirrors.isProvider (com.google.auto.factory.processor.Mirrors.isProvider)1 Mirrors.unwrapOptionalEquivalence (com.google.auto.factory.processor.Mirrors.unwrapOptionalEquivalence)1 Mirrors.wrapOptionalInEquivalence (com.google.auto.factory.processor.Mirrors.wrapOptionalInEquivalence)1 AutoValue (com.google.auto.value.AutoValue)1 Equivalence (com.google.common.base.Equivalence)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Types (javax.lang.model.util.Types)1