Search in sources :

Example 71 with TypeElement

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

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

the class Utils method getPackage.

public static PackageElement getPackage(TypeElement typeElement) {
    Element result = typeElement.getEnclosingElement();
    ElementKind elementKind = result.getKind();
    while (!elementKind.equals(ElementKind.PACKAGE)) {
        Preconditions.checkState(elementKind.isClass() || elementKind.isInterface(), String.format("Utils.getPackage: unexpected kind: %s for type: %s", elementKind, typeElement));
        result = result.getEnclosingElement();
        elementKind = result.getKind();
    }
    return (PackageElement) result;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement)

Example 73 with TypeElement

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

the class TigerDaggerGeneratorProcessor method generateComponentBuilder.

private void generateComponentBuilder(ClassName componentClassName, @Nullable TypeElement dependencyComponent, List<TypeElement> sortedPassedModules, TypeSpec.Builder componentBuilder, ComponentInfo coreInjector) {
    TypeSpec.Builder builderBuilder = TypeSpec.classBuilder("Builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    // set parent inject methods.
    ClassName dependencyClassName = null;
    if (dependencyComponent != null) {
        dependencyClassName = (ClassName) ClassName.get(dependencyComponent.asType());
        Utils.addSetMethod(componentClassName, builderBuilder, dependencyClassName);
    }
    /**
     * Set module methods.
     */
    for (TypeElement m : sortedPassedModules) {
        Utils.addSetMethod(componentClassName, builderBuilder, (ClassName) ClassName.get(m.asType()));
    }
    // build() method.
    MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).returns(componentClassName);
    StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
    if (dependencyClassName != null) {
        returnCodeBuilder.append(Utils.getSourceCodeName(dependencyComponent));
        if (!sortedPassedModules.isEmpty()) {
            returnCodeBuilder.append(", ");
        }
    }
    if (!sortedPassedModules.isEmpty()) {
        for (TypeElement module : sortedPassedModules) {
            String moduleFiledName = Utils.getSourceCodeName(module);
            returnCodeBuilder.append(moduleFiledName).append(", ");
        }
        returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
    }
    returnCodeBuilder.append(");");
    buildMethodBuilder.addCode(returnCodeBuilder.toString(), componentClassName);
    builderBuilder.addMethod(buildMethodBuilder.build());
    componentBuilder.addType(builderBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 74 with TypeElement

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

use of javax.lang.model.element.TypeElement 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

TypeElement (javax.lang.model.element.TypeElement)1562 ExecutableElement (javax.lang.model.element.ExecutableElement)517 Element (javax.lang.model.element.Element)502 TypeMirror (javax.lang.model.type.TypeMirror)420 VariableElement (javax.lang.model.element.VariableElement)333 DeclaredType (javax.lang.model.type.DeclaredType)201 ArrayList (java.util.ArrayList)177 PackageElement (javax.lang.model.element.PackageElement)159 AnnotationMirror (javax.lang.model.element.AnnotationMirror)151 Test (org.junit.Test)136 HashMap (java.util.HashMap)118 HashSet (java.util.HashSet)109 Elements (javax.lang.model.util.Elements)101 Map (java.util.Map)98 IOException (java.io.IOException)97 List (java.util.List)97 ClassName (com.squareup.javapoet.ClassName)88 Test (org.junit.jupiter.api.Test)80 MethodSpec (com.squareup.javapoet.MethodSpec)72 TypeSpec (com.squareup.javapoet.TypeSpec)63