| 1 | package uk.co.zonetora.javacRunner; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.Collections; |
| 6 | |
| 7 | import javax.tools.JavaCompiler; |
| 8 | import javax.tools.JavaFileObject; |
| 9 | import javax.tools.StandardJavaFileManager; |
| 10 | |
| 11 | import uk.co.zonetora.InputParser; |
| 12 | import uk.co.zonetora.RunnableWithString; |
| 13 | |
| 14 | import com.sun.tools.javac.api.JavacTool; |
| 15 | |
| 16 | public class OpenJDKMain { |
| 17 | |
| 18 | public static void main(String[] args) throws IOException { |
| 19 | InputParser.parse(new RunnableWithString(){ |
| 20 | public String run(String arg) { |
| 21 | return runWithJavac(arg); |
| 22 | } |
| 23 | |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | public static final String CLASSNAME = new String("DummyTestClass10101").intern(); |
| 29 | |
| 30 | |
| 31 | public static String runWithJavac(String code) { |
| 32 | |
| 33 | try { |
| 34 | code = code.replace('[', '(').replace(']',')'); |
| 35 | JavaSourceFromString javaSource = new JavaSourceFromString(CLASSNAME, code); |
| 36 | Iterable<? extends JavaFileObject> compilationUnit = Collections.singleton(javaSource); |
| 37 | |
| 38 | JavaCompiler compiler = JavacTool.create(); |
| 39 | StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); |
| 40 | final CachedOutputFileManager fjf = new CachedOutputFileManager(fileManager); |
| 41 | Iterable<String> options = Arrays.asList(new String[] {"-source", "1.5", "-target", "1.5" }); |
| 42 | compiler.getTask(null, fjf, null, options, null, compilationUnit).call(); |
| 43 | } catch(Throwable t) { |
| 44 | return t.getMessage(); |
| 45 | } |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | } |