Tue, 25 Sep 2012 13:11:05 -0700
7196464: upgrade JavaCompiler.shouldStopPolicy to accomodate policies in face of error and no error
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
26 package com.sun.tools.javac.main;
28 import java.util.Collections;
29 import com.sun.tools.javac.util.Log.PrefixKind;
30 import com.sun.tools.javac.util.Log.WriterKind;
31 import com.sun.tools.javac.util.Log;
32 import com.sun.tools.javac.code.Lint;
33 import com.sun.tools.javac.code.Source;
34 import com.sun.tools.javac.code.Type;
35 import com.sun.tools.javac.jvm.Target;
36 import com.sun.tools.javac.util.Options;
37 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
38 import java.io.File;
39 import java.io.FileWriter;
40 import java.io.PrintWriter;
41 import java.util.EnumSet;
42 import java.util.LinkedHashMap;
43 import java.util.Map;
44 import java.util.Set;
45 import javax.lang.model.SourceVersion;
47 import static com.sun.tools.javac.main.Option.ChoiceKind.*;
48 import static com.sun.tools.javac.main.Option.OptionKind.*;
49 import static com.sun.tools.javac.main.Option.OptionGroup.*;
51 /**
52 * Options for javac. The specific Option to handle a command-line option
53 * is identified by searching the members of this enum in order, looking
54 * the first {@link #matches match}. The action for an Option is performed
55 * by calling {@link #process process}, and by providing a suitable
56 * {@link OptionHelper} to provide access the compiler state.
57 *
58 * <p><b>This is NOT part of any supported API.
59 * If you write code that depends on this, you do so at your own
60 * risk. This code and its internal interfaces are subject to change
61 * or deletion without notice.</b></p>
62 */
63 public enum Option {
64 G("-g", "opt.g", STANDARD, BASIC),
66 G_NONE("-g:none", "opt.g.none", STANDARD, BASIC) {
67 @Override
68 public boolean process(OptionHelper helper, String option) {
69 helper.put("-g:", "none");
70 return false;
71 }
72 },
74 G_CUSTOM("-g:", "opt.g.lines.vars.source",
75 STANDARD, BASIC, ANYOF, "lines", "vars", "source"),
77 XLINT("-Xlint", "opt.Xlint", EXTENDED, BASIC),
79 XLINT_CUSTOM("-Xlint:", "opt.Xlint.suboptlist",
80 EXTENDED, BASIC, ANYOF, getXLintChoices()),
82 // -nowarn is retained for command-line backward compatibility
83 NOWARN("-nowarn", "opt.nowarn", STANDARD, BASIC) {
84 @Override
85 public boolean process(OptionHelper helper, String option) {
86 helper.put("-Xlint:none", option);
87 return false;
88 }
89 },
91 VERBOSE("-verbose", "opt.verbose", STANDARD, BASIC),
93 // -deprecation is retained for command-line backward compatibility
94 DEPRECATION("-deprecation", "opt.deprecation", STANDARD, BASIC) {
95 @Override
96 public boolean process(OptionHelper helper, String option) {
97 helper.put("-Xlint:deprecation", option);
98 return false;
99 }
100 },
102 CLASSPATH("-classpath", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER),
104 CP("-cp", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER) {
105 @Override
106 public boolean process(OptionHelper helper, String option, String arg) {
107 return super.process(helper, "-classpath", arg);
108 }
109 },
111 SOURCEPATH("-sourcepath", "opt.arg.path", "opt.sourcepath", STANDARD, FILEMANAGER),
113 BOOTCLASSPATH("-bootclasspath", "opt.arg.path", "opt.bootclasspath", STANDARD, FILEMANAGER) {
114 @Override
115 public boolean process(OptionHelper helper, String option, String arg) {
116 helper.remove("-Xbootclasspath/p:");
117 helper.remove("-Xbootclasspath/a:");
118 return super.process(helper, option, arg);
119 }
120 },
122 XBOOTCLASSPATH_PREPEND("-Xbootclasspath/p:", "opt.arg.path", "opt.Xbootclasspath.p", EXTENDED, FILEMANAGER),
124 XBOOTCLASSPATH_APPEND("-Xbootclasspath/a:", "opt.arg.path", "opt.Xbootclasspath.a", EXTENDED, FILEMANAGER),
126 XBOOTCLASSPATH("-Xbootclasspath:", "opt.arg.path", "opt.bootclasspath", EXTENDED, FILEMANAGER) {
127 @Override
128 public boolean process(OptionHelper helper, String option, String arg) {
129 helper.remove("-Xbootclasspath/p:");
130 helper.remove("-Xbootclasspath/a:");
131 return super.process(helper, "-bootclasspath", arg);
132 }
133 },
135 EXTDIRS("-extdirs", "opt.arg.dirs", "opt.extdirs", STANDARD, FILEMANAGER),
137 DJAVA_EXT_DIRS("-Djava.ext.dirs=", "opt.arg.dirs", "opt.extdirs", EXTENDED, FILEMANAGER) {
138 @Override
139 public boolean process(OptionHelper helper, String option, String arg) {
140 return super.process(helper, "-extdirs", arg);
141 }
142 },
144 ENDORSEDDIRS("-endorseddirs", "opt.arg.dirs", "opt.endorseddirs", STANDARD, FILEMANAGER),
146 DJAVA_ENDORSED_DIRS("-Djava.endorsed.dirs=", "opt.arg.dirs", "opt.endorseddirs", EXTENDED, FILEMANAGER) {
147 @Override
148 public boolean process(OptionHelper helper, String option, String arg) {
149 return super.process(helper, "-endorseddirs", arg);
150 }
151 },
153 PROC("-proc:", "opt.proc.none.only", STANDARD, BASIC, ONEOF, "none", "only"),
155 PROCESSOR("-processor", "opt.arg.class.list", "opt.processor", STANDARD, BASIC),
157 PROCESSORPATH("-processorpath", "opt.arg.path", "opt.processorpath", STANDARD, FILEMANAGER),
159 D("-d", "opt.arg.directory", "opt.d", STANDARD, FILEMANAGER),
161 S("-s", "opt.arg.directory", "opt.sourceDest", STANDARD, FILEMANAGER),
163 H("-h", "opt.arg.directory", "opt.headerDest", STANDARD, FILEMANAGER),
165 IMPLICIT("-implicit:", "opt.implicit", STANDARD, BASIC, ONEOF, "none", "class"),
167 ENCODING("-encoding", "opt.arg.encoding", "opt.encoding", STANDARD, FILEMANAGER) {
168 @Override
169 public boolean process(OptionHelper helper, String option, String operand) {
170 // System.err.println("process encoding " + operand);
171 return super.process(helper, option, operand);
172 }
174 },
176 SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) {
177 @Override
178 public boolean process(OptionHelper helper, String option, String operand) {
179 Source source = Source.lookup(operand);
180 if (source == null) {
181 helper.error("err.invalid.source", operand);
182 return true;
183 }
184 return super.process(helper, option, operand);
185 }
186 },
188 TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) {
189 @Override
190 public boolean process(OptionHelper helper, String option, String operand) {
191 Target target = Target.lookup(operand);
192 if (target == null) {
193 helper.error("err.invalid.target", operand);
194 return true;
195 }
196 return super.process(helper, option, operand);
197 }
198 },
200 VERSION("-version", "opt.version", STANDARD, INFO) {
201 @Override
202 public boolean process(OptionHelper helper, String option) {
203 Log log = helper.getLog();
204 String ownName = helper.getOwnName();
205 log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version());
206 return super.process(helper, option);
207 }
208 },
210 FULLVERSION("-fullversion", null, HIDDEN, INFO) {
211 @Override
212 public boolean process(OptionHelper helper, String option) {
213 Log log = helper.getLog();
214 String ownName = helper.getOwnName();
215 log.printLines(PrefixKind.JAVAC, "fullVersion", ownName, JavaCompiler.fullVersion());
216 return super.process(helper, option);
217 }
218 },
220 DIAGS("-XDdiags=", null, HIDDEN, INFO) {
221 @Override
222 public boolean process(OptionHelper helper, String option) {
223 option = option.substring(option.indexOf('=') + 1);
224 String diagsOption = option.contains("%") ?
225 "-XDdiagsFormat=" :
226 "-XDdiags=";
227 diagsOption += option;
228 if (XD.matches(diagsOption))
229 return XD.process(helper, diagsOption);
230 else
231 return false;
232 }
233 },
235 HELP("-help", "opt.help", STANDARD, INFO) {
236 @Override
237 public boolean process(OptionHelper helper, String option) {
238 Log log = helper.getLog();
239 String ownName = helper.getOwnName();
240 log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName);
241 for (Option o: getJavaCompilerOptions()) {
242 o.help(log, OptionKind.STANDARD);
243 }
244 log.printNewline();
245 return super.process(helper, option);
246 }
247 },
249 A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC) {
250 { hasSuffix = true; }
252 @Override
253 public boolean matches(String arg) {
254 return arg.startsWith("-A");
255 }
257 @Override
258 public boolean hasArg() {
259 return false;
260 }
261 // Mapping for processor options created in
262 // JavacProcessingEnvironment
263 @Override
264 public boolean process(OptionHelper helper, String option) {
265 int argLength = option.length();
266 if (argLength == 2) {
267 helper.error("err.empty.A.argument");
268 return true;
269 }
270 int sepIndex = option.indexOf('=');
271 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
272 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
273 helper.error("err.invalid.A.key", option);
274 return true;
275 }
276 return process(helper, option, option);
277 }
278 },
280 X("-X", "opt.X", STANDARD, INFO) {
281 @Override
282 public boolean process(OptionHelper helper, String option) {
283 Log log = helper.getLog();
284 for (Option o: getJavaCompilerOptions()) {
285 o.help(log, OptionKind.EXTENDED);
286 }
287 log.printNewline();
288 log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer");
289 return super.process(helper, option);
290 }
291 },
293 // This option exists only for the purpose of documenting itself.
294 // It's actually implemented by the launcher.
295 J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO) {
296 { hasSuffix = true; }
298 @Override
299 public boolean process(OptionHelper helper, String option) {
300 throw new AssertionError
301 ("the -J flag should be caught by the launcher.");
302 }
303 },
305 // stop after parsing and attributing.
306 // new HiddenOption("-attrparseonly"),
308 // new Option("-moreinfo", "opt.moreinfo") {
309 MOREINFO("-moreinfo", null, HIDDEN, BASIC) {
310 @Override
311 public boolean process(OptionHelper helper, String option) {
312 Type.moreInfo = true;
313 return super.process(helper, option);
314 }
315 },
317 // treat warnings as errors
318 WERROR("-Werror", "opt.Werror", STANDARD, BASIC),
320 // // use complex inference from context in the position of a method call argument
321 // COMPLEXINFERENCE("-complexinference", null, HIDDEN, BASIC),
323 // generare source stubs
324 // new HiddenOption("-stubs"),
326 // relax some constraints to allow compiling from stubs
327 // new HiddenOption("-relax"),
329 // output source after translating away inner classes
330 // new Option("-printflat", "opt.printflat"),
331 // new HiddenOption("-printflat"),
333 // display scope search details
334 // new Option("-printsearch", "opt.printsearch"),
335 // new HiddenOption("-printsearch"),
337 // prompt after each error
338 // new Option("-prompt", "opt.prompt"),
339 PROMPT("-prompt", null, HIDDEN, BASIC),
341 // dump stack on error
342 DOE("-doe", null, HIDDEN, BASIC),
344 // output source after type erasure
345 // new Option("-s", "opt.s"),
346 PRINTSOURCE("-printsource", null, HIDDEN, BASIC),
348 // output shrouded class files
349 // new Option("-scramble", "opt.scramble"),
350 // new Option("-scrambleall", "opt.scrambleall"),
352 // display warnings for generic unchecked operations
353 WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) {
354 @Override
355 public boolean process(OptionHelper helper, String option) {
356 helper.put("-Xlint:unchecked", option);
357 return false;
358 }
359 },
361 XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC),
363 XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC),
365 XSTDOUT("-Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) {
366 @Override
367 public boolean process(OptionHelper helper, String option, String arg) {
368 try {
369 Log log = helper.getLog();
370 // TODO: this file should be closed at the end of compilation
371 log.setWriters(new PrintWriter(new FileWriter(arg), true));
372 } catch (java.io.IOException e) {
373 helper.error("err.error.writing.file", arg, e);
374 return true;
375 }
376 return super.process(helper, option, arg);
377 }
378 },
380 XPRINT("-Xprint", "opt.print", EXTENDED, BASIC),
382 XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC),
384 XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC),
386 XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"),
388 XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"),
390 /* -O is a no-op, accepted for backward compatibility. */
391 O("-O", null, HIDDEN, BASIC),
393 /* -Xjcov produces tables to support the code coverage tool jcov. */
394 XJCOV("-Xjcov", null, HIDDEN, BASIC),
396 /* This is a back door to the compiler's option table.
397 * -XDx=y sets the option x to the value y.
398 * -XDx sets the option x to the value x.
399 */
400 XD("-XD", null, HIDDEN, BASIC) {
401 String s;
402 @Override
403 public boolean matches(String s) {
404 this.s = s;
405 return s.startsWith(text);
406 }
407 @Override
408 public boolean process(OptionHelper helper, String option) {
409 s = s.substring(text.length());
410 int eq = s.indexOf('=');
411 String key = (eq < 0) ? s : s.substring(0, eq);
412 String value = (eq < 0) ? s : s.substring(eq+1);
413 helper.put(key, value);
414 return false;
415 }
416 },
418 // This option exists only for the purpose of documenting itself.
419 // It's actually implemented by the CommandLine class.
420 AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO) {
421 { hasSuffix = true; }
423 @Override
424 public boolean process(OptionHelper helper, String option) {
425 throw new AssertionError("the @ flag should be caught by CommandLine.");
426 }
427 },
429 /*
430 * TODO: With apt, the matches method accepts anything if
431 * -XclassAsDecls is used; code elsewhere does the lookup to
432 * see if the class name is both legal and found.
433 *
434 * In apt, the process method adds the candidate class file
435 * name to a separate list.
436 */
437 SOURCEFILE("sourcefile", null, HIDDEN, INFO) {
438 String s;
439 @Override
440 public boolean matches(String s) {
441 this.s = s;
442 return s.endsWith(".java") // Java source file
443 || SourceVersion.isName(s); // Legal type name
444 }
445 @Override
446 public boolean process(OptionHelper helper, String option) {
447 if (s.endsWith(".java") ) {
448 File f = new File(s);
449 if (!f.exists()) {
450 helper.error("err.file.not.found", f);
451 return true;
452 }
453 if (!f.isFile()) {
454 helper.error("err.file.not.file", f);
455 return true;
456 }
457 helper.addFile(f);
458 }
459 else
460 helper.addClassName(s);
461 return false;
462 }
463 };
465 /** The kind of an Option. This is used by the -help and -X options. */
466 public enum OptionKind {
467 /** A standard option, documented by -help. */
468 STANDARD,
469 /** An extended option, documented by -X. */
470 EXTENDED,
471 /** A hidden option, not documented. */
472 HIDDEN,
473 }
475 /** The group for an Option. This determines the situations in which the
476 * option is applicable. */
477 enum OptionGroup {
478 /** A basic option, available for use on the command line or via the
479 * Compiler API. */
480 BASIC,
481 /** An option for javac's standard JavaFileManager. Other file managers
482 * may or may not support these options. */
483 FILEMANAGER,
484 /** A command-line option that requests information, such as -help. */
485 INFO,
486 /** A command-line "option" representing a file or class name. */
487 OPERAND
488 }
490 /** The kind of choice for "choice" options. */
491 enum ChoiceKind {
492 /** The expected value is exactly one of the set of choices. */
493 ONEOF,
494 /** The expected value is one of more of the set of choices. */
495 ANYOF
496 }
498 public final String text;
500 final OptionKind kind;
502 final OptionGroup group;
504 /** Documentation key for arguments.
505 */
506 final String argsNameKey;
508 /** Documentation key for description.
509 */
510 final String descrKey;
512 /** Suffix option (-foo=bar or -foo:bar)
513 */
514 boolean hasSuffix;
516 /** The kind of choices for this option, if any.
517 */
518 final ChoiceKind choiceKind;
520 /** The choices for this option, if any, and whether or not the choices
521 * are hidden
522 */
523 final Map<String,Boolean> choices;
526 Option(String text, String descrKey,
527 OptionKind kind, OptionGroup group) {
528 this(text, null, descrKey, kind, group, null, null);
529 }
531 Option(String text, String argsNameKey, String descrKey,
532 OptionKind kind, OptionGroup group) {
533 this(text, argsNameKey, descrKey, kind, group, null, null);
534 }
536 Option(String text, String descrKey,
537 OptionKind kind, OptionGroup group,
538 ChoiceKind choiceKind, Map<String,Boolean> choices) {
539 this(text, null, descrKey, kind, group, choiceKind, choices);
540 }
542 Option(String text, String descrKey,
543 OptionKind kind, OptionGroup group,
544 ChoiceKind choiceKind, String... choices) {
545 this(text, null, descrKey, kind, group, choiceKind, createChoices(choices));
546 }
547 // where
548 private static Map<String,Boolean> createChoices(String... choices) {
549 Map<String,Boolean> map = new LinkedHashMap<String,Boolean>();
550 for (String c: choices)
551 map.put(c, false);
552 return map;
553 }
555 private Option(String text, String argsNameKey, String descrKey,
556 OptionKind kind, OptionGroup group,
557 ChoiceKind choiceKind, Map<String,Boolean> choices) {
558 this.text = text;
559 this.argsNameKey = argsNameKey;
560 this.descrKey = descrKey;
561 this.kind = kind;
562 this.group = group;
563 this.choiceKind = choiceKind;
564 this.choices = choices;
565 char lastChar = text.charAt(text.length()-1);
566 hasSuffix = lastChar == ':' || lastChar == '=';
567 }
569 public String getText() {
570 return text;
571 }
573 public OptionKind getKind() {
574 return kind;
575 }
577 public boolean hasArg() {
578 return argsNameKey != null && !hasSuffix;
579 }
581 public boolean matches(String option) {
582 if (!hasSuffix)
583 return option.equals(text);
585 if (!option.startsWith(text))
586 return false;
588 if (choices != null) {
589 String arg = option.substring(text.length());
590 if (choiceKind == ChoiceKind.ONEOF)
591 return choices.keySet().contains(arg);
592 else {
593 for (String a: arg.split(",+")) {
594 if (!choices.keySet().contains(a))
595 return false;
596 }
597 }
598 }
600 return true;
601 }
603 public boolean process(OptionHelper helper, String option, String arg) {
604 if (choices != null) {
605 if (choiceKind == ChoiceKind.ONEOF) {
606 // some clients like to see just one of option+choice set
607 for (String s: choices.keySet())
608 helper.remove(option + s);
609 String opt = option + arg;
610 helper.put(opt, opt);
611 // some clients like to see option (without trailing ":")
612 // set to arg
613 String nm = option.substring(0, option.length() - 1);
614 helper.put(nm, arg);
615 } else {
616 // set option+word for each word in arg
617 for (String a: arg.split(",+")) {
618 String opt = option + a;
619 helper.put(opt, opt);
620 }
621 }
622 }
623 helper.put(option, arg);
624 return false;
625 }
627 public boolean process(OptionHelper helper, String option) {
628 if (hasSuffix)
629 return process(helper, text, option.substring(text.length()));
630 else
631 return process(helper, option, option);
632 }
634 void help(Log log, OptionKind kind) {
635 if (this.kind != kind)
636 return;
638 log.printRawLines(WriterKind.NOTICE,
639 String.format(" %-26s %s",
640 helpSynopsis(log),
641 log.localize(PrefixKind.JAVAC, descrKey)));
643 }
645 private String helpSynopsis(Log log) {
646 StringBuilder sb = new StringBuilder();
647 sb.append(text);
648 if (argsNameKey == null) {
649 if (choices != null) {
650 String sep = "{";
651 for (Map.Entry<String,Boolean> e: choices.entrySet()) {
652 if (!e.getValue()) {
653 sb.append(sep);
654 sb.append(e.getKey());
655 sep = ",";
656 }
657 }
658 sb.append("}");
659 }
660 } else {
661 if (!hasSuffix)
662 sb.append(" ");
663 sb.append(log.localize(PrefixKind.JAVAC, argsNameKey));
665 }
667 return sb.toString();
668 }
670 // For -XpkgInfo:value
671 public enum PkgInfo {
672 ALWAYS, LEGACY, NONEMPTY;
673 public static PkgInfo get(Options options) {
674 String v = options.get(XPKGINFO);
675 return (v == null
676 ? PkgInfo.LEGACY
677 : PkgInfo.valueOf(v.toUpperCase()));
678 }
679 }
681 private static Map<String,Boolean> getXLintChoices() {
682 Map<String,Boolean> choices = new LinkedHashMap<String,Boolean>();
683 choices.put("all", false);
684 for (Lint.LintCategory c : Lint.LintCategory.values())
685 choices.put(c.option, c.hidden);
686 for (Lint.LintCategory c : Lint.LintCategory.values())
687 choices.put("-" + c.option, c.hidden);
688 choices.put("none", false);
689 return choices;
690 }
692 static Set<Option> getJavaCompilerOptions() {
693 return EnumSet.allOf(Option.class);
694 }
696 public static Set<Option> getJavacFileManagerOptions() {
697 return getOptions(EnumSet.of(FILEMANAGER));
698 }
700 public static Set<Option> getJavacToolOptions() {
701 return getOptions(EnumSet.of(BASIC));
702 }
704 static Set<Option> getOptions(Set<OptionGroup> desired) {
705 Set<Option> options = EnumSet.noneOf(Option.class);
706 for (Option option : Option.values())
707 if (desired.contains(option.group))
708 options.add(option);
709 return Collections.unmodifiableSet(options);
710 }
712 }