Search in sources :

Example 56 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project querydsl by querydsl.

the class JDKEvaluatorFactory method compile.

protected void compile(String source, ClassType projectionType, String[] names, Type[] types, String id, Map<String, Object> constants) throws IOException {
    // create source
    source = createSource(source, projectionType, names, types, id, constants);
    // compile
    SimpleJavaFileObject javaFileObject = new MemSourceFileObject(id, source);
    Writer out = new StringWriter();
    CompilationTask task = compiler.getTask(out, fileManager, null, compilationOptions, null, Collections.singletonList(javaFileObject));
    if (!task.call().booleanValue()) {
        throw new CodegenException("Compilation of " + source + " failed.\n" + out.toString());
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 57 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class MainTest method compileAndJar.

public static File compileAndJar(String javaModule, String javaClassName, File addToClassPath) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    String javaModulePath = javaModule.replace('.', '/');
    File moduleFile = new File("test/" + javaModulePath, javaClassName + ".java");
    Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjects(moduleFile);
    File destDir = new File("build/javaModules");
    FileUtil.delete(destDir);
    destDir.mkdirs();
    List<String> options = new LinkedList<String>();
    options.add("-d");
    options.add(destDir.getPath());
    if (addToClassPath != null) {
        options.add("-cp");
        options.add(addToClassPath.getPath());
    }
    CompilationTask task = compiler.getTask(null, null, null, options, null, units);
    Boolean result = task.call();
    assertTrue(result != null && result.booleanValue());
    File compiledModuleFile = new File(destDir, javaModulePath + "/" + javaClassName + ".class");
    assertTrue(compiledModuleFile.isFile());
    return jar(compiledModuleFile, javaModulePath);
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) LinkedList(java.util.LinkedList) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 58 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class MainTest method testCeylonModule.

@Test
public void testCeylonModule() throws IOException, ModuleNotFoundException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    File moduleFile = new File("test/foo/foo", "$module_.java");
    Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjects(moduleFile);
    File destDir = new File("build/mainTest");
    FileUtil.delete(destDir);
    destDir.mkdirs();
    CompilationTask task = compiler.getTask(null, null, null, Arrays.asList("-d", destDir.getPath(), "-cp", "build/classes" + File.pathSeparator + LANGUAGE_MODULE_CAR + File.pathSeparator + MODEL_MODULE_CAR), null, units);
    Boolean result = task.call();
    assertTrue(result != null && result.booleanValue());
    File compiledModuleFile = new File(destDir, "foo/foo/$module_.class");
    assertTrue(compiledModuleFile.isFile());
    File jar = jar(compiledModuleFile, "foo/foo");
    try {
        checkJarDependencies(jar);
    } finally {
        jar.delete();
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask) Test(org.junit.Test)

Example 59 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class TestJavacTask_Lock method test.

void test(MethodKind first, MethodKind second) {
    System.err.println("test: " + first + ", " + second);
    File testSrc = new File(System.getProperty("test.src"));
    String thisClassName = TestJavacTask_Lock.class.getName();
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(new File(testSrc, thisClassName + ".java"));
    File tmpDir = new File(first + "_" + second);
    tmpDir.mkdirs();
    List<String> options = Arrays.asList("-d", tmpDir.getPath());
    CompilationTask t = comp.getTask(null, fm, null, options, null, files);
    try {
        first.test(t);
        second.test(t);
        error("No exception thrown");
    } catch (IllegalStateException e) {
        System.err.println("Expected exception caught: " + e);
    } catch (Exception e) {
        error("Unexpected exception caught: " + e);
        e.printStackTrace(System.err);
    }
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 60 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon by eclipse.

the class T6900149 method main.

public static void main(String[] args) throws IOException {
    DiagnosticCollector<JavaFileObject> diag = new DiagnosticCollector<JavaFileObject>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    File emptyFile = File.createTempFile("Empty", ".java");
    File[] files = new File[] { emptyFile, emptyFile };
    CompilationTask task = compiler.getTask(null, fm, diag, null, null, fm.getJavaFileObjects(files));
    if (!task.call()) {
        throw new AssertionError("compilation failed");
    }
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Aggregations

CompilationTask (javax.tools.JavaCompiler.CompilationTask)84 JavaCompiler (javax.tools.JavaCompiler)38 File (java.io.File)33 JavaFileObject (javax.tools.JavaFileObject)32 StandardJavaFileManager (javax.tools.StandardJavaFileManager)28 DiagnosticCollector (javax.tools.DiagnosticCollector)27 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)18 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)14 URLClassLoader (java.net.URLClassLoader)10 StringWriter (java.io.StringWriter)9 URL (java.net.URL)6 JavaFileManager (javax.tools.JavaFileManager)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 PrintWriter (java.io.PrintWriter)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3