Search in sources :

Example 31 with AnnotationValue

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

the class TigerDaggerGeneratorProcessor method getComponentDependencies.

/**
   * Returns dependency components of the give component, empty set if none.
   */
private Set<TypeElement> getComponentDependencies(TypeElement component) {
    Set<TypeElement> result = new HashSet<>();
    AnnotationMirror componentAnnotationMirror = Preconditions.checkNotNull(Utils.getAnnotationMirror(component, Component.class), String.format("@Component not found for %s", component));
    AnnotationValue dependenciesAnnotationValue = Utils.getAnnotationValue(componentAnnotationMirror, "dependencies");
    if (dependenciesAnnotationValue == null) {
        return result;
    }
    List<? extends AnnotationValue> dependencies = (List<? extends AnnotationValue>) dependenciesAnnotationValue.getValue();
    for (AnnotationValue dependency : dependencies) {
        result.add((TypeElement) ((DeclaredType) dependency.getValue()).asElement());
    }
    return result;
}
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) Component(dagger.Component) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 32 with AnnotationValue

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

the class TigerDaggerGeneratorProcessor method verifyComponents.

/**
   * Not all combination of components are allowed. Here are the limitation.
   * 
   * <pre>
   *  1. All components must be scoped.
   * </pre>
   * 
   * <pre>
   *  2. There is at most one dependencies component. This is automatically fulfilled for subcomponents.
   * </pre>
   * 
   * <pre>
   *  3. Only one root. Forest not supported.
   * </pre>
   * 
   * All exceptions are easy to fix, if any. This map to the core injectors very well. Modules
   * needed by a core injector but not needed by related component(s) can just be passed in as null
   * because it will never be used.
   */
private void verifyComponents(Set<TypeElement> components) {
    TypeElement root = null;
    for (TypeElement component : components) {
        Preconditions.checkNotNull(Utils.getScopeType(component), String.format("Unscoped component supported : %s", component));
        AnnotationMirror annotationMirror = Utils.getAnnotationMirror(component, Component.class);
        List<AnnotationValue> dependencies = null;
        AnnotationValue annotationValue = Utils.getAnnotationValue(annotationMirror, "dependencies");
        if (annotationValue != null) {
            dependencies = (List<AnnotationValue>) annotationValue.getValue();
        }
        if (dependencies == null || dependencies.isEmpty()) {
            Preconditions.checkState(root == null, String.format("More than one root components found: %s and %s", root, component));
            root = component;
        } else {
            Preconditions.checkState(dependencies.size() == 1, String.format("More than one dependencies found for component: %s", component));
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 33 with AnnotationValue

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

the class TigerDaggerGeneratorProcessor method getAllModulesOfComponentRecursively.

private Set<TypeElement> getAllModulesOfComponentRecursively(TypeElement component) {
    Set<TypeElement> result = new HashSet<>();
    AnnotationMirror componentAnnotationMirror = Utils.getAnnotationMirror(component, Component.class);
    for (AnnotationValue annotationValue : (List<AnnotationValue>) Utils.getAnnotationValue(componentAnnotationMirror, "modules").getValue()) {
        result.add((TypeElement) ((DeclaredType) annotationValue.getValue()).asElement());
    }
    result = Utils.findAllModulesRecursively(result);
    return result;
}
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) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 34 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project bazel by bazelbuild.

the class AnnotationUtils method getElementValue.

/**
     * Get the attribute with the name {@code name} of the annotation
     * {@code anno}. The result is expected to have type {@code expectedType}.
     *
     * <p>
     * <em>Note 1</em>: The method does not work well for attributes of an array
     * type (as it would return a list of {@link AnnotationValue}s). Use
     * {@code getElementValueArray} instead.
     *
     * <p>
     * <em>Note 2</em>: The method does not work for attributes of an enum type,
     * as the AnnotationValue is a VarSymbol and would be cast to the enum type,
     * which doesn't work. Use {@code getElementValueEnum} instead.
     *
     *
     * @param anno the annotation to disassemble
     * @param name the name of the attribute to access
     * @param expectedType the expected type used to cast the return type
     * @param useDefaults whether to apply default values to the attribute.
     * @return the value of the attribute with the given name
     */
public static <T> T getElementValue(AnnotationMirror anno, CharSequence name, Class<T> expectedType, boolean useDefaults) {
    Map<? extends ExecutableElement, ? extends AnnotationValue> valmap;
    if (useDefaults) {
        valmap = getElementValuesWithDefaults(anno);
    } else {
        valmap = anno.getElementValues();
    }
    for (ExecutableElement elem : valmap.keySet()) {
        if (elem.getSimpleName().contentEquals(name)) {
            AnnotationValue val = valmap.get(elem);
            return expectedType.cast(val.getValue());
        }
    }
    ErrorReporter.errorAbort("No element with name \'" + name + "\' in annotation " + anno);
    // dead code
    return null;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 35 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project glide by bumptech.

the class RootModuleGenerator method getExcludedGlideModuleClassNames.

private static Set<String> getExcludedGlideModuleClassNames(ProcessingEnvironment processingEnv, String rootGlideModuleClassName) {
    TypeElement rootGlideModule = processingEnv.getElementUtils().getTypeElement(rootGlideModuleClassName);
    String excludedModulesName = Excludes.class.getName();
    AnnotationValue excludedModuleAnnotationValue = null;
    for (AnnotationMirror annotationMirror : rootGlideModule.getAnnotationMirrors()) {
        // instead. This check is necessary because a given class may have multiple Annotations.
        if (!excludedModulesName.equals(annotationMirror.getAnnotationType().toString())) {
            continue;
        }
        Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values = annotationMirror.getElementValues().entrySet();
        // (usually value).
        if (values.size() != 1) {
            throw new IllegalArgumentException("Expected single value, but found: " + values);
        }
        excludedModuleAnnotationValue = values.iterator().next().getValue();
        if (excludedModuleAnnotationValue == null) {
            throw new NullPointerException("Failed to find Excludes#value");
        }
    }
    // If the RootGlideModule class is not annotated, then there are no classes to exclude.
    if (excludedModuleAnnotationValue == null) {
        return Collections.emptySet();
    }
    List values = (List) excludedModuleAnnotationValue.getValue();
    Set<String> result = new HashSet<>(values.size());
    for (Object value : values) {
        Attribute.Class clazz = (Attribute.Class) value;
        result.add(clazz.getValue().toString());
    }
    return result;
}
Also used : Attribute(com.sun.tools.javac.code.Attribute) TypeElement(javax.lang.model.element.TypeElement) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) List(com.sun.tools.javac.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)43 AnnotationMirror (javax.lang.model.element.AnnotationMirror)23 TypeElement (javax.lang.model.element.TypeElement)20 List (java.util.List)16 ArrayList (java.util.ArrayList)13 ExecutableElement (javax.lang.model.element.ExecutableElement)12 TypeMirror (javax.lang.model.type.TypeMirror)11 HashSet (java.util.HashSet)8 Map (java.util.Map)8 DeclaredType (javax.lang.model.type.DeclaredType)8 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (javax.lang.model.element.Element)3 ImmutableList (com.google.common.collect.ImmutableList)2 ClassName (com.squareup.javapoet.ClassName)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 HashMap (java.util.HashMap)2 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1