Search in sources :

Example 46 with PackageElement

use of javax.lang.model.element.PackageElement in project auto by google.

the class MoreElements method getLocalAndInheritedMethods.

private static ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Overrides overrides) {
    PackageElement pkg = getPackage(type);
    ImmutableSet.Builder<ExecutableElement> methods = ImmutableSet.builder();
    for (ExecutableElement method : getAllMethods(type, overrides)) {
        // Filter out all static and non-visible methods.
        if (!method.getModifiers().contains(STATIC) && methodVisibleFromPackage(method, pkg)) {
            methods.add(method);
        }
    }
    return methods.build();
}
Also used : MoreStreams.toImmutableSet(com.google.auto.common.MoreStreams.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement)

Example 47 with PackageElement

use of javax.lang.model.element.PackageElement in project auto by google.

the class AutoServiceProcessor method getBinaryNameImpl.

private String getBinaryNameImpl(TypeElement element, String className) {
    Element enclosingElement = element.getEnclosingElement();
    if (enclosingElement instanceof PackageElement) {
        PackageElement pkg = MoreElements.asPackage(enclosingElement);
        if (pkg.isUnnamed()) {
            return className;
        }
        return pkg.getQualifiedName() + "." + className;
    }
    TypeElement typeElement = MoreElements.asType(enclosingElement);
    return getBinaryNameImpl(typeElement, typeElement.getSimpleName() + "$" + className);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement)

Example 48 with PackageElement

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

the class NewInjectorGenerator method generateProvisionMethodForSet.

private void generateProvisionMethodForSet(NewBindingKey key, TypeElement referencingClass, String suffix) {
    TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
    // messager.printMessage(Kind.NOTE, "generateProvisionMethodForSet: " + key +
    // " PackagedInjector: "
    // + getInjectorFor(key, referencingClass) + " SpecBuilder: " + componentSpecBuilder);
    ParameterizedTypeName type = (ParameterizedTypeName) key.getTypeName();
    Preconditions.checkArgument(type.rawType.equals(ClassName.get(Set.class)));
    TypeName elementType = Iterables.getOnlyElement(type.typeArguments);
    MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(type);
    methodSpecBuilder.addStatement("$T result = new $T<>()", type, HashSet.class);
    methodSpecBuilder.addStatement("$T setVar", type);
    methodSpecBuilder.addStatement("$T elementVar", elementType);
    SetMultimap<PackageElement, NewDependencyInfo> packageToDependencyInfoMap = HashMultimap.create();
    for (NewDependencyInfo dependencyInfo : dependencies.get(key)) {
        packageToDependencyInfoMap.put(Utils.getPackage(dependencyInfo.getSourceClassElement()), dependencyInfo);
    }
    for (PackageElement pkg : packageToDependencyInfoMap.keySet()) {
        // messager.printMessage(Kind.NOTE, String.format("generateProvisionMethodForSet for %s from"
        // +
        // " %s", key, packageToDependencyInfoMap.get(pkg)));
        generateSetTypeProvisionMethodForPackage(key, packageToDependencyInfoMap.get(pkg), suffix);
        NewDependencyInfo dependencyInfo = Iterables.getFirst(packageToDependencyInfoMap.get(pkg), null);
        Preconditions.checkNotNull(dependencyInfo, String.format("no dependencyInfo for set key %s in module %s", key, pkg));
        ClassName packagedInjectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
        methodSpecBuilder.addStatement("setVar = $L.$N().$N()", TOP_LEVEL_INJECTOR_FIELD, Utils.getGetMethodName(packagedInjectorClassName), getProvisionMethodName(key));
        methodSpecBuilder.addStatement("result.addAll($L)", "setVar");
    }
    methodSpecBuilder.addStatement("return result");
    componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) PackageElement(javax.lang.model.element.PackageElement) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 49 with PackageElement

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

the class Utils method getPackage.

public PackageElement getPackage(Element element) {
    Preconditions.checkArgument(Sets.newHashSet(ElementKind.CLASS, ElementKind.METHOD, ElementKind.CONSTRUCTOR, ElementKind.FIELD, ElementKind.INTERFACE).contains(element.getKind()), "Unexpected element: " + element + " with kind: " + element.getKind());
    Element result = element.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, element));
        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 50 with PackageElement

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

the class ProxyGenerator method collectModulesAndInjected.

private void collectModulesAndInjected(PackageElement packageElement, Set<TypeElement> modules, Set<TypeElement> classesWithInject) {
    Set<TypeElement> ctorInjectedClasses = new HashSet<>();
    Set<TypeElement> injectedClasses = new HashSet<>();
    for (Element e : packageElement.getEnclosedElements()) {
        if (e instanceof TypeElement) {
            PackagedInjectorGenerator.collectFromClassOrInterface((TypeElement) e, modules, ctorInjectedClasses, injectedClasses, utils, env, logger);
        }
    }
    classesWithInject.addAll(ctorInjectedClasses);
    classesWithInject.addAll(injectedClasses);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) HashSet(java.util.HashSet)

Aggregations

PackageElement (javax.lang.model.element.PackageElement)134 TypeElement (javax.lang.model.element.TypeElement)93 Element (javax.lang.model.element.Element)52 ExecutableElement (javax.lang.model.element.ExecutableElement)34 VariableElement (javax.lang.model.element.VariableElement)32 TypeSpec (com.squareup.javapoet.TypeSpec)20 IOException (java.io.IOException)17 HashSet (java.util.HashSet)13 TypeMirror (javax.lang.model.type.TypeMirror)13 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 JavaFileObject (javax.tools.JavaFileObject)12 ClassName (com.squareup.javapoet.ClassName)11 ArrayList (java.util.ArrayList)11 Set (java.util.Set)10 Elements (javax.lang.model.util.Elements)10 PrintWriter (java.io.PrintWriter)9 HashMap (java.util.HashMap)9 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)8 Map (java.util.Map)8 Messager (javax.annotation.processing.Messager)8