Search in sources :

Example 71 with CompilationTask

use of javax.tools.JavaCompiler.CompilationTask in project drill by apache.

the class JDKClassCompiler method doCompile.

private DrillJavaFileObject doCompile(final ClassNames className, final String sourceCode) throws CompileException, IOException, ClassNotFoundException {
    // JavaFileManager should be closed after its usage to release all resources opened by this file manager
    try (JavaFileManager fileManager = new DrillJavaFileManager(compiler.getStandardFileManager(listener, null, Charsets.UTF_8), classLoader)) {
        // 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;
    }
}
Also used : JavaFileManager(javax.tools.JavaFileManager) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 72 with CompilationTask

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

the class T6458823 method main.

public static void main(String[] args) throws Exception {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new RuntimeException("can't get javax.tools.JavaCompiler!");
    }
    DiagnosticCollector<JavaFileObject> diagColl = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    List<String> options = new ArrayList<String>();
    options.add("-processor");
    options.add("MyProcessor");
    options.add("-proc:only");
    List<File> files = new ArrayList<File>();
    files.add(new File(T6458823.class.getResource("TestClass.java").toURI()));
    final CompilationTask task = compiler.getTask(null, fm, diagColl, options, null, fm.getJavaFileObjectsFromFiles(files));
    task.call();
    int diagCount = 0;
    for (Diagnostic<? extends JavaFileObject> diag : diagColl.getDiagnostics()) {
        if (diag.getKind() != Diagnostic.Kind.WARNING) {
            throw new AssertionError("Only warnings expected");
        }
        System.out.println(diag);
        if (diag.getPosition() == Diagnostic.NOPOS) {
            throw new AssertionError("No position info in message");
        }
        if (diag.getSource() == null) {
            throw new AssertionError("No source info in message");
        }
        diagCount++;
    }
    if (diagCount != 2) {
        throw new AssertionError("unexpected number of warnings: " + diagCount + ", expected: 2");
    }
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) CompilationTask(javax.tools.JavaCompiler.CompilationTask) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) File(java.io.File)

Example 73 with CompilationTask

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

the class TestSuperclass method run.

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

Example 74 with CompilationTask

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

the class CeylonDocToolTests method compileSdkJavaFiles.

/**
 * This is disgusting, but the current CeylonDoc doesn't handle source files, so we need to compile them first,
 * and we do it using javac to avoid compiling the whole SDK for one java file.
 */
private void compileSdkJavaFiles() throws FileNotFoundException, IOException {
    // put it all in a special folder
    File dir = new File("build", "CeylonDocToolTest/" + name.getMethodName());
    if (dir.exists()) {
        FileUtil.delete(dir);
    }
    dir.mkdirs();
    // download a required jar
    RepositoryManager repoManager = CeylonUtils.repoManager().buildManager();
    File undertowCoreModule = repoManager.getArtifact(new ArtifactContext("io.undertow.core", "1.0.0.Beta20", ".jar"));
    File narnyaModule = repoManager.getArtifact(new ArtifactContext("org.jboss.narayana.jta", "5.1.1.Final", ".jar"));
    File languageModule = repoManager.getArtifact(new ArtifactContext(AbstractModelLoader.CEYLON_LANGUAGE, TypeChecker.LANGUAGE_MODULE_VERSION, ".car"));
    // fire up the java compiler
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    assertNotNull("Missing Java compiler, this test is probably being run with a JRE instead of a JDK!", compiler);
    List<String> options = Arrays.asList("-sourcepath", "../ceylon-sdk/source", "-d", dir.getAbsolutePath(), "-classpath", undertowCoreModule.getAbsolutePath() + File.pathSeparator + narnyaModule.getAbsolutePath() + File.pathSeparator + languageModule.getAbsolutePath());
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    String[] fileNames = new String[] { "ceylon/net/http/server/internal/JavaHelper.java", "ceylon/interop/java/internal/javaBooleanArray_.java", "ceylon/interop/java/internal/javaByteArray_.java", "ceylon/interop/java/internal/javaCharArray_.java", "ceylon/interop/java/internal/javaDoubleArray_.java", "ceylon/interop/java/internal/javaFloatArray_.java", "ceylon/interop/java/internal/javaIntArray_.java", "ceylon/interop/java/internal/javaLongArray_.java", "ceylon/interop/java/internal/javaObjectArray_.java", "ceylon/interop/java/internal/javaShortArray_.java", "ceylon/interop/java/internal/javaStringArray_.java", "ceylon/interop/java/internal/Util.java", "ceylon/transaction/internal/RecoveryHelper.java", "ceylon/transaction/internal/RecoveryXAResource.java" };
    List<String> qualifiedNames = new ArrayList<String>(fileNames.length);
    for (String name : fileNames) {
        qualifiedNames.add("../ceylon-sdk/source/" + name);
    }
    Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromStrings(qualifiedNames);
    CompilationTask task = compiler.getTask(null, null, null, options, null, fileObjects);
    Boolean ret = task.call();
    Assert.assertEquals("Compilation failed", Boolean.TRUE, ret);
    // now we need to zip it up
    makeCarFromClassFiles(dir, fileNames, "ceylon.net", Versions.CEYLON_VERSION_NUMBER);
    makeCarFromClassFiles(dir, fileNames, "ceylon.interop.java", Versions.CEYLON_VERSION_NUMBER);
    makeCarFromClassFiles(dir, fileNames, "ceylon.transaction", Versions.CEYLON_VERSION_NUMBER);
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) RepositoryManager(com.redhat.ceylon.cmr.api.RepositoryManager) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) File(java.io.File) CompilationTask(javax.tools.JavaCompiler.CompilationTask)

Example 75 with CompilationTask

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

the class BcTests method testBinaryVersionIncompatibleModule1.

// tests before we renamed module_.class to $module_.class
@Test
public void testBinaryVersionIncompatibleModule1() throws IOException {
    CompilationTask compiler = compileJava("binaryVersionOld/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/binaryVersionOld/1/");
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, "com.redhat.ceylon.compiler.java.test.bc.binaryVersionOld-1.jar");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("com/redhat/ceylon/compiler/java/test/bc/binaryVersionOld/module_.class");
    outputStream.putNextEntry(entry);
    File javaClass = new File(destDir, "com/redhat/ceylon/compiler/java/test/bc/binaryVersionOld/module_.class");
    FileInputStream inputStream = new FileInputStream(javaClass);
    Util.copy(inputStream, outputStream);
    inputStream.close();
    outputStream.close();
    assertErrors("binaryVersion/module", new CompilerError(21, "version '1' of module 'com.redhat.ceylon.compiler.java.test.bc.binaryVersionOld' 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)

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