1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.tools.javafx.script; |
27 | |
|
28 | |
import com.sun.tools.javafx.api.JavafxcTool; |
29 | |
import com.sun.javafx.api.*; |
30 | |
import java.io.FileReader; |
31 | |
import java.io.IOException; |
32 | |
import java.io.Writer; |
33 | |
import java.io.PrintWriter; |
34 | |
import java.io.Reader; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
import javax.tools.*; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class JavaFXScriptCompiler { |
44 | |
private JavafxcTool tool; |
45 | |
private StandardJavaFileManager stdManager; |
46 | |
private ClassLoader parentClassLoader; |
47 | |
|
48 | 26 | public JavaFXScriptCompiler(ClassLoader parent) { |
49 | 26 | parentClassLoader = parent; |
50 | 26 | tool = JavafxcTool.create(); |
51 | 26 | } |
52 | |
|
53 | |
public Map<String, byte[]> compile(String filename, String source) { |
54 | 0 | PrintWriter err = new PrintWriter(System.err); |
55 | 0 | return compile(filename, source, err, null, null); |
56 | |
} |
57 | |
|
58 | |
public Map<String, byte[]> compile(String fileName, String source, |
59 | |
Writer err) { |
60 | 0 | return compile(fileName, source, err, null, null); |
61 | |
} |
62 | |
|
63 | |
public Map<String, byte[]> compile(String fileName, String source, |
64 | |
Writer err, String sourcePath) { |
65 | 0 | return compile(fileName, source, err, sourcePath, null); |
66 | |
} |
67 | |
|
68 | |
public Map<String, byte[]> compile(String fileName, String source, |
69 | |
Writer err, String sourcePath, |
70 | |
String classPath) { |
71 | 0 | return compile(fileName, source, err, sourcePath, classPath, |
72 | |
new DiagnosticCollector<JavaFileObject>(), true); |
73 | |
} |
74 | |
|
75 | |
public Map<String, byte[]> compile(String filename) throws IOException { |
76 | 0 | String source = readFully(new FileReader(filename)); |
77 | 0 | PrintWriter err = new PrintWriter(System.err); |
78 | 0 | return compile(filename, source, err, null, null); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public Map<String, byte[]> compile(String fileName, String source, |
93 | |
Writer err, String sourcePath, String classPath, |
94 | |
DiagnosticListener<JavaFileObject> diagnostics, |
95 | |
boolean printDiagnostics) { |
96 | |
|
97 | |
|
98 | 29 | if (stdManager == null) { |
99 | 19 | stdManager = tool.getStandardFileManager(diagnostics, null, null); |
100 | |
} |
101 | 29 | MemoryFileManager manager = new MemoryFileManager(stdManager, parentClassLoader); |
102 | |
|
103 | |
|
104 | 29 | List<JavaFileObject> compUnits = new ArrayList<JavaFileObject>(1); |
105 | 29 | compUnits.add(manager.makeStringSource(fileName, source)); |
106 | |
|
107 | |
|
108 | 29 | List<String> options = new ArrayList<String>(); |
109 | 29 | options.add("-Xlint:all-unchecked"); |
110 | 29 | options.add("-g"); |
111 | 29 | options.add("-deprecation"); |
112 | 29 | if (sourcePath != null) { |
113 | 0 | options.add("-sourcepath"); |
114 | 0 | options.add(sourcePath); |
115 | |
} |
116 | 29 | if (classPath != null) { |
117 | 29 | options.add("-classpath"); |
118 | 29 | options.add(classPath); |
119 | |
} |
120 | 29 | options.add("-target"); |
121 | 29 | options.add(isJava6() ? "1.6" : "1.5"); |
122 | |
|
123 | 29 | options.add("-XDdumpfx=/tmp"); |
124 | |
|
125 | |
|
126 | 29 | JavafxcTask task = tool.getTask(err, manager, diagnostics, options, compUnits); |
127 | |
|
128 | 29 | if (task.call() == false) { |
129 | 11 | if (printDiagnostics) { |
130 | 2 | if (diagnostics instanceof DiagnosticCollector) { |
131 | 0 | printDiagnostics((DiagnosticCollector)diagnostics, err); |
132 | |
} |
133 | |
} |
134 | 11 | return null; |
135 | |
} |
136 | |
|
137 | 18 | Map<String, byte[]> classBytes = manager.getClassBytes(); |
138 | |
try { |
139 | 18 | manager.close(); |
140 | 0 | } catch (IOException exp) { |
141 | 18 | } |
142 | |
|
143 | 18 | return classBytes; |
144 | |
} |
145 | |
|
146 | |
private static boolean isJava6() { |
147 | |
try { |
148 | 29 | Class.forName("java.util.ServiceLoader"); |
149 | 29 | return true; |
150 | 0 | } catch (ClassNotFoundException e) { |
151 | 0 | return false; |
152 | |
} |
153 | |
} |
154 | |
|
155 | |
static void printDiagnostics(DiagnosticCollector<JavaFileObject> diagnostics, Writer err) { |
156 | |
|
157 | 1 | com.sun.tools.javac.util.Context context = new com.sun.tools.javac.util.Context(); |
158 | 1 | com.sun.tools.javac.util.Options options = |
159 | |
com.sun.tools.javac.util.Options.instance(context); |
160 | 1 | options.put("diags", "%l: %t%m|%p%m"); |
161 | |
|
162 | 1 | PrintWriter perr = new PrintWriter(err); |
163 | 1 | RedirectedLog log = new RedirectedLog(context, perr); |
164 | 1 | for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) { |
165 | 1 | log.report((com.sun.tools.javac.util.JCDiagnostic)diagnostic); |
166 | |
} |
167 | 1 | } |
168 | |
|
169 | |
|
170 | |
private String readFully(Reader reader) throws IOException { |
171 | 0 | char[] arr = new char[8*1024]; |
172 | 0 | StringBuilder buf = new StringBuilder(); |
173 | |
int numChars; |
174 | 0 | while ((numChars = reader.read(arr, 0, arr.length)) > 0) { |
175 | 0 | buf.append(arr, 0, numChars); |
176 | |
} |
177 | 0 | return buf.toString(); |
178 | |
} |
179 | |
|
180 | |
private static class RedirectedLog extends com.sun.tools.javac.util.Log { |
181 | |
RedirectedLog(com.sun.tools.javac.util.Context context, PrintWriter writer) { |
182 | 1 | super(context, writer); |
183 | 1 | } |
184 | |
} |
185 | |
} |