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 Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: T6378728.java    License: 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 Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: Test.java    License: 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 #4
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 #5
Source Project: TencentKona-8   Author: Tencent   File: TestGetResource2.java    License: 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 Project: TencentKona-8   Author: Tencent   File: TestGetResource2.java    License: 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 #7
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 #8
Source Project: TencentKona-8   Author: Tencent   File: Test.java    License: 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 #9
Source Project: dremio-oss   Author: dremio   File: TestClassCompilers.java    License: 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 #10
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 #11
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 #12
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TestSuperclass.java    License: 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 #13
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: T7068437.java    License: 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 #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Test.java    License: 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 Project: jdk8u60   Author: chenghanpeng   File: TestSuperclass.java    License: 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 #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: T6956462.java    License: 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 #17
Source Project: jdk8u60   Author: chenghanpeng   File: T6378728.java    License: 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 #18
Source Project: jdk8u60   Author: chenghanpeng   File: TestGetResource2.java    License: 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 #19
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TestGetResource2.java    License: 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 #20
Source Project: jdk8u60   Author: chenghanpeng   File: T7068437.java    License: 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 Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: Test.java    License: 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 #22
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: T6378728.java    License: 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 #23
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: T7068437.java    License: 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 #24
Source Project: lua-for-android   Author: qtiuto   File: Trees.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a Trees object for a given CompilationTask.
 * @param task the compilation task for which to get the Trees object
 * @throws IllegalArgumentException if the task does not support the Trees API.
 * @return the Trees object
 */
public static Trees instance(CompilationTask task) {
    String taskClassName = task.getClass().getName();
    if (!taskClassName.equals("com.sun.tools.javac.api.JavacTaskImpl")
            && !taskClassName.equals("com.sun.tools.javac.api.BasicJavacTask"))
        throw new IllegalArgumentException();
    return getJavacTrees(CompilationTask.class, task);
}
 
Example #25
Source Project: Bats   Author: lealone   File: JDKClassCompiler.java    License: Apache License 2.0 5 votes vote down vote up
private DrillJavaFileObject doCompile(final ClassNames className, final String sourceCode)
      throws CompileException, IOException, ClassNotFoundException {
  try {
    // Create one Java source file in memory, which will be compiled later.
    DrillJavaFileObject compilationUnit = new DrillJavaFileObject(className.dot, sourceCode);

    CompilationTask task = compiler.getTask(null, fileManager, listener, compilerOptions, null, Collections.singleton(compilationUnit));

    // Run the compiler.
    if(!task.call()) {
      throw new CompileException("Compilation failed", null);
    } else if (!compilationUnit.isCompiled()) {
      throw new ClassNotFoundException(className + ": Class file not created by compilation.");
    }
    // all good
    return compilationUnit;
  } catch (RuntimeException rte) {
    // Unwrap the compilation exception and throw it.
    Throwable cause = rte.getCause();
    if (cause != null) {
      cause = cause.getCause();
      if (cause instanceof CompileException) {
        throw (CompileException) cause;
      }
      if (cause instanceof IOException) {
        throw (IOException) cause;
      }
    }
    throw rte;
  }
}
 
Example #26
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: InMemoryJavaCompiler.java    License: 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 #27
Source Project: dragonwell8_jdk   Author: alibaba   File: InMemoryJavaCompiler.java    License: 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 #28
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: TestJavacTask_Multiple.java    License: 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 #29
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: InMemoryJavaCompiler.java    License: 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 #30
Source Project: TencentKona-8   Author: Tencent   File: InMemoryJavaCompiler.java    License: 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();
}