Search in sources :

Example 81 with PackageElement

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

the class TreeBackedPackageElementTest method testUnnamedPackageCanContainStuff.

@Test
public void testUnnamedPackageCanContainStuff() throws IOException {
    compile("class Foo { }");
    PackageElement unnamedPackage = elements.getPackageElement("");
    TypeElement fooElement = elements.getTypeElement("Foo");
    assertPackageContains(unnamedPackage, fooElement);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Test(org.junit.Test)

Example 82 with PackageElement

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

the class TreeBackedPackageElementTest method testUnnamedPackageHasEmptyNames.

@Test
public void testUnnamedPackageHasEmptyNames() throws IOException {
    initCompiler();
    PackageElement unnamedPackage = elements.getPackageElement("");
    Name emptyName = elements.getName("");
    assertTrue(unnamedPackage.isUnnamed());
    assertSame(emptyName, unnamedPackage.getSimpleName());
    assertSame(emptyName, unnamedPackage.getQualifiedName());
}
Also used : PackageElement(javax.lang.model.element.PackageElement) Name(javax.lang.model.element.Name) Test(org.junit.Test)

Example 83 with PackageElement

use of javax.lang.model.element.PackageElement in project camel by apache.

the class AnnotationProcessorHelper method findTypeElementChildren.

public static void findTypeElementChildren(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, Set<TypeElement> found, String superClassName) {
    Elements elementUtils = processingEnv.getElementUtils();
    int idx = superClassName.lastIndexOf('.');
    if (idx > 0) {
        String packageName = superClassName.substring(0, idx);
        PackageElement pe = elementUtils.getPackageElement(packageName);
        if (pe != null) {
            List<? extends Element> enclosedElements = pe.getEnclosedElements();
            for (Element rootElement : enclosedElements) {
                if (rootElement instanceof TypeElement) {
                    TypeElement typeElement = (TypeElement) rootElement;
                    String aSuperClassName = canonicalClassName(typeElement.getSuperclass().toString());
                    if (superClassName.equals(aSuperClassName)) {
                        found.add(typeElement);
                    }
                }
            }
        }
    }
}
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) PackageElement(javax.lang.model.element.PackageElement) Elements(javax.lang.model.util.Elements)

Example 84 with PackageElement

use of javax.lang.model.element.PackageElement in project realm-java by realm.

the class ClassMetaData method generate.

/**
     * Builds the meta data structures for this class. Any errors or messages will be
     * posted on the provided Messager.
     *
     * @return True if meta data was correctly created and processing can continue, false otherwise.
     */
public boolean generate() {
    // Get the package of the class
    Element enclosingElement = classType.getEnclosingElement();
    if (!enclosingElement.getKind().equals(ElementKind.PACKAGE)) {
        Utils.error("The RealmClass annotation does not support nested classes.", classType);
        return false;
    }
    TypeElement parentElement = (TypeElement) Utils.getSuperClass(classType);
    if (!parentElement.toString().equals("java.lang.Object") && !parentElement.toString().equals("io.realm.RealmObject")) {
        Utils.error("Valid model classes must either extend RealmObject or implement RealmModel.", classType);
        return false;
    }
    PackageElement packageElement = (PackageElement) enclosingElement;
    packageName = packageElement.getQualifiedName().toString();
    if (!categorizeClassElements()) {
        return false;
    }
    if (!checkListTypes()) {
        return false;
    }
    if (!checkReferenceTypes()) {
        return false;
    }
    if (!checkDefaultConstructor()) {
        return false;
    }
    if (!checkForFinalFields()) {
        return false;
    }
    if (!checkForTransientFields()) {
        return false;
    }
    if (!checkForVolatileFields()) {
        return false;
    }
    // Meta data was successfully generated
    return true;
}
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) PackageElement(javax.lang.model.element.PackageElement)

Example 85 with PackageElement

use of javax.lang.model.element.PackageElement in project robolectric by robolectric.

the class RobolectricModel method prepare.

/**
   * Prepares the various derived parts of the model based on the class mappings
   * that have been registered to date.
   */
public void prepare() {
    for (Map.Entry<TypeElement, TypeElement> entry : getVisibleShadowTypes().entrySet()) {
        final TypeElement shadowType = entry.getKey();
        registerType(shadowType);
        final TypeElement solidType = entry.getValue();
        registerType(solidType);
    }
    for (Map.Entry<TypeElement, TypeElement> entry : getResetterShadowTypes().entrySet()) {
        final TypeElement shadowType = entry.getKey();
        registerType(shadowType);
    }
    while (!typeMap.isEmpty()) {
        final HashMultimap<String, TypeElement> nextRound = HashMultimap.create();
        for (Map.Entry<String, Set<TypeElement>> referents : Multimaps.asMap(typeMap).entrySet()) {
            final Set<TypeElement> c = referents.getValue();
            // name, then
            if (c.size() == 1) {
                final TypeElement type = c.iterator().next();
                referentMap.put(type, referents.getKey());
            } else {
                for (TypeElement type : c) {
                    SimpleElementVisitor6<Void, TypeElement> visitor = new SimpleElementVisitor6<Void, TypeElement>() {

                        @Override
                        public Void visitType(TypeElement parent, TypeElement type) {
                            nextRound.put(parent.getSimpleName() + "." + type.getSimpleName(), type);
                            importMap.put(type, parent);
                            return null;
                        }

                        @Override
                        public Void visitPackage(PackageElement parent, TypeElement type) {
                            referentMap.put(type, type.getQualifiedName().toString());
                            importMap.remove(type);
                            return null;
                        }
                    };
                    visitor.visit(importMap.get(type).getEnclosingElement(), type);
                }
            }
        }
        typeMap = nextRound;
    }
    for (TypeElement imp : importMap.values()) {
        if (imp.getModifiers().contains(Modifier.PUBLIC) && !JAVA_LANG.equals(imp.getEnclosingElement())) {
            imports.add(imp.getQualifiedName().toString());
        }
    }
    // Other imports that the generated class needs
    imports.add("java.util.Map");
    imports.add("java.util.HashMap");
    imports.add("javax.annotation.Generated");
    imports.add("org.robolectric.internal.ShadowExtractor");
    imports.add("org.robolectric.internal.ShadowProvider");
}
Also used : TreeSet(java.util.TreeSet) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) Set(java.util.Set) SimpleElementVisitor6(javax.lang.model.util.SimpleElementVisitor6) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) HashMap(java.util.HashMap) Map(java.util.Map) Maps.newTreeMap(com.google.common.collect.Maps.newTreeMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) TreeMap(java.util.TreeMap)

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