|
| 1 | +package com.cloudbees.groovy.cps.tool; |
| 2 | + |
| 3 | +import com.sun.codemodel.writer.FileCodeWriter; |
| 4 | +import groovy.lang.GroovyShell; |
| 5 | +import hudson.remoting.Which; |
| 6 | + |
| 7 | +import javax.tools.DiagnosticListener; |
| 8 | +import javax.tools.JavaCompiler; |
| 9 | +import javax.tools.JavaFileObject; |
| 10 | +import javax.tools.StandardJavaFileManager; |
| 11 | +import javax.tools.StandardLocation; |
| 12 | +import javax.tools.ToolProvider; |
| 13 | +import java.io.File; |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Locale; |
| 21 | + |
| 22 | +import static java.util.Arrays.*; |
| 23 | + |
| 24 | +public class Driver { |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + new Driver().run(new File(args[0])); |
| 27 | + } |
| 28 | + |
| 29 | + public void run(File dir) throws Exception { |
| 30 | + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); |
| 31 | + DiagnosticListener<JavaFileObject> errorListener = createErrorListener(); |
| 32 | + |
| 33 | + try (StandardJavaFileManager fileManager = javac.getStandardFileManager(errorListener, Locale.getDefault(), Charset.defaultCharset())) { |
| 34 | + fileManager.setLocation(StandardLocation.CLASS_PATH, |
| 35 | + Collections.singleton(Which.jarFile(GroovyShell.class))); |
| 36 | + |
| 37 | + File groovySrcJar = Which.jarFile(Driver.class.getClassLoader().getResource("groovy/lang/GroovyShell.java")); |
| 38 | + |
| 39 | + // classes to translate |
| 40 | + // TODO include other classes mentioned in DefaultGroovyMethods.DGM_LIKE_CLASSES if they have any applicable methods |
| 41 | + List<String> fileNames = asList("DefaultGroovyMethods", |
| 42 | + "DefaultGroovyStaticMethods", |
| 43 | + "StringGroovyMethods"); |
| 44 | + |
| 45 | + List<JavaFileObject> src = new ArrayList<>(); |
| 46 | + for (JavaFileObject jfo : fileManager.list(StandardLocation.CLASS_PATH, "org.codehaus.groovy.runtime", Collections.singleton(JavaFileObject.Kind.SOURCE), true)) { |
| 47 | + for (String name : fileNames) { |
| 48 | + if (jfo.toUri().toString().endsWith("/org/codehaus/groovy/runtime/" + name + ".java")) { |
| 49 | + src.add(jfo); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // annotation processing appears to cause the source files to be reparsed |
| 56 | + // (even though I couldn't find exactly where it's done), which causes |
| 57 | + // Tree symbols created by the original JavacTask.parse() call to be thrown away, |
| 58 | + // which breaks later processing. |
| 59 | + // So for now, don't perform annotation processing |
| 60 | + List<String> options = asList("-proc:none"); |
| 61 | + |
| 62 | + Translator t = new Translator(javac.getTask(null, fileManager, errorListener, options, null, src)); |
| 63 | + |
| 64 | + for (String name : fileNames) { |
| 65 | + t.translate( |
| 66 | + "org.codehaus.groovy.runtime."+name, |
| 67 | + "com.cloudbees.groovy.cps.Cps"+name, |
| 68 | + groovySrcJar.getName()); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + Files.createDirectories(dir.toPath()); |
| 73 | + t.generateTo(new FileCodeWriter(dir)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private DiagnosticListener<JavaFileObject> createErrorListener() { |
| 78 | + return System.out::println; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments