javax.tools.JavaCompiler.CompilationTask Java Examples

The following examples show how to use javax.tools.JavaCompiler.CompilationTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestGetResource2.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    System.err.println("testCompositeSearchPath");
    File tmpdir = new File("testCompositeSourcePath");
    File srcdir = new File(tmpdir, "src");
    File rsrcdir = new File(tmpdir, "rsrc");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(rsrcdir, "resources/file.txt", "hello");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with composite source path: " + result);
    expect(result, true);
}
 
Example #2
Source File: T6378728.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Get a compiler tool
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    String srcdir = System.getProperty("test.src");
    File source = new File(srcdir, "T6378728.java");
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);

    CompilationTask task =
        compiler.getTask(null,
                         new ExceptionalFileManager(fm),
                         null,
                         Arrays.asList("-proc:only"),
                         null,
                         fm.getJavaFileObjectsFromFiles(Arrays.asList(source)));
    if (!task.call())
        throw new RuntimeException("Unexpected compilation failure");
}
 
Example #3
Source File: TestSuperclass.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
    System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
    File testDir = new File(ck + "-" + gk + "-" + sk);
    testDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));

    JavaSource js = new JavaSource();
    System.err.println(js.getCharContent(false));
    CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
    if (!t.call())
        throw new Error("compilation failed");

    File testClass = new File(testDir, "Test.class");
    String out = javap(testClass);

    // Extract class sig from first line of Java source
    String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");

    // Extract class sig from line from javap output
    String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");

    checkEqual("class signature", expect, found);

    return errors;
}
 
Example #4
Source File: T6378728.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    // Get a compiler tool
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    String srcdir = System.getProperty("test.src");
    File source = new File(srcdir, "T6378728.java");
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);

    CompilationTask task =
        compiler.getTask(null,
                         new ExceptionalFileManager(fm),
                         null,
                         Arrays.asList("-proc:only"),
                         null,
                         fm.getJavaFileObjectsFromFiles(Arrays.asList(source)));
    if (!task.call())
        throw new RuntimeException("Unexpected compilation failure");
}
 
Example #5
Source File: TestGetResource2.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    System.err.println("testCompositeSearchPath");
    File tmpdir = new File("testCompositeSourcePath");
    File srcdir = new File(tmpdir, "src");
    File rsrcdir = new File(tmpdir, "rsrc");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(rsrcdir, "resources/file.txt", "hello");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with composite source path: " + result);
    expect(result, true);
}
 
Example #6
Source File: Test.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
void test(List<String> options, String expect) throws Exception {
    System.err.println("test: " + options);
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavaFileObject f = new MyFileObject("myfo://test", "class Bad { Missing x; }");
    List<? extends JavaFileObject> files = Arrays.asList(f);
    CompilationTask task = javac.getTask(pw, null, null, options, null, files);
    boolean ok = task.call();
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (ok)
        throw new Exception("Compilation succeeded unexpectedly");
    if (!out.contains(expect))
        throw new Exception("expected text not found: " + expect);
}
 
Example #7
Source File: TestGetResource2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void testMissingResource(JavaCompiler javac) throws Exception {
    System.err.println("testMissingResource");
    File tmpdir = new File("testMissingResource");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result when missing resource: " + result);
    expect(result, false);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #8
Source File: T6956462.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    }
    try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
        List<File> files = new ArrayList<File>();
        files.add(new File(T6956462.class.getResource("TestClass.java").toURI()));
        final CompilationTask task = compiler.getTask(null, fm, null,
            null, null, fm.getJavaFileObjectsFromFiles(files));
        JavacTask javacTask = (JavacTask) task;
        for (CompilationUnitTree cu : javacTask.parse()) {
            cu.accept(new MyVisitor(javacTask), null);
        }
    }
}
 
Example #9
Source File: T7068437.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    System.err.println("using " + compiler.getClass()
            + " from " + compiler.getClass().getProtectionDomain().getCodeSource());

    CompilationTask task = compiler.getTask(null, null, null,
            Collections.singleton("-proc:only"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());

    task = compiler.getTask(null, null, null,
            Arrays.asList("-proc:only", "-AexpectFile"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());
}
 
Example #10
Source File: TestGetResource2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    System.err.println("testCompositeSearchPath");
    File tmpdir = new File("testCompositeSourcePath");
    File srcdir = new File(tmpdir, "src");
    File rsrcdir = new File(tmpdir, "rsrc");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(rsrcdir, "resources/file.txt", "hello");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with composite source path: " + result);
    expect(result, true);
}
 
Example #11
Source File: TestGetResource2.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void testMissingResource(JavaCompiler javac) throws Exception {
    System.err.println("testMissingResource");
    File tmpdir = new File("testMissingResource");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result when missing resource: " + result);
    expect(result, false);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #12
Source File: T6378728.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // Get a compiler tool
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    String srcdir = System.getProperty("test.src");
    File source = new File(srcdir, "T6378728.java");
    try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {

        CompilationTask task =
            compiler.getTask(null,
                             new ExceptionalFileManager(fm),
                             null,
                             Arrays.asList("-proc:only"),
                             null,
                             fm.getJavaFileObjectsFromFiles(Arrays.asList(source)));
        if (!task.call())
            throw new RuntimeException("Unexpected compilation failure");
    }
}
 
Example #13
Source File: TestGetResource2.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void testMissingResource(JavaCompiler javac) throws Exception {
    System.err.println("testMissingResource");
    File tmpdir = new File("testMissingResource");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result when missing resource: " + result);
    expect(result, false);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #14
Source File: Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void test(List<String> options, String expect) throws Exception {
    System.err.println("test: " + options);
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavaFileObject f = new MyFileObject("myfo://test", "class Bad { Missing x; }");
    List<? extends JavaFileObject> files = Arrays.asList(f);
    CompilationTask task = javac.getTask(pw, null, null, options, null, files);
    boolean ok = task.call();
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (ok)
        throw new Exception("Compilation succeeded unexpectedly");
    if (!out.contains(expect))
        throw new Exception("expected text not found: " + expect);
}
 
Example #15
Source File: Test.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
void test(List<String> options, String expect) throws Exception {
    System.err.println("test: " + options);
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavaFileObject f = new MyFileObject("myfo://test", "class Bad { Missing x; }");
    List<? extends JavaFileObject> files = Arrays.asList(f);
    CompilationTask task = javac.getTask(pw, null, null, options, null, files);
    boolean ok = task.call();
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (ok)
        throw new Exception("Compilation succeeded unexpectedly");
    if (!out.contains(expect))
        throw new Exception("expected text not found: " + expect);
}
 
Example #16
Source File: T7068437.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    System.err.println("using " + compiler.getClass()
            + " from " + compiler.getClass().getProtectionDomain().getCodeSource());

    CompilationTask task = compiler.getTask(null, null, null,
            Collections.singleton("-proc:only"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());

    task = compiler.getTask(null, null, null,
            Arrays.asList("-proc:only", "-AexpectFile"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());
}
 
Example #17
Source File: TestGetResource2.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void testSingleSourceDir(JavaCompiler javac) throws Exception {
    System.err.println("testSingleSourceDir");
    File tmpdir = new File("testSingleSourceDir");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(srcdir, "resources/file.txt", "hello");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with single source dir: " + result);
    expect(result, true);
}
 
Example #18
Source File: Test.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void test(List<String> options, String expect) throws Exception {
    System.err.println("test: " + options);
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JavaFileObject f = new MyFileObject("myfo://test", "class Bad { Missing x; }");
    List<? extends JavaFileObject> files = Arrays.asList(f);
    CompilationTask task = javac.getTask(pw, null, null, options, null, files);
    boolean ok = task.call();
    pw.close();
    String out = sw.toString();
    if (!out.isEmpty())
        System.err.println(out);
    if (ok)
        throw new Exception("Compilation succeeded unexpectedly");
    if (!out.contains(expect))
        throw new Exception("expected text not found: " + expect);
}
 
Example #19
Source File: TestSuperclass.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
    System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
    File testDir = new File(ck + "-" + gk + "-" + sk);
    testDir.mkdirs();
    fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));

    JavaSource js = new JavaSource();
    System.err.println(js.getCharContent(false));
    CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
    if (!t.call())
        throw new Error("compilation failed");

    File testClass = new File(testDir, "Test.class");
    String out = javap(testClass);

    // Extract class sig from first line of Java source
    String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");

    // Extract class sig from line from javap output
    String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");

    checkEqual("class signature", expect, found);

    return errors;
}
 
Example #20
Source File: T7068437.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    System.err.println("using " + compiler.getClass()
            + " from " + compiler.getClass().getProtectionDomain().getCodeSource());

    CompilationTask task = compiler.getTask(null, null, null,
            Collections.singleton("-proc:only"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());

    task = compiler.getTask(null, null, null,
            Arrays.asList("-proc:only", "-AexpectFile"),
            Collections.singleton("java.lang.Object"),
            null);
    task.setProcessors(Collections.singleton(new Proc()));
    check("compilation", task.call());
}
 
Example #21
Source File: TestClassCompilers.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void compileDependencyClass() throws IOException, ClassNotFoundException {
  JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
  Assume.assumeNotNull(javaCompiler);

  classes = temporaryFolder.newFolder("classes");;

  StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, Locale.ROOT, UTF_8);
  fileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(classes));

  SimpleJavaFileObject compilationUnit = new SimpleJavaFileObject(URI.create("FooTest.java"), Kind.SOURCE) {
    String fooTestSource = Resources.toString(Resources.getResource("com/dremio/exec/compile/FooTest.java"), UTF_8);
    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
      return fooTestSource;
    }
  };

  CompilationTask task = javaCompiler.getTask(null, fileManager, null, Collections.<String>emptyList(), null, ImmutableList.of(compilationUnit));
  assertTrue(task.call());
}
 
