Search in sources :

Example 86 with JavacTask

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

the class Warn4 method test.

static void test(SourceLevel sourceLevel, TrustMe trustMe, SuppressLevel suppressLevelClient, SuppressLevel suppressLevelDecl, ModifierKind modKind, Signature vararg_meth, Signature client_meth) throws Exception {
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavaSource source = new JavaSource(trustMe, suppressLevelClient, suppressLevelDecl, modKind, vararg_meth, client_meth);
    DiagnosticChecker dc = new DiagnosticChecker();
    JavacTask ct = (JavacTask) tool.getTask(null, fm, dc, Arrays.asList("-Xlint:unchecked", "-source", sourceLevel.sourceKey), null, Arrays.asList(source));
    // to get mandatory notes
    ct.generate();
    check(dc.warnings, sourceLevel, new boolean[] { vararg_meth.giveUnchecked(client_meth), vararg_meth.giveVarargs(client_meth) }, source, trustMe, suppressLevelClient, suppressLevelDecl, modKind);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 87 with JavacTask

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

the class Test method test.

void test(File test) throws Exception {
    JavacTool tool1 = JavacTool.create();
    StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
    // parse test file into a tree, and write it out to a stringbuffer using Pretty
    JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Iterable<? extends CompilationUnitTree> trees = t1.parse();
    for (CompilationUnitTree tree : trees) {
        new Pretty(pw, true).printExpr((JCTree) tree);
    }
    pw.close();
    final String out = sw.toString();
    System.err.println("generated code:\n" + out + "\n");
    // verify the generated code is valid Java by compiling it
    JavacTool tool2 = JavacTool.create();
    JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return out;
        }
    };
    JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
    boolean ok = t2.call();
    if (!ok)
        throw new Exception("compilation of generated code failed");
    File expectedClass = new File(test.getName().replace(".java", ".class"));
    if (!expectedClass.exists())
        throw new Exception(expectedClass + " not found");
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) Pretty(com.sun.tools.javac.tree.Pretty) JavacTask(com.sun.source.util.JavacTask)

Example 88 with JavacTask

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

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 89 with JavacTask

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

the class T6956638 method test.

void test(File... sourceFiles) throws Exception {
    System.err.println("Test " + (++count) + ": " + Arrays.asList(sourceFiles));
    File classesDir = new File("classes" + count);
    classesDir.mkdirs();
    StringWriter compilerOutputStream = new StringWriter();
    List<String> compileOptions = Arrays.asList("-d", classesDir.getPath());
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null);
    Iterable<? extends JavaFileObject> sourceFileObjects = fileManager.getJavaFileObjects(sourceFiles);
    System.err.println("1- javac given java source JavaFileObjects " + sourceFileObjects);
    JavaCompiler.CompilationTask task = compiler.getTask(compilerOutputStream, fileManager, null, compileOptions, null, sourceFileObjects);
    JavacTask javacTask = (JavacTask) task;
    Iterable<? extends CompilationUnitTree> parsedTrees = javacTask.parse();
    Iterable<? extends Element> analyzedTrees = javacTask.analyze();
    Iterable<? extends JavaFileObject> generatedFiles = javacTask.generate();
    System.err.println("2- parsed:" + size(parsedTrees) + " analysed:" + size(analyzedTrees) + " generated:" + size(generatedFiles));
    System.err.print("3-");
    for (JavaFileObject f : generatedFiles) System.err.print(" " + f);
    System.err.println("");
    System.err.print("5-");
    for (File f : classesDir.listFiles()) System.err.print(" " + f);
    System.err.println("");
    System.err.println("----");
    System.err.println(compilerOutputStream.toString());
    if (size(generatedFiles) != size(parsedTrees)) {
        throw new Exception("wrong number of files generated: " + size(generatedFiles) + " expected: " + size(parsedTrees));
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StringWriter(java.io.StringWriter) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) File(java.io.File)

Example 90 with JavacTask

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

the class InterruptedExceptionTest method run.

void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
    JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, Arrays.asList(xlint.getXlintOption()), null, Arrays.asList(source));
    ct.analyze();
    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