Search in sources :

Example 51 with PackageElement

use of javax.lang.model.element.PackageElement in project ceylon-compiler by ceylon.

the class Main method main.

public static void main(String[] args) throws Exception {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
    JavacTask javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
    Elements elements = javac.getElements();
    final Set<String> packages = new LinkedHashSet<String>();
    int nestedClasses = 0;
    int classes = 0;
    for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
        String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
        if (type.endsWith("package-info"))
            continue;
        try {
            TypeElement elem = elements.getTypeElement(type);
            if (elem == null && type.indexOf('$') > 0) {
                nestedClasses++;
                type = null;
                continue;
            }
            classes++;
            packages.add(getPackage(elem).getQualifiedName().toString());
            // force completion
            elements.getTypeElement(type).getKind();
            type = null;
        } finally {
            if (type != null)
                System.err.println("Looking at " + type);
        }
    }
    javac = null;
    elements = null;
    javac = (JavacTask) tool.getTask(null, fm, null, null, null, null);
    elements = javac.getElements();
    for (String name : packages) {
        PackageElement pe = elements.getPackageElement(name);
        for (Element e : pe.getEnclosedElements()) {
            e.getSimpleName().getClass();
        }
    }
    /*
         * A few sanity checks based on current values:
         *
         * packages: 775, classes: 12429 + 5917
         *
         * As the platform evolves the numbers are likely to grow
         * monotonically but in case somebody gets a clever idea for
         * limiting the number of packages exposed, this number might
         * drop.  So we test low values.
         */
    System.out.format("packages: %s, classes: %s + %s%n", packages.size(), classes, nestedClasses);
    if (classes < 9000)
        throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
    if (packages.size() < 530)
        throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
    if (nestedClasses < 3000)
        throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) PackageElement(javax.lang.model.element.PackageElement) JavacTask(com.sun.source.util.JavacTask)

Example 52 with PackageElement

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

the class ConstantsTreeVisitor method visitCompilationUnit.

@Override
public ConstantsVisitorContext visitCompilationUnit(CompilationUnitTree node, ConstantsVisitorContext context) {
    // For JDK8 visitPackage is not called for package-info.java, so we have to resolve package-info from compilation unit
    String sourceName = node.getSourceFile().getName();
    if (sourceName.endsWith(PACKAGE_INFO_JAVA)) {
        PackageElement packageElement = elements.getPackageOf(trees.getElement(getCurrentPath()));
        String visitedPackageInfo = packageElement == null || packageElement.getQualifiedName().toString().isEmpty() ? PACKAGE_INFO : packageElement.getQualifiedName().toString() + "." + PACKAGE_INFO;
        return super.visitCompilationUnit(node, new ConstantsVisitorContext(visitedPackageInfo, consumer::consumeAccessibleDependent));
    }
    return super.visitCompilationUnit(node, context);
}
Also used : PackageElement(javax.lang.model.element.PackageElement)

Example 53 with PackageElement

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

the class ElementUtils method getTopLevelTypeName.

public static String getTopLevelTypeName(Element originatingElement) {
    Element current = originatingElement;
    Element parent = originatingElement;
    while (parent != null && !(parent instanceof PackageElement)) {
        current = parent;
        parent = current.getEnclosingElement();
    }
    String name = getElementName(current);
    if (name != null) {
        return name;
    }
    throw new IllegalArgumentException("Unexpected element " + originatingElement);
}
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)

Example 54 with PackageElement

use of javax.lang.model.element.PackageElement in project classindex by atteo.

the class ClassIndexProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    try {
        for (Element element : roundEnv.getRootElements()) {
            if (!(element instanceof TypeElement)) {
                continue;
            }
            final PackageElement packageElement = getPackage(element);
            element.accept(new ElementScanner6<Void, Void>() {

                @Override
                public Void visitType(TypeElement typeElement, Void o) {
                    try {
                        for (AnnotationMirror mirror : typeElement.getAnnotationMirrors()) {
                            storeRepeatableAnnotation(mirror, typeElement);
                            final TypeElement annotationElement = (TypeElement) mirror.getAnnotationType().asElement();
                            storeAnnotation(annotationElement, typeElement);
                        }
                        indexSupertypes(typeElement, typeElement);
                        if (packageElement != null) {
                            storeClassFromPackage(packageElement, typeElement);
                        }
                    } catch (IOException e) {
                        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] " + e.getMessage());
                    }
                    if (typeElement.getKind().toString().equals("RECORD")) {
                        return null;
                    }
                    return super.visitType(typeElement, o);
                }
            }, null);
        }
        if (!roundEnv.processingOver()) {
            return false;
        }
        writeIndexFiles(ClassIndex.SUBCLASS_INDEX_PREFIX, subclassMap);
        writeIndexFiles(ClassIndex.ANNOTATED_INDEX_PREFIX, annotatedMap);
        for (Map.Entry<String, Set<String>> entry : packageMap.entrySet()) {
            writeSimpleNameIndexFile(entry.getValue(), entry.getKey().replace(".", "/") + "/" + ClassIndex.PACKAGE_INDEX_NAME);
        }
    } catch (IOException e) {
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Can't write index file: " + e.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
        messager.printMessage(Diagnostic.Kind.ERROR, "[ClassIndexProcessor] Internal error: " + e.getMessage());
    }
    return false;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) PackageElement(javax.lang.model.element.PackageElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with PackageElement

use of javax.lang.model.element.PackageElement in project checker-framework by typetools.

the class ElementUtils method enclosingPackage.

/**
 * Returns the innermost package element enclosing the given element. The same effect as {@link
 * javax.lang.model.util.Elements#getPackageOf(Element)}. Returns the element itself if it is a
 * package.
 *
 * @param elem the enclosed element of a package
 * @return the innermost package element
 */
public static PackageElement enclosingPackage(final Element elem) {
    Element result = elem;
    while (result != null && result.getKind() != ElementKind.PACKAGE) {
        @Nullable Element encl = result.getEnclosingElement();
        result = encl;
    }
    return (PackageElement) result;
}
Also used : 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) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

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