Search in sources :

Example 21 with JavacTask

use of com.sun.source.util.JavacTask 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 22 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6993305 method run.

void run() throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    File f = new File(testSrc, T6993305.class.getSimpleName() + ".java");
    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f);
    JavacTask task = tool.getTask(null, fm, null, null, null, fos);
    Iterable<? extends CompilationUnitTree> cus = task.parse();
    TestScanner s = new TestScanner();
    s.scan(cus, task);
    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) IOException(java.io.IOException)

Example 23 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T6199075 method compileAndCheck.

void compileAndCheck(VarargsMethod m1, VarargsMethod m2, TypeKind actual, ArgumentsArity argsArity) throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(m1, m2, actual, argsArity);
    ErrorChecker ec = new ErrorChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, ec, null, null, Arrays.asList(source));
    ct.generate();
    check(source, ec, m1, m2, actual, argsArity);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 24 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T7042566 method compileAndCheck.

void compileAndCheck() throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource();
    ErrorChecker ec = new ErrorChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, ec, null, null, Arrays.asList(source));
    ct.call();
    check(source, ec);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 25 with JavacTask

use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.

the class T7043922 method compileAndCheck.

void compileAndCheck() throws Exception {
    JavaSource source = new JavaSource();
    ErrorChecker ec = new ErrorChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, ec, null, null, Arrays.asList(source));
    ct.analyze();
    if (ec.errorFound) {
        throw new Error("invalid diagnostics for source:\n" + source.getCharContent(true) + "\nCompiler diagnostics:\n" + ec.printDiags());
    }
}
Also used : JavacTask(com.sun.source.util.JavacTask)

Aggregations

JavacTask (com.sun.source.util.JavacTask)46 JavaCompiler (javax.tools.JavaCompiler)20 JavacTool (com.sun.tools.javac.api.JavacTool)12 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)10 JavaFileObject (javax.tools.JavaFileObject)9 StandardJavaFileManager (javax.tools.StandardJavaFileManager)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)6 Context (com.sun.tools.javac.util.Context)5 File (java.io.File)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)4 ClassTree (com.sun.source.tree.ClassTree)3 Trees (com.sun.source.util.Trees)3 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 CeyloncTool (com.redhat.ceylon.compiler.java.tools.CeyloncTool)2 Tree (com.sun.source.tree.Tree)2