Search in sources :

Example 41 with AnnotationMirror

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

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

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

the class Utils method findAllModulesRecursively.

@SuppressWarnings("unchecked")
public static Set<TypeElement> findAllModulesRecursively(TypeElement inModule) {
    Set<TypeElement> result = new HashSet<>();
    result.add(inModule);
    for (AnnotationMirror annotationMirror : inModule.getAnnotationMirrors()) {
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
            ExecutableElement key = entry.getKey();
            /** Checks for {@link Module.includes}. */
            if (key.getSimpleName().contentEquals("includes")) {
                for (AnnotationValue annotationValue : (List<AnnotationValue>) entry.getValue().getValue()) {
                    TypeElement childModule = (TypeElement) ((DeclaredType) annotationValue.getValue()).asElement();
                    result.addAll(findAllModulesRecursively(childModule));
                }
            }
        }
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) IntoMap(dagger.multibindings.IntoMap) HashSet(java.util.HashSet)

Example 44 with AnnotationMirror

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

the class ComponentGeneratorProcessor method getScopeTree.

/**
   * Creates a map from child to parent from the give {@link TypeElement}s, which are annotated with
   * {@link ScopeDependency}.
   */
private Map<TypeElement, TypeElement> getScopeTree(Set<TypeElement> scopeDependencies) {
    Map<TypeElement, TypeElement> result = new HashMap<>();
    for (TypeElement element : scopeDependencies) {
        AnnotationMirror annotationMirror = Preconditions.checkNotNull(Utils.getAnnotationMirror(element, ScopeDependency.class), String.format("Did not find @ScopeDependency on %s", element));
        TypeElement scope = (TypeElement) ((DeclaredType) Utils.getAnnotationValue(annotationMirror, "scope").getValue()).asElement();
        TypeElement parent = (TypeElement) ((DeclaredType) Utils.getAnnotationValue(annotationMirror, "parent").getValue()).asElement();
        Preconditions.checkState(result.put(scope, parent) == null, String.format("Duplicate ScopeDependencies found for %s. All dependencies are: %s", scope, scopeDependencies));
    }
    verifyScopeTree(result);
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement)

Example 45 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project squidb by yahoo.

the class ImplementsPlugin method parseInterfaces.

private void parseInterfaces() {
    TypeElement modelSpecElement = modelSpec.getModelSpecElement();
    if (modelSpecElement.getAnnotation(Implements.class) != null) {
        List<DeclaredTypeName> typeNames = utils.getTypeNamesFromAnnotationValue(utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceClasses"));
        if (!AptUtils.isEmpty(typeNames)) {
            interfaces.addAll(typeNames);
        }
        AnnotationValue value = utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceDefinitions");
        List<AnnotationMirror> interfaceSpecs = utils.getValuesFromAnnotationValue(value, AnnotationMirror.class);
        for (AnnotationMirror spec : interfaceSpecs) {
            AnnotationValue interfaceClassValue = utils.getAnnotationValueFromMirror(spec, "interfaceClass");
            List<DeclaredTypeName> interfaceClassList = utils.getTypeNamesFromAnnotationValue(interfaceClassValue);
            if (!AptUtils.isEmpty(interfaceClassList)) {
                DeclaredTypeName interfaceClass = interfaceClassList.get(0);
                AnnotationValue interfaceTypeArgsValue = utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgs");
                List<DeclaredTypeName> typeArgs = utils.getTypeNamesFromAnnotationValue(interfaceTypeArgsValue);
                if (AptUtils.isEmpty(typeArgs)) {
                    List<String> typeArgNames = utils.getValuesFromAnnotationValue(utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgNames"), String.class);
                    for (String typeArgName : typeArgNames) {
                        typeArgs.add(new DeclaredTypeName(typeArgName));
                    }
                }
                interfaceClass.setTypeArgs(typeArgs);
                interfaces.add(interfaceClass);
            }
        }
    }
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Implements(com.yahoo.squidb.annotations.Implements) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)88 TypeElement (javax.lang.model.element.TypeElement)46 AnnotationValue (javax.lang.model.element.AnnotationValue)21 Element (javax.lang.model.element.Element)19 DeclaredType (javax.lang.model.type.DeclaredType)19 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableElement (javax.lang.model.element.VariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)16 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)13 List (java.util.List)12 Map (java.util.Map)10 PackageElement (javax.lang.model.element.PackageElement)7 HashMap (java.util.HashMap)5 Nullable (javax.annotation.Nullable)5 IOException (java.io.IOException)4 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ClassName (com.squareup.javapoet.ClassName)3