23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javac.api; |
26 package com.sun.tools.javac.api; |
27 |
27 |
28 import java.io.File; |
|
29 import java.io.InputStream; |
28 import java.io.InputStream; |
30 import java.io.OutputStream; |
29 import java.io.OutputStream; |
31 import java.io.OutputStreamWriter; |
30 import java.io.OutputStreamWriter; |
32 import java.io.PrintWriter; |
31 import java.io.PrintWriter; |
33 import java.io.Writer; |
32 import java.io.Writer; |
34 import java.nio.charset.Charset; |
33 import java.nio.charset.Charset; |
35 import java.util.ArrayList; |
|
36 import java.util.Collections; |
34 import java.util.Collections; |
37 import java.util.EnumSet; |
35 import java.util.EnumSet; |
38 import java.util.Iterator; |
36 import java.util.Iterator; |
39 import java.util.List; |
|
40 import java.util.Locale; |
37 import java.util.Locale; |
41 import java.util.Set; |
38 import java.util.Set; |
42 import javax.lang.model.SourceVersion; |
39 import javax.lang.model.SourceVersion; |
43 import javax.tools.*; |
40 import javax.tools.*; |
44 |
41 |
45 import com.sun.source.util.JavacTask; |
42 import com.sun.source.util.JavacTask; |
46 import com.sun.tools.javac.file.JavacFileManager; |
43 import com.sun.tools.javac.file.JavacFileManager; |
47 import com.sun.tools.javac.main.JavacOption.OptionKind; |
|
48 import com.sun.tools.javac.main.JavacOption; |
|
49 import com.sun.tools.javac.main.Main; |
44 import com.sun.tools.javac.main.Main; |
50 import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper; |
45 import com.sun.tools.javac.main.Option; |
51 import com.sun.tools.javac.main.RecognizedOptions; |
46 import com.sun.tools.javac.main.OptionHelper; |
|
47 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; |
52 import com.sun.tools.javac.util.ClientCodeException; |
48 import com.sun.tools.javac.util.ClientCodeException; |
53 import com.sun.tools.javac.util.Context; |
49 import com.sun.tools.javac.util.Context; |
54 import com.sun.tools.javac.util.Log; |
50 import com.sun.tools.javac.util.Log; |
55 import com.sun.tools.javac.util.Log.PrefixKind; |
51 import com.sun.tools.javac.util.Log.PrefixKind; |
56 import com.sun.tools.javac.util.Options; |
52 import com.sun.tools.javac.util.Options; |
57 import com.sun.tools.javac.util.Pair; |
|
58 |
53 |
59 /** |
54 /** |
60 * TODO: describe com.sun.tools.javac.api.Tool |
55 * TODO: describe com.sun.tools.javac.api.Tool |
61 * |
56 * |
62 * <p><b>This is NOT part of any supported API. |
57 * <p><b>This is NOT part of any supported API. |
65 * or deletion without notice.</b></p> |
60 * or deletion without notice.</b></p> |
66 * |
61 * |
67 * @author Peter von der Ah\u00e9 |
62 * @author Peter von der Ah\u00e9 |
68 */ |
63 */ |
69 public final class JavacTool implements JavaCompiler { |
64 public final class JavacTool implements JavaCompiler { |
70 private final List<Pair<String,String>> options |
|
71 = new ArrayList<Pair<String,String>>(); |
|
72 private final Context dummyContext = new Context(); |
|
73 |
|
74 private final PrintWriter silent = new PrintWriter(new OutputStream(){ |
|
75 public void write(int b) {} |
|
76 }); |
|
77 |
|
78 private final Main sharedCompiler = new Main("javac", silent); |
|
79 { |
|
80 sharedCompiler.setOptions(Options.instance(dummyContext)); |
|
81 } |
|
82 |
|
83 /** |
65 /** |
84 * Constructor used by service provider mechanism. The correct way to |
66 * Constructor used by service provider mechanism. The recommended way to |
85 * obtain an instance of this class is using create or the service provider |
67 * obtain an instance of this class is by using {@link #create} or the |
86 * mechanism. |
68 * service provider mechanism. |
87 * @see javax.tools.JavaCompilerTool |
69 * @see javax.tools.JavaCompilerTool |
88 * @see javax.tools.ToolProvider |
70 * @see javax.tools.ToolProvider |
89 * @see #create |
71 * @see #create |
90 */ |
72 */ |
91 @Deprecated |
73 @Deprecated |
95 * Static factory method for creating new instances of this tool. |
77 * Static factory method for creating new instances of this tool. |
96 * @return new instance of this tool |
78 * @return new instance of this tool |
97 */ |
79 */ |
98 public static JavacTool create() { |
80 public static JavacTool create() { |
99 return new JavacTool(); |
81 return new JavacTool(); |
100 } |
|
101 |
|
102 private String argsToString(Object... args) { |
|
103 String newArgs = null; |
|
104 if (args.length > 0) { |
|
105 StringBuilder sb = new StringBuilder(); |
|
106 String separator = ""; |
|
107 for (Object arg : args) { |
|
108 sb.append(separator).append(arg.toString()); |
|
109 separator = File.pathSeparator; |
|
110 } |
|
111 newArgs = sb.toString(); |
|
112 } |
|
113 return newArgs; |
|
114 } |
|
115 |
|
116 private void setOption1(String name, OptionKind kind, Object... args) { |
|
117 String arg = argsToString(args); |
|
118 JavacOption option = sharedCompiler.getOption(name); |
|
119 if (option == null || !match(kind, option.getKind())) |
|
120 throw new IllegalArgumentException(name); |
|
121 if ((args.length != 0) != option.hasArg()) |
|
122 throw new IllegalArgumentException(name); |
|
123 if (option.hasArg()) { |
|
124 if (option.process(null, name, arg)) // FIXME |
|
125 throw new IllegalArgumentException(name); |
|
126 } else { |
|
127 if (option.process(null, name)) // FIXME |
|
128 throw new IllegalArgumentException(name); |
|
129 } |
|
130 options.add(new Pair<String,String>(name,arg)); |
|
131 } |
|
132 |
|
133 public void setOption(String name, Object... args) { |
|
134 setOption1(name, OptionKind.NORMAL, args); |
|
135 } |
|
136 |
|
137 public void setExtendedOption(String name, Object... args) { |
|
138 setOption1(name, OptionKind.EXTENDED, args); |
|
139 } |
|
140 |
|
141 private static boolean match(OptionKind clientKind, OptionKind optionKind) { |
|
142 return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind)); |
|
143 } |
82 } |
144 |
83 |
145 public JavacFileManager getStandardFileManager( |
84 public JavacFileManager getStandardFileManager( |
146 DiagnosticListener<? super JavaFileObject> diagnosticListener, |
85 DiagnosticListener<? super JavaFileObject> diagnosticListener, |
147 Locale locale, |
86 Locale locale, |
207 context.put(Log.outKey, new PrintWriter(out, true)); |
146 context.put(Log.outKey, new PrintWriter(out, true)); |
208 |
147 |
209 if (fileManager == null) |
148 if (fileManager == null) |
210 fileManager = getStandardFileManager(diagnosticListener, null, null); |
149 fileManager = getStandardFileManager(diagnosticListener, null, null); |
211 fileManager = ccw.wrap(fileManager); |
150 fileManager = ccw.wrap(fileManager); |
|
151 |
212 context.put(JavaFileManager.class, fileManager); |
152 context.put(JavaFileManager.class, fileManager); |
|
153 |
213 processOptions(context, fileManager, options); |
154 processOptions(context, fileManager, options); |
214 Main compiler = new Main("javacTask", context.get(Log.outKey)); |
155 Main compiler = new Main("javacTask", context.get(Log.outKey)); |
215 return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); |
156 return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); |
216 } catch (ClientCodeException ex) { |
157 } catch (ClientCodeException ex) { |
217 throw new RuntimeException(ex.getCause()); |
158 throw new RuntimeException(ex.getCause()); |
223 Iterable<String> options) |
164 Iterable<String> options) |
224 { |
165 { |
225 if (options == null) |
166 if (options == null) |
226 return; |
167 return; |
227 |
168 |
228 Options optionTable = Options.instance(context); |
169 final Options optionTable = Options.instance(context); |
229 Log log = Log.instance(context); |
170 Log log = Log.instance(context); |
230 |
171 |
231 JavacOption[] recognizedOptions = |
172 Option[] recognizedOptions = |
232 RecognizedOptions.getJavacToolOptions(new GrumpyHelper(log)); |
173 Option.getJavacToolOptions().toArray(new Option[0]); |
|
174 OptionHelper optionHelper = new GrumpyHelper(log) { |
|
175 @Override |
|
176 public String get(Option option) { |
|
177 return optionTable.get(option.getText()); |
|
178 } |
|
179 |
|
180 @Override |
|
181 public void put(String name, String value) { |
|
182 optionTable.put(name, value); |
|
183 } |
|
184 |
|
185 @Override |
|
186 public void remove(String name) { |
|
187 optionTable.remove(name); |
|
188 } |
|
189 }; |
|
190 |
233 Iterator<String> flags = options.iterator(); |
191 Iterator<String> flags = options.iterator(); |
234 while (flags.hasNext()) { |
192 while (flags.hasNext()) { |
235 String flag = flags.next(); |
193 String flag = flags.next(); |
236 int j; |
194 int j; |
237 for (j=0; j<recognizedOptions.length; j++) |
195 for (j=0; j<recognizedOptions.length; j++) |
245 String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag); |
203 String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag); |
246 throw new IllegalArgumentException(msg); |
204 throw new IllegalArgumentException(msg); |
247 } |
205 } |
248 } |
206 } |
249 |
207 |
250 JavacOption option = recognizedOptions[j]; |
208 Option option = recognizedOptions[j]; |
251 if (option.hasArg()) { |
209 if (option.hasArg()) { |
252 if (!flags.hasNext()) { |
210 if (!flags.hasNext()) { |
253 String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag); |
211 String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag); |
254 throw new IllegalArgumentException(msg); |
212 throw new IllegalArgumentException(msg); |
255 } |
213 } |
256 String operand = flags.next(); |
214 String operand = flags.next(); |
257 if (option.process(optionTable, flag, operand)) |
215 if (option.process(optionHelper, flag, operand)) |
258 // should not happen as the GrumpyHelper will throw exceptions |
216 // should not happen as the GrumpyHelper will throw exceptions |
259 // in case of errors |
217 // in case of errors |
260 throw new IllegalArgumentException(flag + " " + operand); |
218 throw new IllegalArgumentException(flag + " " + operand); |
261 } else { |
219 } else { |
262 if (option.process(optionTable, flag)) |
220 if (option.process(optionHelper, flag)) |
263 // should not happen as the GrumpyHelper will throw exceptions |
221 // should not happen as the GrumpyHelper will throw exceptions |
264 // in case of errors |
222 // in case of errors |
265 throw new IllegalArgumentException(flag); |
223 throw new IllegalArgumentException(flag); |
266 } |
224 } |
267 } |
225 } |
281 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
239 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
282 SourceVersion.latest())); |
240 SourceVersion.latest())); |
283 } |
241 } |
284 |
242 |
285 public int isSupportedOption(String option) { |
243 public int isSupportedOption(String option) { |
286 JavacOption[] recognizedOptions = |
244 Set<Option> recognizedOptions = Option.getJavacToolOptions(); |
287 RecognizedOptions.getJavacToolOptions(new GrumpyHelper(null)); |
245 for (Option o : recognizedOptions) { |
288 for (JavacOption o : recognizedOptions) { |
|
289 if (o.matches(option)) |
246 if (o.matches(option)) |
290 return o.hasArg() ? 1 : 0; |
247 return o.hasArg() ? 1 : 0; |
291 } |
248 } |
292 return -1; |
249 return -1; |
293 } |
250 } |