Search in sources :

Example 81 with AnnotationValue

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

the class Utils method findDirectModules.

public void findDirectModules(Set<TypeElement> modules, Element component) {
    AnnotationMirror componentAnnotationMirror = isComponent(component) ? getAnnotationMirror(component, Component.class) : getAnnotationMirror(component, Subcomponent.class);
    AnnotationValue moduleAnnotationValue = Utils.getAnnotationValue(elements, componentAnnotationMirror, "modules");
    if (moduleAnnotationValue != null) {
        for (AnnotationValue annotationValue : (List<AnnotationValue>) moduleAnnotationValue.getValue()) {
            modules.add((TypeElement) ((DeclaredType) annotationValue.getValue()).asElement());
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Subcomponent(dagger.Subcomponent) AnnotationValue(javax.lang.model.element.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) Component(dagger.Component) DeclaredType(javax.lang.model.type.DeclaredType)

Example 82 with AnnotationValue

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

the class Utils method findAllModulesRecursively.

/**
 * Returns modules included and created by {@link ContributesAndroidInjector}.
 */
@SuppressWarnings("unchecked")
public Set<TypeElement> findAllModulesRecursively(Elements elements, TypeElement module) {
    Set<TypeElement> result = new HashSet<>();
    result.add(module);
    // included
    for (AnnotationMirror annotationMirror : module.getAnnotationMirrors()) {
        // TODO: only handle @Module
        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(elements, childModule));
                }
            // messager.printMessage(Kind.NOTE, TAG + ".findAllModulesRecursively, result " + result);
            }
        }
    }
    /**
     * by {@link ContributesAndroidInjector}
     */
    Set<TypeElement> modulesGenerated = collectModulesByContributesAndroidInjectorInModule(elements, messager, module);
    if (modulesGenerated != null) {
        // trimModules(modulesGenerated);
        result.addAll(modulesGenerated);
    }
    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 83 with AnnotationValue

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

the class Utils method getComponentDependencies.

/**
 * Returns dependencies of the give component, empty set if none.
 */
public Set<TypeElement> getComponentDependencies(TypeElement component) {
    Set<TypeElement> result = new HashSet<>();
    AnnotationMirror componentAnnotationMirror = Preconditions.checkNotNull(getAnnotationMirror(component, Component.class), String.format("@Component not found for %s", component));
    AnnotationValue dependenciesAnnotationValue = getAnnotationValue(elements, componentAnnotationMirror, COMPONANT_ANNOTATION_ELEMENT_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) List(java.util.List) ArrayList(java.util.ArrayList) Component(dagger.Component) HashSet(java.util.HashSet) DeclaredType(javax.lang.model.type.DeclaredType)

Example 84 with AnnotationValue

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

the class Tiger2Processor method getComponentDependencies.

/**
 * Returns dependencies 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(elements, componentAnnotationMirror, COMPONANT_ANNOTATION_ELEMENT_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 85 with AnnotationValue

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

the class Tiger2Processor method verifyComponents.

/**
 * Not all combination of eitherComponents are allowed. Here are the limitation.
 *
 * <pre>
 *  1. All eitherComponents 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, scopeAliasCondenser), String.format("Unscoped component supported : %s", component));
        AnnotationMirror annotationMirror = utils.getAnnotationMirror(component, Component.class);
        List<AnnotationValue> dependencies = null;
        AnnotationValue annotationValue = utils.getAnnotationValue(elements, annotationMirror, COMPONANT_ANNOTATION_ELEMENT_DEPENDENCIES);
        if (annotationValue != null) {
            dependencies = (List<AnnotationValue>) annotationValue.getValue();
        }
        if (dependencies == null || dependencies.isEmpty()) {
            Preconditions.checkState(root == null, String.format("More than one root eitherComponents 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)

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