Search in sources :

Example 76 with JavacTask

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

the class OverrideBridge method compile.

static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
    File destDir = new File(workDir, destPath);
    destDir.mkdir();
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(implB, implC, implD);
    JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-d", destPath), null, Arrays.asList(source));
    ct.generate();
    Map<ClassFile, List<Method>> members = new HashMap<>();
    addMembers(destDir, members);
    return members;
}
Also used : ClassFile(com.sun.tools.classfile.ClassFile) HashMap(java.util.HashMap) JavaCompiler(javax.tools.JavaCompiler) ArrayList(java.util.ArrayList) List(java.util.List) JavacTask(com.sun.source.util.JavacTask) ClassFile(com.sun.tools.classfile.ClassFile)

Example 77 with JavacTask

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

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    // System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Example 78 with JavacTask

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

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Example 79 with JavacTask

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

the class TreePosTest method read.

/**
 * Read a file.
 * @param file the file to be read
 * @return the tree for the content of the file
 * @throws IOException if any IO errors occur
 * @throws TreePosTest.ParseException if any errors occur while parsing the file
 */
JCCompilationUnit read(File file) throws IOException, ParseException {
    JavacTool tool = JavacTool.create();
    r.errors = 0;
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
    JavacTask task = tool.getTask(pw, fm, r, Collections.<String>emptyList(), null, files);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    pw.flush();
    if (r.errors > 0)
        throw new ParseException(sw.toString());
    Iterator<? extends CompilationUnitTree> iter = trees.iterator();
    if (!iter.hasNext())
        throw new Error("no trees found");
    JCCompilationUnit t = (JCCompilationUnit) iter.next();
    if (iter.hasNext())
        throw new Error("too many trees found");
    return t;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavacTool(com.sun.tools.javac.api.JavacTool) JavacTask(com.sun.source.util.JavacTask)

Example 80 with JavacTask

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

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)

Aggregations

JavacTask (com.sun.source.util.JavacTask)97 JavaCompiler (javax.tools.JavaCompiler)45 JavaFileObject (javax.tools.JavaFileObject)27 JavacTool (com.sun.tools.javac.api.JavacTool)24 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)23 StandardJavaFileManager (javax.tools.StandardJavaFileManager)17 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)16 IOException (java.io.IOException)14 DiagnosticCollector (javax.tools.DiagnosticCollector)13 File (java.io.File)12 StringWriter (java.io.StringWriter)12 Context (com.sun.tools.javac.util.Context)11 PrintWriter (java.io.PrintWriter)11 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)9 Path (java.nio.file.Path)8 ArrayList (java.util.ArrayList)8 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)7 Diagnostic (javax.tools.Diagnostic)7 ClassTree (com.sun.source.tree.ClassTree)6 Trees (com.sun.source.util.Trees)6