author | jjg |
Tue, 08 Nov 2011 17:06:58 -0800 | |
changeset 11053 | 48713f779b1d |
parent 11052 | 65b9fa7eaf55 |
child 11314 | b612aaca08d0 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8614 | 2 |
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.api; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.OutputStream; |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
31 |
import java.io.OutputStreamWriter; |
10 | 32 |
import java.io.PrintWriter; |
33 |
import java.io.Writer; |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
34 |
import java.nio.charset.Charset; |
10 | 35 |
import java.util.ArrayList; |
36 |
import java.util.Collections; |
|
37 |
import java.util.EnumSet; |
|
38 |
import java.util.Iterator; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Locale; |
|
41 |
import java.util.Set; |
|
42 |
import javax.lang.model.SourceVersion; |
|
43 |
import javax.tools.*; |
|
44 |
||
45 |
import com.sun.source.util.JavacTask; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
46 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 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; |
|
50 |
import com.sun.tools.javac.main.RecognizedOptions.GrumpyHelper; |
|
51 |
import com.sun.tools.javac.main.RecognizedOptions; |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
52 |
import com.sun.tools.javac.util.ClientCodeException; |
10 | 53 |
import com.sun.tools.javac.util.Context; |
54 |
import com.sun.tools.javac.util.Log; |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
55 |
import com.sun.tools.javac.util.Log.PrefixKind; |
10 | 56 |
import com.sun.tools.javac.util.Options; |
57 |
import com.sun.tools.javac.util.Pair; |
|
58 |
||
59 |
/** |
|
60 |
* TODO: describe com.sun.tools.javac.api.Tool |
|
61 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
62 |
* <p><b>This is NOT part of any supported API. |
10 | 63 |
* If you write code that depends on this, you do so at your own |
64 |
* risk. This code and its internal interfaces are subject to change |
|
65 |
* or deletion without notice.</b></p> |
|
66 |
* |
|
67 |
* @author Peter von der Ah\u00e9 |
|
68 |
*/ |
|
69 |
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 |
/** |
|
84 |
* Constructor used by service provider mechanism. The correct way to |
|
85 |
* obtain an instance of this class is using create or the service provider |
|
86 |
* mechanism. |
|
87 |
* @see javax.tools.JavaCompilerTool |
|
88 |
* @see javax.tools.ToolProvider |
|
89 |
* @see #create |
|
90 |
*/ |
|
91 |
@Deprecated |
|
92 |
public JavacTool() {} |
|
93 |
||
94 |
/** |
|
95 |
* Static factory method for creating new instances of this tool. |
|
96 |
* @return new instance of this tool |
|
97 |
*/ |
|
98 |
public static JavacTool create() { |
|
99 |
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) { |
|
5004
6c2694dc2da0
6929544: langtools source code uses statics qualified by instance variables
jjg
parents:
1471
diff
changeset
|
142 |
return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind)); |
10 | 143 |
} |
144 |
||
145 |
public JavacFileManager getStandardFileManager( |
|
146 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
147 |
Locale locale, |
|
148 |
Charset charset) { |
|
149 |
Context context = new Context(); |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
150 |
context.put(Locale.class, locale); |
10 | 151 |
if (diagnosticListener != null) |
152 |
context.put(DiagnosticListener.class, diagnosticListener); |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
153 |
PrintWriter pw = (charset == null) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
154 |
? new PrintWriter(System.err, true) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
155 |
: new PrintWriter(new OutputStreamWriter(System.err, charset), true); |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
156 |
context.put(Log.outKey, pw); |
10 | 157 |
return new JavacFileManager(context, true, charset); |
158 |
} |
|
159 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
160 |
@Override |
10 | 161 |
public JavacTask getTask(Writer out, |
162 |
JavaFileManager fileManager, |
|
163 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
164 |
Iterable<String> options, |
|
165 |
Iterable<String> classes, |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
166 |
Iterable<? extends JavaFileObject> compilationUnits) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
167 |
Context context = new Context(); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
168 |
return getTask(out, fileManager, diagnosticListener, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
169 |
options, classes, compilationUnits, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
170 |
context); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
171 |
} |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
172 |
|
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
173 |
public JavacTask getTask(Writer out, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
174 |
JavaFileManager fileManager, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
175 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
176 |
Iterable<String> options, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
177 |
Iterable<String> classes, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
178 |
Iterable<? extends JavaFileObject> compilationUnits, |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
179 |
Context context) |
10 | 180 |
{ |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
181 |
try { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
182 |
ClientCodeWrapper ccw = ClientCodeWrapper.instance(context); |
10 | 183 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
184 |
final String kindMsg = "All compilation units must be of SOURCE kind"; |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
185 |
if (options != null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
186 |
for (String option : options) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
187 |
option.getClass(); // null check |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
188 |
if (classes != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
189 |
for (String cls : classes) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
190 |
if (!SourceVersion.isName(cls)) // implicit null check |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
191 |
throw new IllegalArgumentException("Not a valid class name: " + cls); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
192 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
193 |
if (compilationUnits != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
194 |
compilationUnits = ccw.wrapJavaFileObjects(compilationUnits); // implicit null check |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
195 |
for (JavaFileObject cu : compilationUnits) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
196 |
if (cu.getKind() != JavaFileObject.Kind.SOURCE) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
197 |
throw new IllegalArgumentException(kindMsg); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
198 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
199 |
} |
10 | 200 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
201 |
if (diagnosticListener != null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
202 |
context.put(DiagnosticListener.class, ccw.wrap(diagnosticListener)); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
203 |
|
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
204 |
if (out == null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
205 |
context.put(Log.outKey, new PrintWriter(System.err, true)); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
206 |
else |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
207 |
context.put(Log.outKey, new PrintWriter(out, true)); |
10 | 208 |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
209 |
if (fileManager == null) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
210 |
fileManager = getStandardFileManager(diagnosticListener, null, null); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
211 |
fileManager = ccw.wrap(fileManager); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
212 |
context.put(JavaFileManager.class, fileManager); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
213 |
processOptions(context, fileManager, options); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
214 |
Main compiler = new Main("javacTask", context.get(Log.outKey)); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
215 |
return new JavacTaskImpl(compiler, options, context, classes, compilationUnits); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
216 |
} catch (ClientCodeException ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
217 |
throw new RuntimeException(ex.getCause()); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
9069
diff
changeset
|
218 |
} |
10 | 219 |
} |
220 |
||
221 |
private static void processOptions(Context context, |
|
222 |
JavaFileManager fileManager, |
|
223 |
Iterable<String> options) |
|
224 |
{ |
|
225 |
if (options == null) |
|
226 |
return; |
|
227 |
||
228 |
Options optionTable = Options.instance(context); |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
229 |
Log log = Log.instance(context); |
10 | 230 |
|
231 |
JavacOption[] recognizedOptions = |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
232 |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper(log)); |
10 | 233 |
Iterator<String> flags = options.iterator(); |
234 |
while (flags.hasNext()) { |
|
235 |
String flag = flags.next(); |
|
236 |
int j; |
|
237 |
for (j=0; j<recognizedOptions.length; j++) |
|
238 |
if (recognizedOptions[j].matches(flag)) |
|
239 |
break; |
|
240 |
||
241 |
if (j == recognizedOptions.length) { |
|
242 |
if (fileManager.handleOption(flag, flags)) { |
|
243 |
continue; |
|
244 |
} else { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
245 |
String msg = log.localize(PrefixKind.JAVAC, "err.invalid.flag", flag); |
10 | 246 |
throw new IllegalArgumentException(msg); |
247 |
} |
|
248 |
} |
|
249 |
||
250 |
JavacOption option = recognizedOptions[j]; |
|
251 |
if (option.hasArg()) { |
|
252 |
if (!flags.hasNext()) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
253 |
String msg = log.localize(PrefixKind.JAVAC, "err.req.arg", flag); |
10 | 254 |
throw new IllegalArgumentException(msg); |
255 |
} |
|
256 |
String operand = flags.next(); |
|
257 |
if (option.process(optionTable, flag, operand)) |
|
258 |
// should not happen as the GrumpyHelper will throw exceptions |
|
259 |
// in case of errors |
|
260 |
throw new IllegalArgumentException(flag + " " + operand); |
|
261 |
} else { |
|
262 |
if (option.process(optionTable, flag)) |
|
263 |
// should not happen as the GrumpyHelper will throw exceptions |
|
264 |
// in case of errors |
|
265 |
throw new IllegalArgumentException(flag); |
|
266 |
} |
|
267 |
} |
|
11052 | 268 |
|
269 |
optionTable.notifyListeners(); |
|
10 | 270 |
} |
271 |
||
272 |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
|
273 |
if (err == null) |
|
274 |
err = System.err; |
|
275 |
for (String argument : arguments) |
|
276 |
argument.getClass(); // null check |
|
277 |
return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
|
278 |
} |
|
279 |
||
280 |
public Set<SourceVersion> getSourceVersions() { |
|
281 |
return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
|
282 |
SourceVersion.latest())); |
|
283 |
} |
|
284 |
||
285 |
public int isSupportedOption(String option) { |
|
286 |
JavacOption[] recognizedOptions = |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
287 |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper(null)); |
10 | 288 |
for (JavacOption o : recognizedOptions) { |
289 |
if (o.matches(option)) |
|
290 |
return o.hasArg() ? 1 : 0; |
|
291 |
} |
|
292 |
return -1; |
|
293 |
} |
|
294 |
||
295 |
} |