author | jjg |
Mon, 19 Nov 2012 11:38:49 -0800 | |
changeset 14548 | aa687b312c97 |
parent 14448 | 91aa6570b59a |
child 14954 | f8eea2960bcb |
permissions | -rw-r--r-- |
10 | 1 |
/* |
13631
dc1212c348f9
7191449: update copyright year to match last edit in jdk8 langtools repository
jjh
parents:
11708
diff
changeset
|
2 |
* Copyright (c) 1999, 2012, 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.main; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.PrintWriter; |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
31 |
import java.net.URL; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
32 |
import java.security.DigestInputStream; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
33 |
import java.security.MessageDigest; |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
34 |
import java.util.Arrays; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
35 |
import java.util.Collection; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
36 |
import java.util.Iterator; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
37 |
import java.util.LinkedHashSet; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
38 |
import java.util.ServiceLoader; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
39 |
import java.util.Set; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
40 |
|
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
41 |
import javax.annotation.processing.Processor; |
6721 | 42 |
import javax.tools.JavaFileManager; |
43 |
import javax.tools.JavaFileObject; |
|
10 | 44 |
|
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
45 |
import com.sun.source.util.JavacTask; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
46 |
import com.sun.source.util.Plugin; |
10 | 47 |
import com.sun.tools.javac.code.Source; |
1208 | 48 |
import com.sun.tools.javac.file.CacheFSInfo; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
10
diff
changeset
|
49 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 50 |
import com.sun.tools.javac.jvm.Target; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
51 |
import com.sun.tools.javac.processing.AnnotationProcessingError; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
52 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
10 | 53 |
import com.sun.tools.javac.util.*; |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
54 |
import com.sun.tools.javac.util.Log.PrefixKind; |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
55 |
import com.sun.tools.javac.util.Log.WriterKind; |
11314 | 56 |
import static com.sun.tools.javac.main.Option.*; |
10 | 57 |
|
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
58 |
/** This class provides a command line interface to the javac compiler. |
10 | 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. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
61 |
* If you write code that depends on this, you do so at your own risk. |
10 | 62 |
* This code and its internal interfaces are subject to change or |
63 |
* deletion without notice.</b> |
|
64 |
*/ |
|
65 |
public class Main { |
|
66 |
||
67 |
/** The name of the compiler, for use in diagnostics. |
|
68 |
*/ |
|
69 |
String ownName; |
|
70 |
||
71 |
/** The writer to use for diagnostic output. |
|
72 |
*/ |
|
73 |
PrintWriter out; |
|
74 |
||
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
75 |
/** The log to use for diagnostic output. |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
76 |
*/ |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
77 |
Log log; |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
78 |
|
10 | 79 |
/** |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
80 |
* If true, certain errors will cause an exception, such as command line |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
81 |
* arg errors, or exceptions in user provided code. |
10 | 82 |
*/ |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
83 |
boolean apiMode; |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
84 |
|
10 | 85 |
|
86 |
/** Result codes. |
|
87 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
88 |
public enum Result { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
89 |
OK(0), // Compilation completed with no errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
90 |
ERROR(1), // Completed but reported errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
91 |
CMDERR(2), // Bad command-line arguments |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
92 |
SYSERR(3), // System error or resource exhaustion. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
93 |
ABNORMAL(4); // Compiler terminated abnormally |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
94 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
95 |
Result(int exitCode) { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
96 |
this.exitCode = exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
97 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
98 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
99 |
public boolean isOK() { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
100 |
return (exitCode == 0); |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
101 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
102 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
103 |
public final int exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
104 |
} |
10 | 105 |
|
11314 | 106 |
private Option[] recognizedOptions = |
107 |
Option.getJavaCompilerOptions().toArray(new Option[0]); |
|
10 | 108 |
|
11314 | 109 |
private OptionHelper optionHelper = new OptionHelper() { |
110 |
@Override |
|
111 |
public String get(Option option) { |
|
112 |
return options.get(option); |
|
113 |
} |
|
114 |
||
115 |
@Override |
|
116 |
public void put(String name, String value) { |
|
117 |
options.put(name, value); |
|
10 | 118 |
} |
119 |
||
11314 | 120 |
@Override |
121 |
public void remove(String name) { |
|
122 |
options.remove(name); |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public Log getLog() { |
|
127 |
return log; |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public String getOwnName() { |
|
132 |
return ownName; |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
10 | 136 |
public void error(String key, Object... args) { |
137 |
Main.this.error(key, args); |
|
138 |
} |
|
139 |
||
11314 | 140 |
@Override |
10 | 141 |
public void addFile(File f) { |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
142 |
filenames.add(f); |
10 | 143 |
} |
144 |
||
11314 | 145 |
@Override |
10 | 146 |
public void addClassName(String s) { |
147 |
classnames.append(s); |
|
148 |
} |
|
149 |
||
11314 | 150 |
}; |
10 | 151 |
|
152 |
/** |
|
153 |
* Construct a compiler instance. |
|
154 |
*/ |
|
155 |
public Main(String name) { |
|
156 |
this(name, new PrintWriter(System.err, true)); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Construct a compiler instance. |
|
161 |
*/ |
|
162 |
public Main(String name, PrintWriter out) { |
|
163 |
this.ownName = name; |
|
164 |
this.out = out; |
|
165 |
} |
|
166 |
/** A table of all options that's passed to the JavaCompiler constructor. */ |
|
167 |
private Options options = null; |
|
168 |
||
169 |
/** The list of source files to process |
|
170 |
*/ |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
171 |
public Set<File> filenames = null; // XXX sb protected |
10 | 172 |
|
173 |
/** List of class files names passed on the command line |
|
174 |
*/ |
|
175 |
public ListBuffer<String> classnames = null; // XXX sb protected |
|
176 |
||
177 |
/** Report a usage error. |
|
178 |
*/ |
|
179 |
void error(String key, Object... args) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
180 |
if (apiMode) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
181 |
String msg = log.localize(PrefixKind.JAVAC, key, args); |
10 | 182 |
throw new PropagatedException(new IllegalStateException(msg)); |
183 |
} |
|
184 |
warning(key, args); |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
185 |
log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); |
10 | 186 |
} |
187 |
||
188 |
/** Report a warning. |
|
189 |
*/ |
|
190 |
void warning(String key, Object... args) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
191 |
log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); |
10 | 192 |
} |
193 |
||
194 |
public Option getOption(String flag) { |
|
195 |
for (Option option : recognizedOptions) { |
|
196 |
if (option.matches(flag)) |
|
197 |
return option; |
|
198 |
} |
|
199 |
return null; |
|
200 |
} |
|
201 |
||
202 |
public void setOptions(Options options) { |
|
203 |
if (options == null) |
|
204 |
throw new NullPointerException(); |
|
205 |
this.options = options; |
|
206 |
} |
|
207 |
||
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
208 |
public void setAPIMode(boolean apiMode) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
209 |
this.apiMode = apiMode; |
10 | 210 |
} |
211 |
||
212 |
/** Process command line arguments: store all command line options |
|
213 |
* in `options' table and return all source filenames. |
|
214 |
* @param flags The array of command line arguments. |
|
215 |
*/ |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
216 |
public Collection<File> processArgs(String[] flags) { // XXX sb protected |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
217 |
return processArgs(flags, null); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
218 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
219 |
|
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
220 |
public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected |
10 | 221 |
int ac = 0; |
222 |
while (ac < flags.length) { |
|
223 |
String flag = flags[ac]; |
|
224 |
ac++; |
|
225 |
||
226 |
Option option = null; |
|
227 |
||
228 |
if (flag.length() > 0) { |
|
229 |
// quick hack to speed up file processing: |
|
230 |
// if the option does not begin with '-', there is no need to check |
|
231 |
// most of the compiler options. |
|
232 |
int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; |
|
233 |
for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) { |
|
234 |
if (recognizedOptions[j].matches(flag)) { |
|
235 |
option = recognizedOptions[j]; |
|
236 |
break; |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
if (option == null) { |
|
242 |
error("err.invalid.flag", flag); |
|
243 |
return null; |
|
244 |
} |
|
245 |
||
246 |
if (option.hasArg()) { |
|
247 |
if (ac == flags.length) { |
|
248 |
error("err.req.arg", flag); |
|
249 |
return null; |
|
250 |
} |
|
251 |
String operand = flags[ac]; |
|
252 |
ac++; |
|
11314 | 253 |
if (option.process(optionHelper, flag, operand)) |
10 | 254 |
return null; |
255 |
} else { |
|
11314 | 256 |
if (option.process(optionHelper, flag)) |
10 | 257 |
return null; |
258 |
} |
|
259 |
} |
|
260 |
||
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
261 |
if (this.classnames != null && classNames != null) { |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
262 |
this.classnames.addAll(Arrays.asList(classNames)); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
263 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
264 |
|
6721 | 265 |
if (!checkDirectory(D)) |
10 | 266 |
return null; |
6721 | 267 |
if (!checkDirectory(S)) |
10 | 268 |
return null; |
269 |
||
6721 | 270 |
String sourceString = options.get(SOURCE); |
10 | 271 |
Source source = (sourceString != null) |
272 |
? Source.lookup(sourceString) |
|
273 |
: Source.DEFAULT; |
|
6721 | 274 |
String targetString = options.get(TARGET); |
10 | 275 |
Target target = (targetString != null) |
276 |
? Target.lookup(targetString) |
|
277 |
: Target.DEFAULT; |
|
278 |
// We don't check source/target consistency for CLDC, as J2ME |
|
279 |
// profiles are not aligned with J2SE targets; moreover, a |
|
280 |
// single CLDC target may have many profiles. In addition, |
|
281 |
// this is needed for the continued functioning of the JSR14 |
|
282 |
// prototype. |
|
283 |
if (Character.isDigit(target.name.charAt(0))) { |
|
284 |
if (target.compareTo(source.requiredTarget()) < 0) { |
|
285 |
if (targetString != null) { |
|
286 |
if (sourceString == null) { |
|
287 |
warning("warn.target.default.source.conflict", |
|
288 |
targetString, |
|
289 |
source.requiredTarget().name); |
|
290 |
} else { |
|
291 |
warning("warn.source.target.conflict", |
|
292 |
sourceString, |
|
293 |
source.requiredTarget().name); |
|
294 |
} |
|
295 |
return null; |
|
296 |
} else { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
297 |
target = source.requiredTarget(); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
298 |
options.put("-target", target.name); |
10 | 299 |
} |
300 |
} else { |
|
301 |
if (targetString == null && !source.allowGenerics()) { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
302 |
target = Target.JDK1_4; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
303 |
options.put("-target", target.name); |
10 | 304 |
} |
305 |
} |
|
306 |
} |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
307 |
|
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
308 |
// handle this here so it works even if no other options given |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
309 |
String showClass = options.get("showClass"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
310 |
if (showClass != null) { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
311 |
if (showClass.equals("showClass")) // no value given for option |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
312 |
showClass = "com.sun.tools.javac.Main"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
313 |
showClass(showClass); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
314 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
315 |
|
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
316 |
options.notifyListeners(); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
317 |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
318 |
return filenames; |
10 | 319 |
} |
320 |
// where |
|
11314 | 321 |
private boolean checkDirectory(Option option) { |
322 |
String value = options.get(option); |
|
10 | 323 |
if (value == null) |
324 |
return true; |
|
325 |
File file = new File(value); |
|
326 |
if (!file.exists()) { |
|
327 |
error("err.dir.not.found", value); |
|
328 |
return false; |
|
329 |
} |
|
330 |
if (!file.isDirectory()) { |
|
331 |
error("err.file.not.directory", value); |
|
332 |
return false; |
|
333 |
} |
|
334 |
return true; |
|
335 |
} |
|
336 |
||
337 |
/** Programmatic interface for main function. |
|
338 |
* @param args The command line parameters. |
|
339 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
340 |
public Result compile(String[] args) { |
10 | 341 |
Context context = new Context(); |
342 |
JavacFileManager.preRegister(context); // can't create it until Log has been set up |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
343 |
Result result = compile(args, context); |
10 | 344 |
if (fileManager instanceof JavacFileManager) { |
345 |
// A fresh context was created above, so jfm must be a JavacFileManager |
|
346 |
((JavacFileManager)fileManager).close(); |
|
347 |
} |
|
348 |
return result; |
|
349 |
} |
|
350 |
||
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
351 |
public Result compile(String[] args, Context context) { |
10 | 352 |
return compile(args, context, List.<JavaFileObject>nil(), null); |
353 |
} |
|
354 |
||
355 |
/** Programmatic interface for main function. |
|
356 |
* @param args The command line parameters. |
|
357 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
358 |
public Result compile(String[] args, |
10 | 359 |
Context context, |
360 |
List<JavaFileObject> fileObjects, |
|
361 |
Iterable<? extends Processor> processors) |
|
362 |
{ |
|
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
363 |
return compile(args, null, context, fileObjects, processors); |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
364 |
} |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
365 |
|
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
366 |
public Result compile(String[] args, |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
367 |
String[] classNames, |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
368 |
Context context, |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
369 |
List<JavaFileObject> fileObjects, |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
370 |
Iterable<? extends Processor> processors) |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
371 |
{ |
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
372 |
context.put(Log.outKey, out); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
373 |
log = Log.instance(context); |
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
374 |
|
10 | 375 |
if (options == null) |
376 |
options = Options.instance(context); // creates a new one |
|
377 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
378 |
filenames = new LinkedHashSet<File>(); |
10 | 379 |
classnames = new ListBuffer<String>(); |
380 |
JavaCompiler comp = null; |
|
381 |
/* |
|
382 |
* TODO: Logic below about what is an acceptable command line |
|
383 |
* should be updated to take annotation processing semantics |
|
384 |
* into account. |
|
385 |
*/ |
|
386 |
try { |
|
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
387 |
if (args.length == 0 |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
388 |
&& (classNames == null || classNames.length == 0) |
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
389 |
&& fileObjects.isEmpty()) { |
11314 | 390 |
Option.HELP.process(optionHelper, "-help"); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
391 |
return Result.CMDERR; |
10 | 392 |
} |
393 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
394 |
Collection<File> files; |
10 | 395 |
try { |
11708
f83264d62af2
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
jjh
parents:
11314
diff
changeset
|
396 |
files = processArgs(CommandLine.parse(args), classNames); |
1866 | 397 |
if (files == null) { |
10 | 398 |
// null signals an error in options, abort |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
399 |
return Result.CMDERR; |
1866 | 400 |
} else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { |
10 | 401 |
// it is allowed to compile nothing if just asking for help or version info |
6721 | 402 |
if (options.isSet(HELP) |
403 |
|| options.isSet(X) |
|
404 |
|| options.isSet(VERSION) |
|
405 |
|| options.isSet(FULLVERSION)) |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
406 |
return Result.OK; |
8623
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
407 |
if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
408 |
error("err.no.source.files.classes"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
409 |
} else { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
410 |
error("err.no.source.files"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
411 |
} |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
412 |
return Result.CMDERR; |
10 | 413 |
} |
414 |
} catch (java.io.FileNotFoundException e) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
415 |
warning("err.file.not.found", e.getMessage()); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
416 |
return Result.SYSERR; |
10 | 417 |
} |
418 |
||
6721 | 419 |
boolean forceStdOut = options.isSet("stdout"); |
10 | 420 |
if (forceStdOut) { |
11052
65b9fa7eaf55
7108668: allow Log to be initialized and used earlier
jjg
parents:
10638
diff
changeset
|
421 |
log.flush(); |
11314 | 422 |
log.setWriters(new PrintWriter(System.out, true)); |
10 | 423 |
} |
424 |
||
1208 | 425 |
// allow System property in following line as a Mustang legacy |
6721 | 426 |
boolean batchMode = (options.isUnset("nonBatchMode") |
1208 | 427 |
&& System.getProperty("nonBatchMode") == null); |
428 |
if (batchMode) |
|
429 |
CacheFSInfo.preRegister(context); |
|
430 |
||
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
431 |
// invoke any available plugins |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
432 |
String plugins = options.get(PLUGIN); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
433 |
if (plugins != null) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
434 |
JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
435 |
ClassLoader cl = pEnv.getProcessorClassLoader(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
436 |
ServiceLoader<Plugin> sl = ServiceLoader.load(Plugin.class, cl); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
437 |
Set<List<String>> pluginsToCall = new LinkedHashSet<List<String>>(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
438 |
for (String plugin: plugins.split("\\x00")) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
439 |
pluginsToCall.add(List.from(plugin.split("\\s+"))); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
440 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
441 |
JavacTask task = null; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
442 |
Iterator<Plugin> iter = sl.iterator(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
443 |
while (iter.hasNext()) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
444 |
Plugin plugin = iter.next(); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
445 |
for (List<String> p: pluginsToCall) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
446 |
if (plugin.getName().equals(p.head)) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
447 |
pluginsToCall.remove(p); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
448 |
try { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
449 |
if (task == null) |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
450 |
task = JavacTask.instance(pEnv); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
451 |
plugin.call(task, p.tail.toArray(new String[p.tail.size()])); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
452 |
} catch (Throwable ex) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
453 |
if (apiMode) |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
454 |
throw new RuntimeException(ex); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
455 |
pluginMessage(ex); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
456 |
return Result.SYSERR; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
457 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
458 |
|
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
459 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
460 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
461 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
462 |
for (List<String> p: pluginsToCall) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
463 |
log.printLines(PrefixKind.JAVAC, "msg.plugin.not.found", p.head); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
464 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
465 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
466 |
|
10 | 467 |
fileManager = context.get(JavaFileManager.class); |
468 |
||
469 |
comp = JavaCompiler.instance(context); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
470 |
if (comp == null) return Result.SYSERR; |
10 | 471 |
|
1866 | 472 |
if (!files.isEmpty()) { |
10 | 473 |
// add filenames to fileObjects |
474 |
comp = JavaCompiler.instance(context); |
|
475 |
List<JavaFileObject> otherFiles = List.nil(); |
|
476 |
JavacFileManager dfm = (JavacFileManager)fileManager; |
|
1866 | 477 |
for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) |
10 | 478 |
otherFiles = otherFiles.prepend(fo); |
479 |
for (JavaFileObject fo : otherFiles) |
|
480 |
fileObjects = fileObjects.prepend(fo); |
|
481 |
} |
|
482 |
comp.compile(fileObjects, |
|
483 |
classnames.toList(), |
|
484 |
processors); |
|
485 |
||
1866 | 486 |
if (log.expectDiagKeys != null) { |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6721
diff
changeset
|
487 |
if (log.expectDiagKeys.isEmpty()) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
488 |
log.printRawLines("all expected diagnostics found"); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
489 |
return Result.OK; |
1866 | 490 |
} else { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
491 |
log.printRawLines("expected diagnostic keys not found: " + log.expectDiagKeys); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
492 |
return Result.ERROR; |
1866 | 493 |
} |
494 |
} |
|
495 |
||
1996 | 496 |
if (comp.errorCount() != 0) |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
497 |
return Result.ERROR; |
10 | 498 |
} catch (IOException ex) { |
499 |
ioMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
500 |
return Result.SYSERR; |
10 | 501 |
} catch (OutOfMemoryError ex) { |
502 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
503 |
return Result.SYSERR; |
10 | 504 |
} catch (StackOverflowError ex) { |
505 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
506 |
return Result.SYSERR; |
10 | 507 |
} catch (FatalError ex) { |
508 |
feMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
509 |
return Result.SYSERR; |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
510 |
} catch (AnnotationProcessingError ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
511 |
if (apiMode) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
512 |
throw new RuntimeException(ex.getCause()); |
10 | 513 |
apMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
514 |
return Result.SYSERR; |
10 | 515 |
} catch (ClientCodeException ex) { |
516 |
// as specified by javax.tools.JavaCompiler#getTask |
|
517 |
// and javax.tools.JavaCompiler.CompilationTask#call |
|
518 |
throw new RuntimeException(ex.getCause()); |
|
519 |
} catch (PropagatedException ex) { |
|
520 |
throw ex.getCause(); |
|
521 |
} catch (Throwable ex) { |
|
522 |
// Nasty. If we've already reported an error, compensate |
|
523 |
// for buggy compiler error recovery by swallowing thrown |
|
524 |
// exceptions. |
|
525 |
if (comp == null || comp.errorCount() == 0 || |
|
6721 | 526 |
options == null || options.isSet("dev")) |
10 | 527 |
bugMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
528 |
return Result.ABNORMAL; |
10 | 529 |
} finally { |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
530 |
if (comp != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
531 |
try { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
532 |
comp.close(); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
533 |
} catch (ClientCodeException ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
534 |
throw new RuntimeException(ex.getCause()); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
535 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
536 |
} |
10 | 537 |
filenames = null; |
538 |
options = null; |
|
539 |
} |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
540 |
return Result.OK; |
10 | 541 |
} |
542 |
||
543 |
/** Print a message reporting an internal error. |
|
544 |
*/ |
|
545 |
void bugMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
546 |
log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version()); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
547 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 548 |
} |
549 |
||
6581 | 550 |
/** Print a message reporting a fatal error. |
10 | 551 |
*/ |
552 |
void feMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
553 |
log.printRawLines(ex.getMessage()); |
6721 | 554 |
if (ex.getCause() != null && options.isSet("dev")) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
555 |
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
6581 | 556 |
} |
10 | 557 |
} |
558 |
||
559 |
/** Print a message reporting an input/output error. |
|
560 |
*/ |
|
561 |
void ioMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
562 |
log.printLines(PrefixKind.JAVAC, "msg.io"); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
563 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 564 |
} |
565 |
||
566 |
/** Print a message reporting an out-of-resources error. |
|
567 |
*/ |
|
568 |
void resourceMessage(Throwable ex) { |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
569 |
log.printLines(PrefixKind.JAVAC, "msg.resource"); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
570 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 571 |
} |
572 |
||
573 |
/** Print a message reporting an uncaught exception from an |
|
574 |
* annotation processor. |
|
575 |
*/ |
|
576 |
void apMessage(AnnotationProcessingError ex) { |
|
14448 | 577 |
log.printLines(PrefixKind.JAVAC, "msg.proc.annotation.uncaught.exception"); |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
578 |
ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE)); |
10 | 579 |
} |
580 |
||
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
581 |
/** Print a message reporting an uncaught exception from an |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
582 |
* annotation processor. |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
583 |
*/ |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
584 |
void pluginMessage(Throwable ex) { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
585 |
log.printLines(PrefixKind.JAVAC, "msg.plugin.uncaught.exception"); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
586 |
ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
587 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14448
diff
changeset
|
588 |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
589 |
/** Display the location and checksum of a class. */ |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
590 |
void showClass(String className) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
591 |
PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
592 |
pw.println("javac: show class: " + className); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
593 |
URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
594 |
if (url == null) |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
595 |
pw.println(" class not found"); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
596 |
else { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
597 |
pw.println(" " + url); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
598 |
try { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
599 |
final String algorithm = "MD5"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
600 |
byte[] digest; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
601 |
MessageDigest md = MessageDigest.getInstance(algorithm); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
602 |
DigestInputStream in = new DigestInputStream(url.openStream(), md); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
603 |
try { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
604 |
byte[] buf = new byte[8192]; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
605 |
int n; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
606 |
do { n = in.read(buf); } while (n > 0); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
607 |
digest = md.digest(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
608 |
} finally { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
609 |
in.close(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
610 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
611 |
StringBuilder sb = new StringBuilder(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
612 |
for (byte b: digest) |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
613 |
sb.append(String.format("%02x", b)); |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
614 |
pw.println(" " + algorithm + " checksum: " + sb); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
615 |
} catch (Exception e) { |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
616 |
pw.println(" cannot compute digest: " + e); |
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
617 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
618 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
619 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
620 |
|
10 | 621 |
private JavaFileManager fileManager; |
622 |
||
623 |
/* ************************************************************************ |
|
624 |
* Internationalization |
|
625 |
*************************************************************************/ |
|
626 |
||
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
627 |
// /** Find a localized string in the resource bundle. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
628 |
// * @param key The key for the localized string. |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
629 |
// */ |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
630 |
// public static String getLocalizedString(String key, Object... args) { // FIXME sb private |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
631 |
// try { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
632 |
// if (messages == null) |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
633 |
// messages = new JavacMessages(javacBundleName); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
634 |
// return messages.getLocalizedString("javac." + key, args); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
635 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
636 |
// catch (MissingResourceException e) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
637 |
// throw new Error("Fatal Error: Resource for javac is missing", e); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
638 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
639 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
640 |
// |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
641 |
// public static void useRawMessages(boolean enable) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
642 |
// if (enable) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
643 |
// messages = new JavacMessages(javacBundleName) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
644 |
// @Override |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
645 |
// public String getLocalizedString(String key, Object... args) { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
646 |
// return key; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
647 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
648 |
// }; |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
649 |
// } else { |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
650 |
// messages = new JavacMessages(javacBundleName); |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
651 |
// } |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
652 |
// } |
10 | 653 |
|
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
654 |
public static final String javacBundleName = |
10 | 655 |
"com.sun.tools.javac.resources.javac"; |
11053
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
656 |
// |
48713f779b1d
7108669: cleanup Log methods for direct printing to streams
jjg
parents:
11052
diff
changeset
|
657 |
// private static JavacMessages messages; |
10 | 658 |
} |