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