Search in sources :

Example 36 with AnnotationMirror

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

the class ElementUtilTest method testGetAnnotationValue.

public void testGetAnnotationValue() throws IOException {
    CompilationUnit unit = translateType("Example", "@com.google.j2objc.annotations.ObjectiveCName(\"E\") class Example {}");
    AbstractTypeDeclaration decl = unit.getTypes().get(0);
    TypeElement element = decl.getTypeElement();
    AnnotationMirror annotation = ElementUtil.getAnnotation(element, ObjectiveCName.class);
    Object value = ElementUtil.getAnnotationValue(annotation, "value");
    assertEquals("E", value);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 37 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project error-prone by google.

the class RequiredAnnotationProcessor method validateElement.

private void validateElement(final Element element) {
    TypeMirror requiredAnnotationTypeMirror = processingEnv.getElementUtils().getTypeElement(RequiredAnnotation.class.getName()).asType();
    for (final AnnotationMirror annotation : processingEnv.getElementUtils().getAllAnnotationMirrors(element)) {
        AnnotationMirror requiredAnnotationMirror = getAnnotationMirror(annotation.getAnnotationType().asElement(), requiredAnnotationTypeMirror);
        if (requiredAnnotationMirror == null) {
            continue;
        }
        AnnotationValue value = getAnnotationValue(requiredAnnotationMirror, "value");
        if (value == null) {
            continue;
        }
        new SimpleAnnotationValueVisitor7<Void, Void>() {

            @Override
            public Void visitType(TypeMirror t, Void p) {
                if (getAnnotationMirror(element, t) == null) {
                    printError(element, annotation, "Annotation %s on %s also requires %s", annotation, element, t);
                }
                return null;
            }

            @Override
            public Void visitArray(List<? extends AnnotationValue> vals, Void p) {
                for (AnnotationValue val : vals) {
                    visit(val);
                }
                return null;
            }
        }.visit(value);
    }
    validateElements(element.getEnclosedElements());
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 38 with AnnotationMirror

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

the class TigerDaggerGeneratorProcessor method getMembersInjectorScope.

/**
   * Returns the {@link DeclaredType} of the scope class of the
   * {@link MembersInjector} specified.
   */
private DeclaredType getMembersInjectorScope(DeclaredType membersInjectorType) {
    ExecutableElement scopeElement = null;
    TypeElement membersInjectorTypeElement = elementUtils.getTypeElement(MembersInjector.class.getCanonicalName());
    for (Element element : membersInjectorTypeElement.getEnclosedElements()) {
        if (element.getSimpleName().contentEquals("scope")) {
            scopeElement = (ExecutableElement) element;
        }
    }
    Preconditions.checkNotNull(scopeElement);
    for (AnnotationMirror annotationMirror : membersInjectorType.asElement().getAnnotationMirrors()) {
        if (annotationMirror.getAnnotationType().asElement().equals(elementUtils.getTypeElement(MembersInjector.class.getCanonicalName()))) {
            return (DeclaredType) annotationMirror.getElementValues().get(scopeElement).getValue();
        }
    }
    throw new RuntimeException(String.format("Scope not found for MembersInjector: %s", membersInjectorType));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 39 with AnnotationMirror

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

the class TigerDaggerGeneratorProcessor method getModulesInComponents.

private void getModulesInComponents(Collection<? extends Element> components, SetMultimap<ComponentInfo, TypeElement> scopeModules, Set<TypeElement> unscopedModules) {
    Set<TypeElement> modules = new HashSet<>();
    for (Element component : components) {
        AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
        for (AnnotationValue annotationValue : (List<AnnotationValue>) Utils.getAnnotationValue(componentAnnotationMirror, "modules").getValue()) {
            modules.add((TypeElement) ((DeclaredType) annotationValue.getValue()).asElement());
        }
    }
    modules = Utils.findAllModulesRecursively(modules);
    for (TypeElement module : modules) {
        TypeElement scopeType = Utils.getModuleScope((DeclaredType) module.asType());
        if (scopeType != null) {
            scopeModules.put(new ComponentInfo(scopeType), module);
        } else {
            unscopedModules.add(module);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 40 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror 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) {
    DeclaredType scope = Utils.getScopeType(component);
    if (scope != null) {
        return (TypeElement) scope.asElement();
    }
    AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
    List<AnnotationValue> dependencies = (List<AnnotationValue>) Utils.getAnnotationValue(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());
        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) ArrayList(java.util.ArrayList) List(java.util.List) DeclaredType(javax.lang.model.type.DeclaredType) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

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