Search in sources :

Example 31 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class Test method test.

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);
}
Also used : CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 32 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class T6378728 method main.

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");
}
Also used : File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 33 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class BcTests method testBinaryVersionIncompatibleModule2.

// tests after we renamed module_.class to $module_.class
@Test
public void testBinaryVersionIncompatibleModule2() throws IOException {
    CompilationTask compiler = compileJava("binaryVersionOld2/$module_.java");
    Assert.assertTrue(compiler.call());
    // so now make a jar containing the java module
    File jarFolder = new File(destDir, "com/redhat/ceylon/compiler/java/test/bc/binaryVersionOld2/1/");
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, "com.redhat.ceylon.compiler.java.test.bc.binaryVersionOld2-1.jar");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("com/redhat/ceylon/compiler/java/test/bc/binaryVersionOld2/$module_.class");
    outputStream.putNextEntry(entry);
    File javaClass = new File(destDir, "com/redhat/ceylon/compiler/java/test/bc/binaryVersionOld2/$module_.class");
    FileInputStream inputStream = new FileInputStream(javaClass);
    Util.copy(inputStream, outputStream);
    inputStream.close();
    outputStream.close();
    assertErrors("binaryVersion2/module", new CompilerError(21, "version '1' of module 'com.redhat.ceylon.compiler.java.test.bc.binaryVersionOld2' was compiled by" + " an incompatible version of the compiler" + " (binary version 0.0 of module is not compatible with binary version " + Versions.JVM_BINARY_MAJOR_VERSION + "." + Versions.JVM_BINARY_MINOR_VERSION + " of this compiler)"));
}
Also used : FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) CompilerError(com.redhat.ceylon.compiler.java.test.CompilerError) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 34 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

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)

Example 35 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project ceylon-compiler by ceylon.

the class TestGetResource2 method testSingleSourceDir.

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);
}
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