Search in sources :

Example 76 with PackageElement

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

the class ElementUtil method areParametersNonnullByDefault.

public boolean areParametersNonnullByDefault(TypeElement typeElement, Options options) {
    if (ElementUtil.hasAnnotation(typeElement, ParametersAreNonnullByDefault.class)) {
        return true;
    }
    PackageElement pkg = getPackage(typeElement);
    String pkgName = pkg.getQualifiedName().toString();
    return options.getPackageInfoLookup().hasParametersAreNonnullByDefault(pkgName);
}
Also used : PackageElement(javax.lang.model.element.PackageElement)

Example 77 with PackageElement

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

the class ModuleAnnotationProcessor method process.

/**
   * Each round we do the following:
   * <ol>
   *   <li>Find all RootGlideModules and save them to an instance variable (throw if > 1).
   *   <li>Find all ChildGlideModules
   *   <li>For each ChildGlideModule, write an Indexer with an Annotation with the class name.
   *   <li>If we wrote any Indexers, return and wait for the next round.
   *   <li>If we didn't write any Indexers and there is a RootGlideModule, write the
   *   GeneratedRootGlideModule. Once the GeneratedRootGlideModule is written, we expect to be
   *   finished. Any further generation of related classes will result in errors.
   * </ol>
   */
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
    round++;
    // Order matters here, if we find an Indexer below, we return before writing the root module. If
    // we fail to add to rootModules before then, we might accidentally skip a valid RootModule.
    List<TypeElement> childGlideModules = new ArrayList<>();
    for (TypeElement element : getElementsFor(GlideModule.class, env)) {
        if (processingEnv.getTypeUtils().isAssignable(element.asType(), rootGlideModuleType.asType())) {
            rootGlideModules.add(element);
        } else if (processingEnv.getTypeUtils().isAssignable(element.asType(), childGlideModuleType.asType())) {
            childGlideModules.add(element);
        } else {
            throw new IllegalStateException("@GlideModule can only be applied to ChildGlideModule" + " and RootGlideModule implementations, not: " + element);
        }
    }
    debugLog("got child modules: " + childGlideModules);
    debugLog("got root modules: " + rootGlideModules);
    if (rootGlideModules.size() > 1) {
        throw new IllegalStateException("You cannot have more than one RootGlideModule, found: " + rootGlideModules);
    }
    if (!childGlideModules.isEmpty()) {
        if (isGeneratedRootGlideModuleWritten) {
            throw new IllegalStateException("Cannot process ChildModules after writing RootModules: " + childGlideModules);
        }
        TypeSpec indexer = GlideIndexerGenerator.generate(childGlideModules);
        writeIndexer(indexer);
        debugLog("Wrote an Indexer this round, skipping the root module to ensure all indexers are" + " found");
        // and we can safely wait to write our RootModule until then.
        return true;
    }
    // most once.
    if (isGeneratedRootGlideModuleWritten || rootGlideModules.isEmpty()) {
        return false;
    }
    debugLog("Processing root module: " + rootGlideModules.iterator().next());
    // If this package is null, it means there are no classes with this package name. One way this
    // could happen is if we process an annotation and reach this point without writing something
    // to the package. We do not error check here because that shouldn't happen with the
    // current implementation.
    PackageElement glideGenPackage = processingEnv.getElementUtils().getPackageElement(COMPILER_PACKAGE_NAME);
    glideModuleClassNames.addAll(getGlideModuleClassNames(glideGenPackage));
    TypeSpec generatedRootGlideModule = RootModuleGenerator.generate(processingEnv, rootGlideModules.get(0).getQualifiedName().toString(), glideModuleClassNames);
    writeRootModule(generatedRootGlideModule);
    isGeneratedRootGlideModuleWritten = true;
    infoLog("Wrote GeneratedRootGlideModule with: " + glideModuleClassNames);
    return true;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) PackageElement(javax.lang.model.element.PackageElement) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 78 with PackageElement

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

the class ModuleAnnotationProcessor method getGlideModuleClassNames.

@SuppressWarnings("unchecked")
private Set<String> getGlideModuleClassNames(PackageElement glideGenPackage) {
    Set<String> glideModules = new HashSet<>();
    List<? extends Element> glideGeneratedElements = glideGenPackage.getEnclosedElements();
    for (Element indexer : glideGeneratedElements) {
        ModuleIndex annotation = indexer.getAnnotation(ModuleIndex.class);
        // that we can safely ignore.
        if (annotation != null) {
            Collections.addAll(glideModules, annotation.glideModules());
        }
    }
    debugLog("Found GlideModules: " + glideModules);
    return glideModules;
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 79 with PackageElement

use of javax.lang.model.element.PackageElement in project buck by facebook.

the class TreeBackedElements method enterElement.

public TreeBackedElement enterElement(Element underlyingElement) {
    TreeBackedElement result = treeBackedElements.get(underlyingElement);
    if (result != null) {
        return result;
    }
    ElementKind kind = underlyingElement.getKind();
    switch(kind) {
        case PACKAGE:
            result = newTreeBackedPackage((PackageElement) underlyingElement);
            break;
        case ANNOTATION_TYPE:
        case CLASS:
        case ENUM:
        case INTERFACE:
            result = newTreeBackedType((TypeElement) underlyingElement);
            break;
        case TYPE_PARAMETER:
            result = newTreeBackedTypeParameter((TypeParameterElement) underlyingElement);
            break;
        case ENUM_CONSTANT:
        case FIELD:
        case PARAMETER:
            result = newTreeBackedVariable((VariableElement) underlyingElement);
            break;
        case CONSTRUCTOR:
        case METHOD:
            result = newTreeBackedExecutable((ExecutableElement) underlyingElement);
            break;
        // $CASES-OMITTED$
        default:
            throw new UnsupportedOperationException(String.format("Element kind %s NYI", kind));
    }
    treeBackedElements.put(underlyingElement, result);
    return result;
}
Also used : ElementKind(javax.lang.model.element.ElementKind) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement)

Example 80 with PackageElement

use of javax.lang.model.element.PackageElement in project buck by facebook.

the class TreeBackedPackageElementTest method testAccept.

@Test
public void testAccept() throws IOException {
    initCompiler();
    PackageElement expectedPackage = elements.getPackageElement("java.lang");
    Object expectedResult = new Object();
    Object actualResult = expectedPackage.accept(new SimpleElementVisitor8<Object, Object>() {

        @Override
        protected Object defaultAction(Element e, Object o) {
            return null;
        }

        @Override
        public Object visitPackage(PackageElement actualPackage, Object o) {
            assertSame(expectedPackage, actualPackage);
            return o;
        }
    }, expectedResult);
    assertSame(expectedResult, actualResult);
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Test(org.junit.Test)

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