Search in sources :

Example 1 with ZipArchive

use of com.sun.tools.javac.file.ZipArchive in project groovy-cps by cloudbees.

the class Driver method run.

public void run(File dir) throws Exception {
    JavaCompiler javac = JavacTool.create();
    DiagnosticListener<JavaFileObject> errorListener = createErrorListener();
    try (StandardJavaFileManager fileManager = javac.getStandardFileManager(errorListener, Locale.getDefault(), Charset.defaultCharset())) {
        fileManager.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(Which.jarFile(GroovyShell.class)));
        File groovySrcJar = Which.jarFile(Driver.class.getClassLoader().getResource("groovy/lang/GroovyShell.java"));
        // classes to translate
        // TODO include other classes mentioned in DgmConverter if they have any applicable methods; and certainly StringGroovyMethods which has some Closure-based methods
        List<String> fileNames = asList("DefaultGroovyMethods", "DefaultGroovyStaticMethods");
        List<JavaFileObject> src = new ArrayList<>();
        ZipArchive a = new ZipArchive((JavacFileManager) fileManager, new ZipFile(groovySrcJar));
        for (String name : fileNames) {
            src.add(a.getFileObject(new RelativeDirectory("org/codehaus/groovy/runtime"), name + ".java"));
        }
        // annotation processing appears to cause the source files to be reparsed
        // (even though I couldn't find exactly where it's done), which causes
        // Tree symbols created by the original JavacTask.parse() call to be thrown away,
        // which breaks later processing.
        // So for now, don't perform annotation processing
        List<String> options = asList("-proc:none");
        Translator t = new Translator(javac.getTask(null, fileManager, errorListener, options, null, src));
        for (String name : fileNames) {
            t.translate("org.codehaus.groovy.runtime." + name, "com.cloudbees.groovy.cps.Cps" + name, groovySrcJar.getName());
        }
        dir.mkdirs();
        t.generateTo(new FileCodeWriter(dir));
    }
}
Also used : RelativeDirectory(com.sun.tools.javac.file.RelativePath.RelativeDirectory) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) ZipArchive(com.sun.tools.javac.file.ZipArchive) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) JavaFileObject(javax.tools.JavaFileObject) ZipFile(java.util.zip.ZipFile) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)1 RelativeDirectory (com.sun.tools.javac.file.RelativePath.RelativeDirectory)1 ZipArchive (com.sun.tools.javac.file.ZipArchive)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ZipFile (java.util.zip.ZipFile)1 JavaCompiler (javax.tools.JavaCompiler)1 JavaFileObject (javax.tools.JavaFileObject)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1