author | jjg |
Thu, 24 Mar 2011 16:14:30 -0700 | |
changeset 9069 | bcab4a29758f |
parent 8844 | 18170a311747 |
child 9071 | 88cd61b4e5aa |
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; |
|
52 |
import com.sun.tools.javac.util.Context; |
|
53 |
import com.sun.tools.javac.util.Log; |
|
54 |
import com.sun.tools.javac.util.Options; |
|
55 |
import com.sun.tools.javac.util.Pair; |
|
56 |
||
57 |
/** |
|
58 |
* TODO: describe com.sun.tools.javac.api.Tool |
|
59 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
60 |
* <p><b>This is NOT part of any supported API. |
10 | 61 |
* If you write code that depends on this, you do so at your own |
62 |
* risk. This code and its internal interfaces are subject to change |
|
63 |
* or deletion without notice.</b></p> |
|
64 |
* |
|
65 |
* @author Peter von der Ah\u00e9 |
|
66 |
*/ |
|
67 |
public final class JavacTool implements JavaCompiler { |
|
68 |
private final List<Pair<String,String>> options |
|
69 |
= new ArrayList<Pair<String,String>>(); |
|
70 |
private final Context dummyContext = new Context(); |
|
71 |
||
72 |
private final PrintWriter silent = new PrintWriter(new OutputStream(){ |
|
73 |
public void write(int b) {} |
|
74 |
}); |
|
75 |
||
76 |
private final Main sharedCompiler = new Main("javac", silent); |
|
77 |
{ |
|
78 |
sharedCompiler.setOptions(Options.instance(dummyContext)); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Constructor used by service provider mechanism. The correct way to |
|
83 |
* obtain an instance of this class is using create or the service provider |
|
84 |
* mechanism. |
|
85 |
* @see javax.tools.JavaCompilerTool |
|
86 |
* @see javax.tools.ToolProvider |
|
87 |
* @see #create |
|
88 |
*/ |
|
89 |
@Deprecated |
|
90 |
public JavacTool() {} |
|
91 |
||
92 |
/** |
|
93 |
* Static factory method for creating new instances of this tool. |
|
94 |
* @return new instance of this tool |
|
95 |
*/ |
|
96 |
public static JavacTool create() { |
|
97 |
return new JavacTool(); |
|
98 |
} |
|
99 |
||
100 |
private String argsToString(Object... args) { |
|
101 |
String newArgs = null; |
|
102 |
if (args.length > 0) { |
|
103 |
StringBuilder sb = new StringBuilder(); |
|
104 |
String separator = ""; |
|
105 |
for (Object arg : args) { |
|
106 |
sb.append(separator).append(arg.toString()); |
|
107 |
separator = File.pathSeparator; |
|
108 |
} |
|
109 |
newArgs = sb.toString(); |
|
110 |
} |
|
111 |
return newArgs; |
|
112 |
} |
|
113 |
||
114 |
private void setOption1(String name, OptionKind kind, Object... args) { |
|
115 |
String arg = argsToString(args); |
|
116 |
JavacOption option = sharedCompiler.getOption(name); |
|
117 |
if (option == null || !match(kind, option.getKind())) |
|
118 |
throw new IllegalArgumentException(name); |
|
119 |
if ((args.length != 0) != option.hasArg()) |
|
120 |
throw new IllegalArgumentException(name); |
|
121 |
if (option.hasArg()) { |
|
122 |
if (option.process(null, name, arg)) // FIXME |
|
123 |
throw new IllegalArgumentException(name); |
|
124 |
} else { |
|
125 |
if (option.process(null, name)) // FIXME |
|
126 |
throw new IllegalArgumentException(name); |
|
127 |
} |
|
128 |
options.add(new Pair<String,String>(name,arg)); |
|
129 |
} |
|
130 |
||
131 |
public void setOption(String name, Object... args) { |
|
132 |
setOption1(name, OptionKind.NORMAL, args); |
|
133 |
} |
|
134 |
||
135 |
public void setExtendedOption(String name, Object... args) { |
|
136 |
setOption1(name, OptionKind.EXTENDED, args); |
|
137 |
} |
|
138 |
||
139 |
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
|
140 |
return (clientKind == (optionKind == OptionKind.HIDDEN ? OptionKind.EXTENDED : optionKind)); |
10 | 141 |
} |
142 |
||
143 |
public JavacFileManager getStandardFileManager( |
|
144 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
145 |
Locale locale, |
|
146 |
Charset charset) { |
|
147 |
Context context = new Context(); |
|
9069
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
148 |
context.put(Locale.class, locale); |
10 | 149 |
if (diagnosticListener != null) |
150 |
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
|
151 |
PrintWriter pw = (charset == null) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
152 |
? new PrintWriter(System.err, true) |
bcab4a29758f
6597678: JavaCompiler.getStandardFileManager always uses default charset not the one that user specifies
jjg
parents:
8844
diff
changeset
|
153 |
: 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
|
154 |
context.put(Log.outKey, pw); |
10 | 155 |
return new JavacFileManager(context, true, charset); |
156 |
} |
|
157 |
||
158 |
public JavacTask getTask(Writer out, |
|
159 |
JavaFileManager fileManager, |
|
160 |
DiagnosticListener<? super JavaFileObject> diagnosticListener, |
|
161 |
Iterable<String> options, |
|
162 |
Iterable<String> classes, |
|
163 |
Iterable<? extends JavaFileObject> compilationUnits) |
|
164 |
{ |
|
165 |
final String kindMsg = "All compilation units must be of SOURCE kind"; |
|
166 |
if (options != null) |
|
167 |
for (String option : options) |
|
168 |
option.getClass(); // null check |
|
169 |
if (classes != null) { |
|
170 |
for (String cls : classes) |
|
171 |
if (!SourceVersion.isName(cls)) // implicit null check |
|
172 |
throw new IllegalArgumentException("Not a valid class name: " + cls); |
|
173 |
} |
|
174 |
if (compilationUnits != null) { |
|
175 |
for (JavaFileObject cu : compilationUnits) { |
|
176 |
if (cu.getKind() != JavaFileObject.Kind.SOURCE) // implicit null check |
|
177 |
throw new IllegalArgumentException(kindMsg); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
Context context = new Context(); |
|
182 |
||
183 |
if (diagnosticListener != null) |
|
184 |
context.put(DiagnosticListener.class, diagnosticListener); |
|
185 |
||
186 |
if (out == null) |
|
187 |
context.put(Log.outKey, new PrintWriter(System.err, true)); |
|
188 |
else |
|
189 |
context.put(Log.outKey, new PrintWriter(out, true)); |
|
190 |
||
191 |
if (fileManager == null) |
|
192 |
fileManager = getStandardFileManager(diagnosticListener, null, null); |
|
193 |
context.put(JavaFileManager.class, fileManager); |
|
194 |
processOptions(context, fileManager, options); |
|
195 |
Main compiler = new Main("javacTask", context.get(Log.outKey)); |
|
196 |
return new JavacTaskImpl(this, compiler, options, context, classes, compilationUnits); |
|
197 |
} |
|
198 |
||
199 |
private static void processOptions(Context context, |
|
200 |
JavaFileManager fileManager, |
|
201 |
Iterable<String> options) |
|
202 |
{ |
|
203 |
if (options == null) |
|
204 |
return; |
|
205 |
||
206 |
Options optionTable = Options.instance(context); |
|
207 |
||
208 |
JavacOption[] recognizedOptions = |
|
209 |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
|
210 |
Iterator<String> flags = options.iterator(); |
|
211 |
while (flags.hasNext()) { |
|
212 |
String flag = flags.next(); |
|
213 |
int j; |
|
214 |
for (j=0; j<recognizedOptions.length; j++) |
|
215 |
if (recognizedOptions[j].matches(flag)) |
|
216 |
break; |
|
217 |
||
218 |
if (j == recognizedOptions.length) { |
|
219 |
if (fileManager.handleOption(flag, flags)) { |
|
220 |
continue; |
|
221 |
} else { |
|
222 |
String msg = Main.getLocalizedString("err.invalid.flag", flag); |
|
223 |
throw new IllegalArgumentException(msg); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
JavacOption option = recognizedOptions[j]; |
|
228 |
if (option.hasArg()) { |
|
229 |
if (!flags.hasNext()) { |
|
230 |
String msg = Main.getLocalizedString("err.req.arg", flag); |
|
231 |
throw new IllegalArgumentException(msg); |
|
232 |
} |
|
233 |
String operand = flags.next(); |
|
234 |
if (option.process(optionTable, flag, operand)) |
|
235 |
// should not happen as the GrumpyHelper will throw exceptions |
|
236 |
// in case of errors |
|
237 |
throw new IllegalArgumentException(flag + " " + operand); |
|
238 |
} else { |
|
239 |
if (option.process(optionTable, flag)) |
|
240 |
// should not happen as the GrumpyHelper will throw exceptions |
|
241 |
// in case of errors |
|
242 |
throw new IllegalArgumentException(flag); |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { |
|
248 |
if (err == null) |
|
249 |
err = System.err; |
|
250 |
for (String argument : arguments) |
|
251 |
argument.getClass(); // null check |
|
252 |
return com.sun.tools.javac.Main.compile(arguments, new PrintWriter(err, true)); |
|
253 |
} |
|
254 |
||
255 |
public Set<SourceVersion> getSourceVersions() { |
|
256 |
return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, |
|
257 |
SourceVersion.latest())); |
|
258 |
} |
|
259 |
||
260 |
public int isSupportedOption(String option) { |
|
261 |
JavacOption[] recognizedOptions = |
|
262 |
RecognizedOptions.getJavacToolOptions(new GrumpyHelper()); |
|
263 |
for (JavacOption o : recognizedOptions) { |
|
264 |
if (o.matches(option)) |
|
265 |
return o.hasArg() ? 1 : 0; |
|
266 |
} |
|
267 |
return -1; |
|
268 |
} |
|
269 |
||
270 |
} |