Search in sources :

Example 76 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project tiger by google.

the class CoreInjectorGenerator method generateMapTypeProvisionMethodForPackage.

// it is a contributor to map binding.
/**
 * TODO: revisit the logic to handle scoped multi bindings.
 */
private void generateMapTypeProvisionMethodForPackage(final BindingKey key, Set<DependencyInfo> dependencyInfos, String suffix) {
    messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: key " + key + " di " + dependencyInfos);
    Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
    TypeElement scope = scopeCalculator.calculate(key);
    DependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
    TypeName returnType = key.getTypeName();
    ClassName injectorClassName = getPackagedInjectorNameForDependencyInfo(key, dependencyInfo);
    TypeSpec.Builder packagedInjectorSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(utils.getProvisionMethodName(dependencies, key) + suffix);
    methodSpecBuilder.addModifiers(Modifier.PUBLIC).returns(returnType);
    methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
    TypeName mapKeyType = ((ParameterizedTypeName) returnType).typeArguments.get(0);
    TypeName mapValueType = ((ParameterizedTypeName) returnType).typeArguments.get(1);
    BindingKey mapValueKey = BindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        AnnotationMirror mapKeyMirror = utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
            return;
        }
        AnnotationValue mapKey = utils.getAnnotationValue(elements, mapKeyMirror, "value");
        messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: mapKey  " + mapKey);
        methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
        if (utils.isMapWithBuiltinValueType(key)) {
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
        } else {
            addNewStatementToMethodSpec(scope, di, injectorClassName, methodSpecBuilder, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
    methodSpecBuilder.addStatement("return result");
    packagedInjectorSpecBuilder.addMethod(methodSpecBuilder.build());
// messager.printMessage(Kind.NOTE, String.format(
// "generateSetTypeProvisionMethodFromModule: \n key: %s, \n injector: %s, \n method: %s.",
// key, injectorClassName, methodSpecBuilder.build()));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) AnnotationValue(javax.lang.model.element.AnnotationValue) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 77 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project tiger by google.

the class GeneralInjectorGenerator4 method generateMapContributors.

protected final void generateMapContributors(BindingKey key, ParameterizedTypeName returnType, MethodSpec.Builder methodSpecBuilder) {
    Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
    // TODO: remove this hack
    if (dependencyInfos == null) {
        dependencyInfos = new HashSet<>();
        logger.w("no dI for key: " + key);
    }
    Preconditions.checkNotNull(dependencyInfos, String.format("dependencyInfo not found for key: %s", key));
    TypeName mapKeyType = returnType.typeArguments.get(0);
    TypeName mapValueType = returnType.typeArguments.get(1);
    BindingKey mapValueKey = BindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (DependencyInfo di : dependencyInfos) {
        if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
            continue;
        }
        AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            logger.e("unwrapValue = false not supported yet. Consider using set binding.");
            return;
        }
        AnnotationValue mapKey = Utils.getAnnotationValue(elements, mapKeyMirror, "value");
        logger.l(Kind.NOTE, "mapKey: %s", mapKey.toString());
        methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
        if (utils.isMapWithBuiltinValueType(key)) {
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(mapValueKey, di));
        } else {
            addNewStatementToMethodSpec(methodSpecBuilder, di, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 78 with AnnotationValue

use of javax.lang.model.element.AnnotationValue 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 79 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project tiger by google.

the class NewInjectorGenerator method generateMapTypeProvisionMethodForPackage.

// If dependencyInfo is not null, then it is a contributor to map biding.
private void generateMapTypeProvisionMethodForPackage(final NewBindingKey key, Set<NewDependencyInfo> dependencyInfos, String suffix) {
    Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
    NewDependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
    TypeName returnType = key.getTypeName();
    ClassName injectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
    methodSpecBuilder.addModifiers(Modifier.PUBLIC).returns(returnType);
    methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
    TypeName mapKeyType = ((ParameterizedTypeName) returnType).typeArguments.get(0);
    TypeName mapValueType = ((ParameterizedTypeName) returnType).typeArguments.get(1);
    NewBindingKey mapValueKey = NewBindingKey.get(mapValueType);
    methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
    methodSpecBuilder.addStatement("$T mapValue", mapValueType);
    for (NewDependencyInfo di : dependencyInfos) {
        AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
        AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
        if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
            errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
            return;
        }
        Object mapKey = Utils.getAnnotationValue(mapKeyMirror, "value").getValue();
        methodSpecBuilder.addStatement("mapKey = ($T) $S", mapKeyType, mapKey);
        if (Utils.isMapWithBuiltinValueType(key)) {
            TypeElement scope = scopeCalculator.calculate(key);
            methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
        } else {
            addNewStatementToMethodSpec(mapValueKey, di, injectorClassName, methodSpecBuilder, "mapValue");
        }
        methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
    }
    methodSpecBuilder.addStatement("return result");
    componentSpecBuilder.addMethod(methodSpecBuilder.build());
// messager.printMessage(Kind.NOTE, String.format(
// "generateSetTypeProvisionMethodFromModule: \n key: %s, \n injector: %s, \n method: %s.",
// key, injectorClassName, methodSpecBuilder.build()));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) AnnotationValue(javax.lang.model.element.AnnotationValue) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 80 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project tiger by google.

the class TigerDaggerGeneratorProcessor method getScopeForComponent.

/**
 * Returns scope for the give dagger Component, null is unscoped. The result is either
 * explicitly specified or implicitly inherited from one of the ancestors.
 */
@Nullable
private TypeElement getScopeForComponent(TypeElement component, Messager messager) {
    DeclaredType scope = utils.getScopeType(component, scopeAliasCondenser);
    if (scope != null) {
        return (TypeElement) scope.asElement();
    }
    AnnotationMirror componentAnnotationMirror = utils.getAnnotationMirror(component, Component.class);
    List<AnnotationValue> dependencies = componentAnnotationMirror == null ? null : (List<AnnotationValue>) utils.getAnnotationValue(elements, componentAnnotationMirror, "dependencies");
    if (dependencies == null) {
        return null;
    }
    Set<TypeElement> parentScopes = new HashSet<>();
    for (AnnotationValue dependency : dependencies) {
        DeclaredType dependencyClass = (DeclaredType) dependency.getValue();
        TypeElement parentScope = getScopeForComponent((TypeElement) dependencyClass.asElement(), messager);
        if (parentScope != null) {
            parentScopes.add(parentScope);
        }
    }
    if (parentScopes.isEmpty()) {
        return null;
    }
    if (parentScopes.size() > 1) {
        messager.printMessage(Kind.ERROR, String.format("Component %s depends on more than one scoped components. The scopes are: %s", component, parentScopes));
    }
    return Iterables.getOnlyElement(parentScopes);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) DeclaredType(javax.lang.model.type.DeclaredType) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)182 AnnotationMirror (javax.lang.model.element.AnnotationMirror)81 TypeElement (javax.lang.model.element.TypeElement)62 ArrayList (java.util.ArrayList)54 ExecutableElement (javax.lang.model.element.ExecutableElement)53 TypeMirror (javax.lang.model.type.TypeMirror)48 List (java.util.List)45 Test (org.junit.Test)30 DeclaredType (javax.lang.model.type.DeclaredType)27 Map (java.util.Map)20 HashSet (java.util.HashSet)19 HashMap (java.util.HashMap)18 VariableElement (javax.lang.model.element.VariableElement)15 Element (javax.lang.model.element.Element)14 ElementUtils.getAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.getAnnotationValue)10 ElementUtils.fromTypeMirror (com.oracle.truffle.dsl.processor.java.ElementUtils.fromTypeMirror)9 CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)8 ElementUtils.resolveAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.resolveAnnotationValue)7 ElementUtils.unboxAnnotationValue (com.oracle.truffle.dsl.processor.java.ElementUtils.unboxAnnotationValue)7 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)7