Search in sources :

Example 1 with MapKey

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) Provides(dagger.Provides) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MapKey(dagger.MapKey) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) HashSet(java.util.HashSet)

Example 2 with MapKey

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);
}
Also used : MapKey(dagger.MapKey) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Annotations.getAnnotatedAnnotation(com.google.inject.daggeradapter.Annotations.getAnnotatedAnnotation)

Example 3 with MapKey

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;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashSet(java.util.HashSet) Set(java.util.Set) MapKey(dagger.MapKey) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) PrimitiveType(javax.lang.model.type.PrimitiveType) Nullable(javax.annotation.Nullable)

Aggregations

MapKey (dagger.MapKey)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 AnnotationValue (javax.lang.model.element.AnnotationValue)2 TypeElement (javax.lang.model.element.TypeElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Annotations.getAnnotatedAnnotation (com.google.inject.daggeradapter.Annotations.getAnnotatedAnnotation)1 Provides (dagger.Provides)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Nullable (javax.annotation.Nullable)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 PrimitiveType (javax.lang.model.type.PrimitiveType)1 TypeKind (javax.lang.model.type.TypeKind)1