Search in sources :

Example 1 with CompilationTask

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

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, "org/eclipse/ceylon/compiler/java/test/bc/binaryVersionOld/1/");
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, "org.eclipse.ceylon.compiler.java.test.bc.binaryVersionOld-1.jar");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("org/eclipse/ceylon/compiler/java/test/bc/binaryVersionOld/module_.class");
    outputStream.putNextEntry(entry);
    File javaClass = new File(destDir, "org/eclipse/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 'org.eclipse.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(org.eclipse.ceylon.compiler.java.test.CompilerError) File(java.io.File) CompilationTask(org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with CompilationTask

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

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().systemRepo("../dist/dist/repo").buildManager();
    File jbossModulesModule = repoManager.getArtifact(new ArtifactContext(null, "org.jboss.modules", Versions.DEPENDENCY_JBOSS_MODULES_VERSION, ".jar"));
    File runtimeModule = repoManager.getArtifact(new ArtifactContext(null, "ceylon.runtime", Versions.CEYLON_VERSION_NUMBER, ".jar"));
    File modelModule = repoManager.getArtifact(new ArtifactContext(null, "org.eclipse.ceylon.model", Versions.CEYLON_VERSION_NUMBER, ".jar"));
    File undertowCoreModule = repoManager.getArtifact(new ArtifactContext(null, "io.undertow.core", "1.3.5.Final", ".jar"));
    File languageModule = repoManager.getArtifact(new ArtifactContext(null, 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 + jbossModulesModule.getAbsolutePath() + File.pathSeparator + runtimeModule.getAbsolutePath() + File.pathSeparator + modelModule.getAbsolutePath() + File.pathSeparator + languageModule.getAbsolutePath());
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    String[] fileNames = new String[] { "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/synchronize_.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.test", Versions.CEYLON_VERSION_NUMBER);
    makeCarFromClassFiles(dir, fileNames, "ceylon.http.server", 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(org.eclipse.ceylon.javax.tools.JavaCompiler) StandardJavaFileManager(org.eclipse.ceylon.javax.tools.StandardJavaFileManager) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) File(java.io.File) CompilationTask(org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask)

Example 3 with CompilationTask

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

the class CompilerTests method compileJavaModule.

protected void compileJavaModule(File jarOutputFolder, File classesOutputFolder, String moduleName, String moduleVersion, File sourceFolder, File[] extraClassPath, String... sourceFileNames) throws IOException {
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
    assertNotNull("Missing Java compiler, this test is probably being run with a JRE instead of a JDK!", javaCompiler);
    StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);
    Set<String> sourceDirectories = new HashSet<String>();
    File[] javaSourceFiles = new File[sourceFileNames.length];
    for (int i = 0; i < javaSourceFiles.length; i++) {
        javaSourceFiles[i] = new File(sourceFolder, sourceFileNames[i]);
        String sfn = sourceFileNames[i].replace(File.separatorChar, '/');
        int p = sfn.lastIndexOf('/');
        String sourceDir = sfn.substring(0, p);
        sourceDirectories.add(sourceDir);
    }
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(javaSourceFiles);
    StringBuilder cp = new StringBuilder();
    for (int i = 0; i < extraClassPath.length; i++) {
        if (i > 0)
            cp.append(File.pathSeparator);
        cp.append(extraClassPath[i]);
    }
    CompilationTask task = javaCompiler.getTask(null, null, null, Arrays.asList("-d", classesOutputFolder.getPath(), "-cp", cp.toString(), "-sourcepath", sourceFolder.getPath()), null, compilationUnits);
    assertEquals(Boolean.TRUE, task.call());
    File jarFolder = new File(jarOutputFolder, moduleName.replace('.', File.separatorChar) + File.separatorChar + moduleVersion);
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, moduleName + "-" + moduleVersion + ".jar");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    for (String sourceFileName : sourceFileNames) {
        String classFileName = sourceFileName.substring(0, sourceFileName.length() - 5) + ".class";
        ZipEntry entry = new ZipEntry(classFileName);
        outputStream.putNextEntry(entry);
        File classFile = new File(classesOutputFolder, classFileName);
        FileInputStream inputStream = new FileInputStream(classFile);
        Util.copy(inputStream, outputStream);
        inputStream.close();
        outputStream.flush();
    }
    outputStream.close();
    for (String sourceDir : sourceDirectories) {
        File module = null;
        String sourceName = "module.properties";
        File properties = new File(sourceFolder, sourceDir + File.separator + sourceName);
        if (properties.exists()) {
            module = properties;
        } else {
            sourceName = "module.xml";
            properties = new File(sourceFolder, sourceDir + File.separator + sourceName);
            if (properties.exists()) {
                module = properties;
            }
        }
        if (module != null) {
            File moduleFile = new File(sourceFolder, sourceDir + File.separator + sourceName);
            FileInputStream inputStream = new FileInputStream(moduleFile);
            FileOutputStream moduleOutputStream = new FileOutputStream(new File(jarFolder, sourceName));
            Util.copy(inputStream, moduleOutputStream);
            inputStream.close();
            moduleOutputStream.flush();
            moduleOutputStream.close();
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JavaCompiler(org.eclipse.ceylon.javax.tools.JavaCompiler) JarOutputStream(java.util.jar.JarOutputStream) CompilationTask(org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) StandardJavaFileManager(org.eclipse.ceylon.javax.tools.StandardJavaFileManager) ZipFile(java.util.zip.ZipFile) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with CompilationTask

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

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, "org/eclipse/ceylon/compiler/java/test/bc/binaryVersionOld2/1/");
    jarFolder.mkdirs();
    File jarFile = new File(jarFolder, "org.eclipse.ceylon.compiler.java.test.bc.binaryVersionOld2-1.jar");
    // now jar it up
    JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
    ZipEntry entry = new ZipEntry("org/eclipse/ceylon/compiler/java/test/bc/binaryVersionOld2/$module_.class");
    outputStream.putNextEntry(entry);
    File javaClass = new File(destDir, "org/eclipse/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 'org.eclipse.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(org.eclipse.ceylon.compiler.java.test.CompilerError) File(java.io.File) CompilationTask(org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

File (java.io.File)4 CompilationTask (org.eclipse.ceylon.javax.tools.JavaCompiler.CompilationTask)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 ZipEntry (java.util.zip.ZipEntry)3 CompilerError (org.eclipse.ceylon.compiler.java.test.CompilerError)2 JavaCompiler (org.eclipse.ceylon.javax.tools.JavaCompiler)2 StandardJavaFileManager (org.eclipse.ceylon.javax.tools.StandardJavaFileManager)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ZipFile (java.util.zip.ZipFile)1 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)1 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)1