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 ;-)");
}
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);
}
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);
}
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;
}
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;
}
Aggregations