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