Example #22
Source File: TestGetResource2.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void testMissingResource(JavaCompiler javac) throws Exception {
    System.err.println("testMissingResource");
    File tmpdir = new File("testMissingResource");
    File srcdir = new File(tmpdir, "src");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result when missing resource: " + result);
    expect(result, false);

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #23
Source File: TestGetResource2.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    System.err.println("testCompositeSearchPath");
    File tmpdir = new File("testCompositeSourcePath");
    File srcdir = new File(tmpdir, "src");
    File rsrcdir = new File(tmpdir, "rsrc");
    File destdir = new File(tmpdir, "dest");
    write(srcdir, "pkg/X.java", "package pkg; class X {}");
    write(rsrcdir, "resources/file.txt", "hello");
    destdir.mkdirs();

    CompilationTask task = javac.getTask(null, null, null,
            Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
            Collections.singleton("pkg.X"), null);
    task.setProcessors(Collections.singleton(new AnnoProc()));
    boolean result = task.call();
    System.err.println("javac result with composite source path: " + result);
    expect(result, true);
}
 
Example #24
Source File: TestJavacTask_Lock.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
int test(CompilationTask t) {
    try {
        ((JavacTask) t).parse();
    return 1;
    } catch (IOException ex) {
        throw new Error(ex);
    }
}
 
Example #25
Source File: InMemoryJavaCompiler.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the class with the given name and source code.
 *
 * @param className The name of the class
 * @param sourceCode The source code for the class with name {@code className}
 * @throws RuntimeException if the compilation did not succeed
 * @return The resulting byte code from the compilation
 */
public static byte[] compile(String className, CharSequence sourceCode) {
    MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode);
    CompilationTask task = getCompilationTask(file);

    if(!task.call()) {
        throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
    }

    return file.getByteCode();
}
 
Example #26
Source File: T6956462.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    }
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    List<File> files = new ArrayList<File>();
    files.add(new File(T6956462.class.getResource("TestClass.java").toURI()));
    final CompilationTask task = compiler.getTask(null, fm, null,
        null, null, fm.getJavaFileObjectsFromFiles(files));
    JavacTask javacTask = (JavacTask) task;
    for (CompilationUnitTree cu : javacTask.parse()) {
        cu.accept(new MyVisitor(javacTask), null);
    }
}
 
Example #27
Source File: InMemoryJavaCompiler.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the class with the given name and source code.
 *
 * @param className The name of the class
 * @param sourceCode The source code for the class with name {@code className}
 * @param options additional command line options
 * @throws RuntimeException if the compilation did not succeed
 * @return The resulting byte code from the compilation
 */
public static byte[] compile(String className, CharSequence sourceCode, String... options) {
    MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode);
    CompilationTask task = getCompilationTask(file, options);

    if(!task.call()) {
        throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
    }

    return file.getByteCode();
}
 
Example #28
Source File: T7031108.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void compile(JavaCompiler comp, JavaFileManager fm,
        DiagnosticListener<JavaFileObject> dl,
        String processor, JavaFileObject... files) throws Exception {
    System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
    List<String> opts = new ArrayList<String>();
    if (processor != null) {
        // opts.add("-verbose");
        opts.addAll(Arrays.asList("-processor", processor));
    }
    CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
    boolean ok = task.call();
    if (dl == null && !ok)
        throw new Exception("compilation failed");
}
 
Example #29
Source File: Example.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
    if (out != null && keys != null)
        throw new IllegalArgumentException();

    if (verbose)
        System.err.println("run_jsr199: " + opts + " " + files);

    DiagnosticCollector<JavaFileObject> dc = null;
    if (keys != null)
        dc = new DiagnosticCollector<JavaFileObject>();

    if (raw) {
        List<String> newOpts = new ArrayList<String>();
        newOpts.add("-XDrawDiagnostics");
        newOpts.addAll(opts);
        opts = newOpts;
    }

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager fm = c.getStandardFileManager(dc, null, null);
    if (fmOpts != null)
        fm = new FileManager(fm, fmOpts);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    CompilationTask t = c.getTask(out, fm, dc, opts, null, fos);
    Boolean ok = t.call();

    if (keys != null) {
        for (Diagnostic<? extends JavaFileObject> d: dc.getDiagnostics()) {
            scanForKeys(unwrap(d), keys);
        }
    }

    return ok;
}
 
Example #30
Source File: EagerInterfaceCompletionTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void compile(DiagnosticChecker dc, JavaSource... sources) {
    try {
        CompilationTask ct = javacTool.getTask(null, null, dc,
                Arrays.asList("-d", testDir.getAbsolutePath(), "-cp",
                testDir.getAbsolutePath(), versionKind.optsArr[0], versionKind.optsArr[1]),
                null, Arrays.asList(sources));
        ct.call();
    }
    catch (Exception e) {
        e.printStackTrace();
        error("Internal compilation error");
    }
}