use of dagger.MapKey in project tiger by google.
the class NewDependencyCollector method collectFromModule.
/**
* Collects dependencies from a given {@link dagger.Module}. Type.SET and
* Type.SET_VALUES are put together with Key.get(Set<elementType>, annotation)
* for easier later processing.
*/
private Collection<NewDependencyInfo> collectFromModule(TypeElement module) {
Collection<NewDependencyInfo> result = new HashSet<>();
for (Element e : module.getEnclosedElements()) {
if (!Utils.isProvidesMethod(e, env)) {
continue;
}
ExecutableElement executableElement = (ExecutableElement) e;
TypeMirror returnType = executableElement.getReturnType();
TypeKind returnTypeKind = returnType.getKind();
Preconditions.checkState(returnTypeKind.isPrimitive() || returnTypeKind.equals(TypeKind.DECLARED) || returnTypeKind.equals(TypeKind.ARRAY), String.format("Unexpected type %s from method %s in module %s.", returnTypeKind, executableElement, module));
if (!Utils.isBindableType(returnType)) {
errors.add(String.format("Unbindable type found: %s from module %s by method %s", returnType, module, executableElement));
}
AnnotationMirror annotation = Utils.getQualifier(executableElement);
NewBindingKey key = NewBindingKey.get(returnType, annotation);
List<NewBindingKey> keys = Utils.getDependenciesFromExecutableElement(executableElement);
Provides.Type provideType = Utils.getProvidesType(executableElement);
if (Provides.Type.SET.equals(provideType)) {
key = NewBindingKey.get(ParameterizedTypeName.get(ClassName.get(Set.class), key.getTypeName()), annotation);
} else if (Provides.Type.MAP.equals(provideType)) {
AnnotationMirror mapKeyedMirror = Preconditions.checkNotNull(Utils.getMapKey(executableElement), String.format("Map binding %s missed MapKey.", executableElement));
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirror(mapKeyedMirror.getAnnotationType().asElement(), MapKey.class);
AnnotationValue unwrapValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
if (unwrapValue != null && !((Boolean) unwrapValue.getValue())) {
messager.printMessage(Kind.ERROR, String.format("MapKey with unwrapValue false is not supported, yet. Biding: %s", executableElement));
}
TypeMirror keyTypeMirror = Preconditions.checkNotNull(Utils.getElementTypeMirror(mapKeyedMirror, "value"), String.format("Get key type failed for binding %s", executableElement));
TypeMirror valueTypeMirror = executableElement.getReturnType();
AnnotationMirror qualifier = Utils.getQualifier(executableElement);
key = NewBindingKey.get(ParameterizedTypeName.get(ClassName.get(Map.class), TypeName.get(keyTypeMirror), TypeName.get(valueTypeMirror)), qualifier);
}
NewDependencyInfo newDependencyInfo = new NewDependencyInfo(key, Sets.newHashSet(keys), module, executableElement, provideType);
result.add(newDependencyInfo);
}
// messager.printMessage(Kind.NOTE, String.format("collectFromModule: result: %s", result));
return result;
}
use of dagger.MapKey in project guice by google.
the class DaggerMethodScanner method mapKeyData.
private static <K> MapKeyData<K> mapKeyData(Method method) {
Optional<Annotation> mapKeyOpt = getAnnotatedAnnotation(method, MapKey.class);
checkState(mapKeyOpt.isPresent(), "Missing @MapKey annotation on method %s (make sure the annotation has RUNTIME rentention)", method);
Annotation mapKey = mapKeyOpt.get();
MapKey mapKeyDefinition = mapKey.annotationType().getAnnotation(MapKey.class);
if (!mapKeyDefinition.unwrapValue()) {
return MapKeyData.create(TypeLiteral.get(mapKey.annotationType()), mapKey);
}
Method mapKeyValueMethod = getOnlyElement(Arrays.asList(mapKey.annotationType().getDeclaredMethods()));
Object mapKeyValue;
try {
mapKeyValue = mapKeyValueMethod.invoke(mapKey);
} catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException("Cannot extract map key value", e);
}
return MapKeyData.create(TypeLiteral.get(mapKeyValueMethod.getGenericReturnType()), mapKeyValue);
}
use of dagger.MapKey in project tiger by google.
the class DependencyCollector method getDependencyInfoForMethod.
@Nullable
public DependencyInfo getDependencyInfoForMethod(ExecutableElement method, DependencySourceType dependencySourceType, @Nullable TypeElement source, @Nullable CoreInjectorInfo coreInjectorInfo) {
BindingKey key = getBindingKeyForMethod(method);
if (key == null) {
return null;
}
// logger.n(TAG + ".getDependencyInfoForMethod: method " + method);
AnnotationMirror annotation = utils.getQualifier(method);
List<BindingKey> keys = utils.getDependenciesFromExecutableElement(method);
// Could be from module or other source like component dependencies.
ProvisionType provideType = utils.isProvisionMethodInModule(method) ? utils.getProvisionType(method) : UNIQUE;
if (SET.equals(provideType)) {
key = BindingKey.get(ParameterizedTypeName.get(ClassName.get(Set.class), key.getTypeName()), annotation);
} else if (MAP.equals(provideType) && !utils.isMultibindsMethod(method)) {
AnnotationMirror mapKeyedMirror = Preconditions.checkNotNull(utils.getMapKey(method), String.format("Map binding %s missed MapKey.", method));
AnnotationMirror mapKeyMirror = utils.getAnnotationMirror(mapKeyedMirror.getAnnotationType().asElement(), MapKey.class);
AnnotationValue unwrapValue = utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
if (unwrapValue != null && !((Boolean) unwrapValue.getValue())) {
logger.e(String.format("MapKey with unwrapValue false is not supported, yet. Biding: %s", method));
}
TypeMirror keyTypeMirror = Preconditions.checkNotNull(utils.getElementTypeMirror(mapKeyedMirror, "value"), String.format("Get key type failed for binding %s", method));
if (keyTypeMirror instanceof PrimitiveType) {
keyTypeMirror = types.boxedClass((PrimitiveType) keyTypeMirror).asType();
}
TypeMirror valueTypeMirror = method.getReturnType();
AnnotationMirror qualifier = utils.getQualifier(method);
key = BindingKey.get(ParameterizedTypeName.get(ClassName.get(Map.class), TypeName.get(keyTypeMirror), TypeName.get(valueTypeMirror)), qualifier);
}
DependencyInfo result = new DependencyInfo(dependencySourceType, key, Sets.newHashSet(keys), source != null ? source : (TypeElement) method.getEnclosingElement(), method, provideType, coreInjectorInfo);
return result;
}
Aggregations