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);
}
}
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);
}
}
}
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);
}
}
}
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));
}
Aggregations