author | jjg |
Thu, 22 Sep 2011 09:24:01 -0700 | |
changeset 10638 | c8e9604cf151 |
parent 10193 | 3e1ef5e9f4fd |
child 11052 | 65b9fa7eaf55 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8623
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
2 |
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.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; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
34 |
import java.util.Collection; |
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
35 |
import java.util.LinkedHashSet; |
10 | 36 |
import java.util.MissingResourceException; |
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
37 |
import java.util.Set; |
6721 | 38 |
import javax.tools.JavaFileManager; |
39 |
import javax.tools.JavaFileObject; |
|
40 |
import javax.annotation.processing.Processor; |
|
10 | 41 |
|
42 |
import com.sun.tools.javac.code.Source; |
|
1208 | 43 |
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
|
44 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 45 |
import com.sun.tools.javac.jvm.Target; |
46 |
import com.sun.tools.javac.main.JavacOption.Option; |
|
47 |
import com.sun.tools.javac.main.RecognizedOptions.OptionHelper; |
|
48 |
import com.sun.tools.javac.util.*; |
|
49 |
import com.sun.tools.javac.processing.AnnotationProcessingError; |
|
6721 | 50 |
|
51 |
import static com.sun.tools.javac.main.OptionName.*; |
|
10 | 52 |
|
53 |
/** This class provides a commandline interface to the GJC compiler. |
|
54 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
55 |
* <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
|
56 |
* If you write code that depends on this, you do so at your own risk. |
10 | 57 |
* This code and its internal interfaces are subject to change or |
58 |
* deletion without notice.</b> |
|
59 |
*/ |
|
60 |
public class Main { |
|
61 |
||
62 |
/** The name of the compiler, for use in diagnostics. |
|
63 |
*/ |
|
64 |
String ownName; |
|
65 |
||
66 |
/** The writer to use for diagnostic output. |
|
67 |
*/ |
|
68 |
PrintWriter out; |
|
69 |
||
70 |
/** |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
71 |
* 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
|
72 |
* arg errors, or exceptions in user provided code. |
10 | 73 |
*/ |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
74 |
boolean apiMode; |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
75 |
|
10 | 76 |
|
77 |
/** Result codes. |
|
78 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
79 |
public enum Result { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
80 |
OK(0), // Compilation completed with no errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
81 |
ERROR(1), // Completed but reported errors. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
82 |
CMDERR(2), // Bad command-line arguments |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
83 |
SYSERR(3), // System error or resource exhaustion. |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
84 |
ABNORMAL(4); // Compiler terminated abnormally |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
85 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
86 |
Result(int exitCode) { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
87 |
this.exitCode = exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
88 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
89 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
90 |
public boolean isOK() { |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
91 |
return (exitCode == 0); |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
92 |
} |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
93 |
|
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
94 |
public final int exitCode; |
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
95 |
} |
10 | 96 |
|
97 |
private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() { |
|
98 |
||
99 |
public void setOut(PrintWriter out) { |
|
100 |
Main.this.out = out; |
|
101 |
} |
|
102 |
||
103 |
public void error(String key, Object... args) { |
|
104 |
Main.this.error(key, args); |
|
105 |
} |
|
106 |
||
107 |
public void printVersion() { |
|
108 |
Log.printLines(out, getLocalizedString("version", ownName, JavaCompiler.version())); |
|
109 |
} |
|
110 |
||
111 |
public void printFullVersion() { |
|
112 |
Log.printLines(out, getLocalizedString("fullVersion", ownName, JavaCompiler.fullVersion())); |
|
113 |
} |
|
114 |
||
115 |
public void printHelp() { |
|
116 |
help(); |
|
117 |
} |
|
118 |
||
119 |
public void printXhelp() { |
|
120 |
xhelp(); |
|
121 |
} |
|
122 |
||
123 |
public void addFile(File f) { |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
124 |
filenames.add(f); |
10 | 125 |
} |
126 |
||
127 |
public void addClassName(String s) { |
|
128 |
classnames.append(s); |
|
129 |
} |
|
130 |
||
131 |
}); |
|
132 |
||
133 |
/** |
|
134 |
* Construct a compiler instance. |
|
135 |
*/ |
|
136 |
public Main(String name) { |
|
137 |
this(name, new PrintWriter(System.err, true)); |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Construct a compiler instance. |
|
142 |
*/ |
|
143 |
public Main(String name, PrintWriter out) { |
|
144 |
this.ownName = name; |
|
145 |
this.out = out; |
|
146 |
} |
|
147 |
/** A table of all options that's passed to the JavaCompiler constructor. */ |
|
148 |
private Options options = null; |
|
149 |
||
150 |
/** The list of source files to process |
|
151 |
*/ |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
152 |
public Set<File> filenames = null; // XXX sb protected |
10 | 153 |
|
154 |
/** List of class files names passed on the command line |
|
155 |
*/ |
|
156 |
public ListBuffer<String> classnames = null; // XXX sb protected |
|
157 |
||
158 |
/** Print a string that explains usage. |
|
159 |
*/ |
|
160 |
void help() { |
|
161 |
Log.printLines(out, getLocalizedString("msg.usage.header", ownName)); |
|
162 |
for (int i=0; i<recognizedOptions.length; i++) { |
|
163 |
recognizedOptions[i].help(out); |
|
164 |
} |
|
165 |
out.println(); |
|
166 |
} |
|
167 |
||
168 |
/** Print a string that explains usage for X options. |
|
169 |
*/ |
|
170 |
void xhelp() { |
|
171 |
for (int i=0; i<recognizedOptions.length; i++) { |
|
172 |
recognizedOptions[i].xhelp(out); |
|
173 |
} |
|
174 |
out.println(); |
|
175 |
Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer")); |
|
176 |
} |
|
177 |
||
178 |
/** Report a usage error. |
|
179 |
*/ |
|
180 |
void error(String key, Object... args) { |
|
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
181 |
if (apiMode) { |
10 | 182 |
String msg = getLocalizedString(key, args); |
183 |
throw new PropagatedException(new IllegalStateException(msg)); |
|
184 |
} |
|
185 |
warning(key, args); |
|
186 |
Log.printLines(out, getLocalizedString("msg.usage", ownName)); |
|
187 |
} |
|
188 |
||
189 |
/** Report a warning. |
|
190 |
*/ |
|
191 |
void warning(String key, Object... args) { |
|
192 |
Log.printLines(out, ownName + ": " |
|
193 |
+ getLocalizedString(key, args)); |
|
194 |
} |
|
195 |
||
196 |
public Option getOption(String flag) { |
|
197 |
for (Option option : recognizedOptions) { |
|
198 |
if (option.matches(flag)) |
|
199 |
return option; |
|
200 |
} |
|
201 |
return null; |
|
202 |
} |
|
203 |
||
204 |
public void setOptions(Options options) { |
|
205 |
if (options == null) |
|
206 |
throw new NullPointerException(); |
|
207 |
this.options = options; |
|
208 |
} |
|
209 |
||
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
210 |
public void setAPIMode(boolean apiMode) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
211 |
this.apiMode = apiMode; |
10 | 212 |
} |
213 |
||
214 |
/** Process command line arguments: store all command line options |
|
215 |
* in `options' table and return all source filenames. |
|
216 |
* @param flags The array of command line arguments. |
|
217 |
*/ |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
218 |
public Collection<File> processArgs(String[] flags) { // XXX sb protected |
10 | 219 |
int ac = 0; |
220 |
while (ac < flags.length) { |
|
221 |
String flag = flags[ac]; |
|
222 |
ac++; |
|
223 |
||
224 |
Option option = null; |
|
225 |
||
226 |
if (flag.length() > 0) { |
|
227 |
// quick hack to speed up file processing: |
|
228 |
// if the option does not begin with '-', there is no need to check |
|
229 |
// most of the compiler options. |
|
230 |
int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; |
|
231 |
for (int j=firstOptionToCheck; j<recognizedOptions.length; j++) { |
|
232 |
if (recognizedOptions[j].matches(flag)) { |
|
233 |
option = recognizedOptions[j]; |
|
234 |
break; |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
if (option == null) { |
|
240 |
error("err.invalid.flag", flag); |
|
241 |
return null; |
|
242 |
} |
|
243 |
||
244 |
if (option.hasArg()) { |
|
245 |
if (ac == flags.length) { |
|
246 |
error("err.req.arg", flag); |
|
247 |
return null; |
|
248 |
} |
|
249 |
String operand = flags[ac]; |
|
250 |
ac++; |
|
251 |
if (option.process(options, flag, operand)) |
|
252 |
return null; |
|
253 |
} else { |
|
254 |
if (option.process(options, flag)) |
|
255 |
return null; |
|
256 |
} |
|
257 |
} |
|
258 |
||
6721 | 259 |
if (!checkDirectory(D)) |
10 | 260 |
return null; |
6721 | 261 |
if (!checkDirectory(S)) |
10 | 262 |
return null; |
263 |
||
6721 | 264 |
String sourceString = options.get(SOURCE); |
10 | 265 |
Source source = (sourceString != null) |
266 |
? Source.lookup(sourceString) |
|
267 |
: Source.DEFAULT; |
|
6721 | 268 |
String targetString = options.get(TARGET); |
10 | 269 |
Target target = (targetString != null) |
270 |
? Target.lookup(targetString) |
|
271 |
: Target.DEFAULT; |
|
272 |
// We don't check source/target consistency for CLDC, as J2ME |
|
273 |
// profiles are not aligned with J2SE targets; moreover, a |
|
274 |
// single CLDC target may have many profiles. In addition, |
|
275 |
// this is needed for the continued functioning of the JSR14 |
|
276 |
// prototype. |
|
277 |
if (Character.isDigit(target.name.charAt(0))) { |
|
278 |
if (target.compareTo(source.requiredTarget()) < 0) { |
|
279 |
if (targetString != null) { |
|
280 |
if (sourceString == null) { |
|
281 |
warning("warn.target.default.source.conflict", |
|
282 |
targetString, |
|
283 |
source.requiredTarget().name); |
|
284 |
} else { |
|
285 |
warning("warn.source.target.conflict", |
|
286 |
sourceString, |
|
287 |
source.requiredTarget().name); |
|
288 |
} |
|
289 |
return null; |
|
290 |
} else { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
291 |
target = source.requiredTarget(); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
292 |
options.put("-target", target.name); |
10 | 293 |
} |
294 |
} else { |
|
295 |
if (targetString == null && !source.allowGenerics()) { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
296 |
target = Target.JDK1_4; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2212
diff
changeset
|
297 |
options.put("-target", target.name); |
10 | 298 |
} |
299 |
} |
|
300 |
} |
|
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
301 |
|
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
302 |
// 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
|
303 |
String showClass = options.get("showClass"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
304 |
if (showClass != null) { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
305 |
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
|
306 |
showClass = "com.sun.tools.javac.Main"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
307 |
showClass(showClass); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
308 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
309 |
|
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
310 |
return filenames; |
10 | 311 |
} |
312 |
// where |
|
6721 | 313 |
private boolean checkDirectory(OptionName optName) { |
10 | 314 |
String value = options.get(optName); |
315 |
if (value == null) |
|
316 |
return true; |
|
317 |
File file = new File(value); |
|
318 |
if (!file.exists()) { |
|
319 |
error("err.dir.not.found", value); |
|
320 |
return false; |
|
321 |
} |
|
322 |
if (!file.isDirectory()) { |
|
323 |
error("err.file.not.directory", value); |
|
324 |
return false; |
|
325 |
} |
|
326 |
return true; |
|
327 |
} |
|
328 |
||
329 |
/** Programmatic interface for main function. |
|
330 |
* @param args The command line parameters. |
|
331 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
332 |
public Result compile(String[] args) { |
10 | 333 |
Context context = new Context(); |
334 |
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
|
335 |
Result result = compile(args, context); |
10 | 336 |
if (fileManager instanceof JavacFileManager) { |
337 |
// A fresh context was created above, so jfm must be a JavacFileManager |
|
338 |
((JavacFileManager)fileManager).close(); |
|
339 |
} |
|
340 |
return result; |
|
341 |
} |
|
342 |
||
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
343 |
public Result compile(String[] args, Context context) { |
10 | 344 |
return compile(args, context, List.<JavaFileObject>nil(), null); |
345 |
} |
|
346 |
||
347 |
/** Programmatic interface for main function. |
|
348 |
* @param args The command line parameters. |
|
349 |
*/ |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
350 |
public Result compile(String[] args, |
10 | 351 |
Context context, |
352 |
List<JavaFileObject> fileObjects, |
|
353 |
Iterable<? extends Processor> processors) |
|
354 |
{ |
|
355 |
if (options == null) |
|
356 |
options = Options.instance(context); // creates a new one |
|
357 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
358 |
filenames = new LinkedHashSet<File>(); |
10 | 359 |
classnames = new ListBuffer<String>(); |
360 |
JavaCompiler comp = null; |
|
361 |
/* |
|
362 |
* TODO: Logic below about what is an acceptable command line |
|
363 |
* should be updated to take annotation processing semantics |
|
364 |
* into account. |
|
365 |
*/ |
|
366 |
try { |
|
367 |
if (args.length == 0 && fileObjects.isEmpty()) { |
|
368 |
help(); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
369 |
return Result.CMDERR; |
10 | 370 |
} |
371 |
||
10193
3e1ef5e9f4fd
7061125: Proposed javac argument processing performance improvement
jjg
parents:
9071
diff
changeset
|
372 |
Collection<File> files; |
10 | 373 |
try { |
1866 | 374 |
files = processArgs(CommandLine.parse(args)); |
375 |
if (files == null) { |
|
10 | 376 |
// null signals an error in options, abort |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
377 |
return Result.CMDERR; |
1866 | 378 |
} else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { |
10 | 379 |
// it is allowed to compile nothing if just asking for help or version info |
6721 | 380 |
if (options.isSet(HELP) |
381 |
|| options.isSet(X) |
|
382 |
|| options.isSet(VERSION) |
|
383 |
|| options.isSet(FULLVERSION)) |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
384 |
return Result.OK; |
8623
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
385 |
if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
386 |
error("err.no.source.files.classes"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
387 |
} else { |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
388 |
error("err.no.source.files"); |
cc57bd7697a2
6986895: compiler gives misleading message for no input files
jjg
parents:
8036
diff
changeset
|
389 |
} |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
390 |
return Result.CMDERR; |
10 | 391 |
} |
392 |
} catch (java.io.FileNotFoundException e) { |
|
393 |
Log.printLines(out, ownName + ": " + |
|
394 |
getLocalizedString("err.file.not.found", |
|
395 |
e.getMessage())); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
396 |
return Result.SYSERR; |
10 | 397 |
} |
398 |
||
6721 | 399 |
boolean forceStdOut = options.isSet("stdout"); |
10 | 400 |
if (forceStdOut) { |
401 |
out.flush(); |
|
402 |
out = new PrintWriter(System.out, true); |
|
403 |
} |
|
404 |
||
405 |
context.put(Log.outKey, out); |
|
406 |
||
1208 | 407 |
// allow System property in following line as a Mustang legacy |
6721 | 408 |
boolean batchMode = (options.isUnset("nonBatchMode") |
1208 | 409 |
&& System.getProperty("nonBatchMode") == null); |
410 |
if (batchMode) |
|
411 |
CacheFSInfo.preRegister(context); |
|
412 |
||
10 | 413 |
fileManager = context.get(JavaFileManager.class); |
414 |
||
415 |
comp = JavaCompiler.instance(context); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
416 |
if (comp == null) return Result.SYSERR; |
10 | 417 |
|
1866 | 418 |
Log log = Log.instance(context); |
419 |
||
420 |
if (!files.isEmpty()) { |
|
10 | 421 |
// add filenames to fileObjects |
422 |
comp = JavaCompiler.instance(context); |
|
423 |
List<JavaFileObject> otherFiles = List.nil(); |
|
424 |
JavacFileManager dfm = (JavacFileManager)fileManager; |
|
1866 | 425 |
for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) |
10 | 426 |
otherFiles = otherFiles.prepend(fo); |
427 |
for (JavaFileObject fo : otherFiles) |
|
428 |
fileObjects = fileObjects.prepend(fo); |
|
429 |
} |
|
430 |
comp.compile(fileObjects, |
|
431 |
classnames.toList(), |
|
432 |
processors); |
|
433 |
||
1866 | 434 |
if (log.expectDiagKeys != null) { |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6721
diff
changeset
|
435 |
if (log.expectDiagKeys.isEmpty()) { |
1866 | 436 |
Log.printLines(log.noticeWriter, "all expected diagnostics found"); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
437 |
return Result.OK; |
1866 | 438 |
} else { |
439 |
Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
440 |
return Result.ERROR; |
1866 | 441 |
} |
442 |
} |
|
443 |
||
1996 | 444 |
if (comp.errorCount() != 0) |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
445 |
return Result.ERROR; |
10 | 446 |
} catch (IOException ex) { |
447 |
ioMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
448 |
return Result.SYSERR; |
10 | 449 |
} catch (OutOfMemoryError ex) { |
450 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
451 |
return Result.SYSERR; |
10 | 452 |
} catch (StackOverflowError ex) { |
453 |
resourceMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
454 |
return Result.SYSERR; |
10 | 455 |
} catch (FatalError ex) { |
456 |
feMessage(ex); |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
457 |
return Result.SYSERR; |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
458 |
} catch (AnnotationProcessingError ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
459 |
if (apiMode) |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
460 |
throw new RuntimeException(ex.getCause()); |
10 | 461 |
apMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
462 |
return Result.SYSERR; |
10 | 463 |
} catch (ClientCodeException ex) { |
464 |
// as specified by javax.tools.JavaCompiler#getTask |
|
465 |
// and javax.tools.JavaCompiler.CompilationTask#call |
|
466 |
throw new RuntimeException(ex.getCause()); |
|
467 |
} catch (PropagatedException ex) { |
|
468 |
throw ex.getCause(); |
|
469 |
} catch (Throwable ex) { |
|
470 |
// Nasty. If we've already reported an error, compensate |
|
471 |
// for buggy compiler error recovery by swallowing thrown |
|
472 |
// exceptions. |
|
473 |
if (comp == null || comp.errorCount() == 0 || |
|
6721 | 474 |
options == null || options.isSet("dev")) |
10 | 475 |
bugMessage(ex); |
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
476 |
return Result.ABNORMAL; |
10 | 477 |
} finally { |
9071
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
478 |
if (comp != null) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
479 |
try { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
480 |
comp.close(); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
481 |
} catch (ClientCodeException ex) { |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
482 |
throw new RuntimeException(ex.getCause()); |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
483 |
} |
88cd61b4e5aa
6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents:
8623
diff
changeset
|
484 |
} |
10 | 485 |
filenames = null; |
486 |
options = null; |
|
487 |
} |
|
10638
c8e9604cf151
7075721: javac should have public enum for exit codes
jjg
parents:
10193
diff
changeset
|
488 |
return Result.OK; |
10 | 489 |
} |
490 |
||
491 |
/** Print a message reporting an internal error. |
|
492 |
*/ |
|
493 |
void bugMessage(Throwable ex) { |
|
494 |
Log.printLines(out, getLocalizedString("msg.bug", |
|
495 |
JavaCompiler.version())); |
|
496 |
ex.printStackTrace(out); |
|
497 |
} |
|
498 |
||
6581 | 499 |
/** Print a message reporting a fatal error. |
10 | 500 |
*/ |
501 |
void feMessage(Throwable ex) { |
|
502 |
Log.printLines(out, ex.getMessage()); |
|
6721 | 503 |
if (ex.getCause() != null && options.isSet("dev")) { |
6581 | 504 |
ex.getCause().printStackTrace(out); |
505 |
} |
|
10 | 506 |
} |
507 |
||
508 |
/** Print a message reporting an input/output error. |
|
509 |
*/ |
|
510 |
void ioMessage(Throwable ex) { |
|
511 |
Log.printLines(out, getLocalizedString("msg.io")); |
|
512 |
ex.printStackTrace(out); |
|
513 |
} |
|
514 |
||
515 |
/** Print a message reporting an out-of-resources error. |
|
516 |
*/ |
|
517 |
void resourceMessage(Throwable ex) { |
|
518 |
Log.printLines(out, getLocalizedString("msg.resource")); |
|
519 |
// System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG |
|
520 |
ex.printStackTrace(out); |
|
521 |
} |
|
522 |
||
523 |
/** Print a message reporting an uncaught exception from an |
|
524 |
* annotation processor. |
|
525 |
*/ |
|
526 |
void apMessage(AnnotationProcessingError ex) { |
|
527 |
Log.printLines(out, |
|
528 |
getLocalizedString("msg.proc.annotation.uncaught.exception")); |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6721
diff
changeset
|
529 |
ex.getCause().printStackTrace(out); |
10 | 530 |
} |
531 |
||
5319
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
532 |
/** 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
|
533 |
void showClass(String className) { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
534 |
out.println("javac: show class: " + className); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
535 |
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
|
536 |
if (url == null) |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
537 |
out.println(" class not found"); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
538 |
else { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
539 |
out.println(" " + url); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
540 |
try { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
541 |
final String algorithm = "MD5"; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
542 |
byte[] digest; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
543 |
MessageDigest md = MessageDigest.getInstance(algorithm); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
544 |
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
|
545 |
try { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
546 |
byte[] buf = new byte[8192]; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
547 |
int n; |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
548 |
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
|
549 |
digest = md.digest(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
550 |
} finally { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
551 |
in.close(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
552 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
553 |
StringBuilder sb = new StringBuilder(); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
554 |
for (byte b: digest) |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
555 |
sb.append(String.format("%02x", b)); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
556 |
out.println(" " + algorithm + " checksum: " + sb); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
557 |
} catch (Exception e) { |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
558 |
out.println(" cannot compute digest: " + e); |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
559 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
560 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
561 |
} |
63dc7f367a37
6942649: add hidden option to identify location and version of javac classes
jjg
parents:
2723
diff
changeset
|
562 |
|
10 | 563 |
private JavaFileManager fileManager; |
564 |
||
565 |
/* ************************************************************************ |
|
566 |
* Internationalization |
|
567 |
*************************************************************************/ |
|
568 |
||
569 |
/** Find a localized string in the resource bundle. |
|
570 |
* @param key The key for the localized string. |
|
571 |
*/ |
|
572 |
public static String getLocalizedString(String key, Object... args) { // FIXME sb private |
|
573 |
try { |
|
574 |
if (messages == null) |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1208
diff
changeset
|
575 |
messages = new JavacMessages(javacBundleName); |
10 | 576 |
return messages.getLocalizedString("javac." + key, args); |
577 |
} |
|
578 |
catch (MissingResourceException e) { |
|
579 |
throw new Error("Fatal Error: Resource for javac is missing", e); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
public static void useRawMessages(boolean enable) { |
|
584 |
if (enable) { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1208
diff
changeset
|
585 |
messages = new JavacMessages(javacBundleName) { |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6721
diff
changeset
|
586 |
@Override |
10 | 587 |
public String getLocalizedString(String key, Object... args) { |
588 |
return key; |
|
589 |
} |
|
590 |
}; |
|
591 |
} else { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1208
diff
changeset
|
592 |
messages = new JavacMessages(javacBundleName); |
10 | 593 |
} |
594 |
} |
|
595 |
||
596 |
private static final String javacBundleName = |
|
597 |
"com.sun.tools.javac.resources.javac"; |
|
598 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1208
diff
changeset
|
599 |
private static JavacMessages messages; |
10 | 600 |
} |