Search in sources :

Example 71 with JavacTask

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

the class T6472751 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    trees = Trees.instance(task);
    positions = trees.getSourcePositions();
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    for (CompilationUnitTree ast : asts) {
        new MyVisitor().scan(ast, null);
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 72 with JavacTask

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

the class Foo method main.

public static void main(String... args) throws IOException {
    String testSrc = System.getProperty("test.src", ".");
    String testClasses = System.getProperty("test.classes", ".");
    JavacTool tool = JavacTool.create();
    MyDiagListener dl = new MyDiagListener();
    StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(testClasses)));
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File(testSrc, T6410706.class.getName() + ".java")));
    JavacTask task = tool.getTask(null, fm, dl, null, null, files);
    task.parse();
    task.analyze();
    task.generate();
    if (dl.notes != 2)
        throw new AssertionError(dl.notes + " notes given");
}
Also used : JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Example 73 with JavacTask

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

the class T6665791 method main.

public static void main(String[] args) throws Exception {
    write(test_java, test);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> units = manager.getJavaFileObjects(test_java);
    final StringWriter sw = new StringWriter();
    JavacTask task = (JavacTask) compiler.getTask(sw, manager, null, null, null, units);
    new TreeScanner<Boolean, Void>() {

        @Override
        public Boolean visitClass(ClassTree arg0, Void arg1) {
            sw.write(arg0.toString());
            return super.visitClass(arg0, arg1);
        }
    }.scan(task.parse(), null);
    System.out.println("output:");
    System.out.println(sw.toString());
    String found = sw.toString().replaceAll("\\s+", " ").trim();
    String expect = test.replaceAll("\\s+", " ").trim();
    if (!expect.equals(found)) {
        System.out.println("expect: " + expect);
        System.out.println("found:  " + found);
        throw new Exception("unexpected output");
    }
}
Also used : StringWriter(java.io.StringWriter) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) IOException(java.io.IOException)

Example 74 with JavacTask

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

the class T6996914a method compileAndCheck.

static void compileAndCheck(PackageKind pk, ConstructorKind ck) throws Exception {
    FooClass foo = new FooClass(pk, ck);
    ClientClass client = new ClientClass(pk);
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    ErrorListener el = new ErrorListener();
    JavacTask ct = (JavacTask) tool.getTask(null, null, el, null, null, Arrays.asList(foo, client));
    ct.analyze();
    if (el.errors > 0 == check(pk, ck)) {
        String msg = el.errors > 0 ? "Error compiling files" : "No error when compiling files";
        throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 75 with JavacTask

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

the class ParserTest method run.

void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source));
    ct.parse();
    check();
}
Also used : 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