Mon, 10 Dec 2012 16:21:26 +0000
8003967: detect and remove all mutable implicit static enum fields in langtools
Reviewed-by: jjg
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 return super.process(helper, option, operand);
171 }
173 },
175 SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) {
176 @Override
177 public boolean process(OptionHelper helper, String option, String operand) {
178 Source source = Source.lookup(operand);
179 if (source == null) {
180 helper.error("err.invalid.source", operand);
181 return true;
182 }
183 return super.process(helper, option, operand);
184 }
185 },
187 TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) {
188 @Override
189 public boolean process(OptionHelper helper, String option, String operand) {
190 Target target = Target.lookup(operand);
191 if (target == null) {
192 helper.error("err.invalid.target", operand);
193 return true;
194 }
195 return super.process(helper, option, operand);
196 }
197 },
199 VERSION("-version", "opt.version", STANDARD, INFO) {
200 @Override
201 public boolean process(OptionHelper helper, String option) {
202 Log log = helper.getLog();
203 String ownName = helper.getOwnName();
204 log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version());
205 return super.process(helper, option);
206 }
207 },
209 FULLVERSION("-fullversion", null, HIDDEN, INFO) {
210 @Override
211 public boolean process(OptionHelper helper, String option) {
212 Log log = helper.getLog();
213 String ownName = helper.getOwnName();
214 log.printLines(PrefixKind.JAVAC, "fullVersion", ownName, JavaCompiler.fullVersion());
215 return super.process(helper, option);
216 }
217 },
219 DIAGS("-XDdiags=", null, HIDDEN, INFO) {
220 @Override
221 public boolean process(OptionHelper helper, String option) {
222 option = option.substring(option.indexOf('=') + 1);
223 String diagsOption = option.contains("%") ?
224 "-XDdiagsFormat=" :
225 "-XDdiags=";
226 diagsOption += option;
227 if (XD.matches(diagsOption))
228 return XD.process(helper, diagsOption);
229 else
230 return false;
231 }
232 },
234 HELP("-help", "opt.help", STANDARD, INFO) {
235 @Override
236 public boolean process(OptionHelper helper, String option) {
237 Log log = helper.getLog();
238 String ownName = helper.getOwnName();
239 log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName);
240 for (Option o: getJavaCompilerOptions()) {
241 o.help(log, OptionKind.STANDARD);
242 }
243 log.printNewline();
244 return super.process(helper, option);
245 }
246 },
248 A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC, true) {
249 @Override
250 public boolean matches(String arg) {
251 return arg.startsWith("-A");
252 }
254 @Override
255 public boolean hasArg() {
256 return false;
257 }
258 // Mapping for processor options created in
259 // JavacProcessingEnvironment
260 @Override
261 public boolean process(OptionHelper helper, String option) {
262 int argLength = option.length();
263 if (argLength == 2) {
264 helper.error("err.empty.A.argument");
265 return true;
266 }
267 int sepIndex = option.indexOf('=');
268 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
269 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
270 helper.error("err.invalid.A.key", option);
271 return true;
272 }
273 return process(helper, option, option);
274 }
275 },
277 X("-X", "opt.X", STANDARD, INFO) {
278 @Override
279 public boolean process(OptionHelper helper, String option) {
280 Log log = helper.getLog();
281 for (Option o: getJavaCompilerOptions()) {
282 o.help(log, OptionKind.EXTENDED);
283 }
284 log.printNewline();
285 log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer");
286 return super.process(helper, option);
287 }
288 },
290 // This option exists only for the purpose of documenting itself.
291 // It's actually implemented by the launcher.
292 J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO) {
293 @Override
294 public boolean process(OptionHelper helper, String option) {
295 throw new AssertionError
296 ("the -J flag should be caught by the launcher.");
297 }
298 },
300 MOREINFO("-moreinfo", null, HIDDEN, BASIC) {
301 @Override
302 public boolean process(OptionHelper helper, String option) {
303 Type.moreInfo = true;
304 return super.process(helper, option);
305 }
306 },
308 // treat warnings as errors
309 WERROR("-Werror", "opt.Werror", STANDARD, BASIC),
311 // prompt after each error
312 // new Option("-prompt", "opt.prompt"),
313 PROMPT("-prompt", null, HIDDEN, BASIC),
315 // dump stack on error
316 DOE("-doe", null, HIDDEN, BASIC),
318 // output source after type erasure
319 PRINTSOURCE("-printsource", null, HIDDEN, BASIC),
321 // display warnings for generic unchecked operations
322 WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) {
323 @Override
324 public boolean process(OptionHelper helper, String option) {
325 helper.put("-Xlint:unchecked", option);
326 return false;
327 }
328 },
330 XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC),
332 XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC),
334 XSTDOUT("-Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) {
335 @Override
336 public boolean process(OptionHelper helper, String option, String arg) {
337 try {
338 Log log = helper.getLog();
339 // TODO: this file should be closed at the end of compilation
340 log.setWriters(new PrintWriter(new FileWriter(arg), true));
341 } catch (java.io.IOException e) {
342 helper.error("err.error.writing.file", arg, e);
343 return true;
344 }
345 return super.process(helper, option, arg);
346 }
347 },
349 XPRINT("-Xprint", "opt.print", EXTENDED, BASIC),
351 XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC),
353 XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC),
355 XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"),
357 XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"),
359 /* -O is a no-op, accepted for backward compatibility. */
360 O("-O", null, HIDDEN, BASIC),
362 /* -Xjcov produces tables to support the code coverage tool jcov. */
363 XJCOV("-Xjcov", null, HIDDEN, BASIC),
365 PLUGIN("-Xplugin:", "opt.arg.plugin", "opt.plugin", EXTENDED, BASIC) {
366 @Override
367 public boolean process(OptionHelper helper, String option) {
368 String p = option.substring(option.indexOf(':') + 1);
369 String prev = helper.get(PLUGIN);
370 helper.put(PLUGIN.text, (prev == null) ? p : prev + '\0' + p.trim());
371 return false;
372 }
373 },
375 /* This is a back door to the compiler's option table.
376 * -XDx=y sets the option x to the value y.
377 * -XDx sets the option x to the value x.
378 */
379 XD("-XD", null, HIDDEN, BASIC) {
380 @Override
381 public boolean matches(String s) {
382 return s.startsWith(text);
383 }
384 @Override
385 public boolean process(OptionHelper helper, String option) {
386 option = option.substring(text.length());
387 int eq = option.indexOf('=');
388 String key = (eq < 0) ? option : option.substring(0, eq);
389 String value = (eq < 0) ? option : option.substring(eq+1);
390 helper.put(key, value);
391 return false;
392 }
393 },
395 // This option exists only for the purpose of documenting itself.
396 // It's actually implemented by the CommandLine class.
397 AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO) {
398 @Override
399 public boolean process(OptionHelper helper, String option) {
400 throw new AssertionError("the @ flag should be caught by CommandLine.");
401 }
402 },
404 /*
405 * TODO: With apt, the matches method accepts anything if
406 * -XclassAsDecls is used; code elsewhere does the lookup to
407 * see if the class name is both legal and found.
408 *
409 * In apt, the process method adds the candidate class file
410 * name to a separate list.
411 */
412 SOURCEFILE("sourcefile", null, HIDDEN, INFO) {
413 @Override
414 public boolean matches(String s) {
415 return s.endsWith(".java") // Java source file
416 || SourceVersion.isName(s); // Legal type name
417 }
418 @Override
419 public boolean process(OptionHelper helper, String option) {
420 if (option.endsWith(".java") ) {
421 File f = new File(option);
422 if (!f.exists()) {
423 helper.error("err.file.not.found", f);
424 return true;
425 }
426 if (!f.isFile()) {
427 helper.error("err.file.not.file", f);
428 return true;
429 }
430 helper.addFile(f);
431 } else {
432 helper.addClassName(option);
433 }
434 return false;
435 }
436 };
438 /** The kind of an Option. This is used by the -help and -X options. */
439 public enum OptionKind {
440 /** A standard option, documented by -help. */
441 STANDARD,
442 /** An extended option, documented by -X. */
443 EXTENDED,
444 /** A hidden option, not documented. */
445 HIDDEN,
446 }
448 /** The group for an Option. This determines the situations in which the
449 * option is applicable. */
450 enum OptionGroup {
451 /** A basic option, available for use on the command line or via the
452 * Compiler API. */
453 BASIC,
454 /** An option for javac's standard JavaFileManager. Other file managers
455 * may or may not support these options. */
456 FILEMANAGER,
457 /** A command-line option that requests information, such as -help. */
458 INFO,
459 /** A command-line "option" representing a file or class name. */
460 OPERAND
461 }
463 /** The kind of choice for "choice" options. */
464 enum ChoiceKind {
465 /** The expected value is exactly one of the set of choices. */
466 ONEOF,
467 /** The expected value is one of more of the set of choices. */
468 ANYOF
469 }
471 public final String text;
473 final OptionKind kind;
475 final OptionGroup group;
477 /** Documentation key for arguments.
478 */
479 final String argsNameKey;
481 /** Documentation key for description.
482 */
483 final String descrKey;
485 /** Suffix option (-foo=bar or -foo:bar)
486 */
487 final boolean hasSuffix;
489 /** The kind of choices for this option, if any.
490 */
491 final ChoiceKind choiceKind;
493 /** The choices for this option, if any, and whether or not the choices
494 * are hidden
495 */
496 final Map<String,Boolean> choices;
499 Option(String text, String descrKey,
500 OptionKind kind, OptionGroup group) {
501 this(text, null, descrKey, kind, group, null, null, false);
502 }
504 Option(String text, String argsNameKey, String descrKey,
505 OptionKind kind, OptionGroup group) {
506 this(text, argsNameKey, descrKey, kind, group, null, null, false);
507 }
509 Option(String text, String argsNameKey, String descrKey,
510 OptionKind kind, OptionGroup group, boolean doHasSuffix) {
511 this(text, argsNameKey, descrKey, kind, group, null, null, doHasSuffix);
512 }
514 Option(String text, String descrKey,
515 OptionKind kind, OptionGroup group,
516 ChoiceKind choiceKind, Map<String,Boolean> choices) {
517 this(text, null, descrKey, kind, group, choiceKind, choices, false);
518 }
520 Option(String text, String descrKey,
521 OptionKind kind, OptionGroup group,
522 ChoiceKind choiceKind, String... choices) {
523 this(text, null, descrKey, kind, group, choiceKind,
524 createChoices(choices), false);
525 }
526 // where
527 private static Map<String,Boolean> createChoices(String... choices) {
528 Map<String,Boolean> map = new LinkedHashMap<String,Boolean>();
529 for (String c: choices)
530 map.put(c, false);
531 return map;
532 }
534 private Option(String text, String argsNameKey, String descrKey,
535 OptionKind kind, OptionGroup group,
536 ChoiceKind choiceKind, Map<String,Boolean> choices,
537 boolean doHasSuffix) {
538 this.text = text;
539 this.argsNameKey = argsNameKey;
540 this.descrKey = descrKey;
541 this.kind = kind;
542 this.group = group;
543 this.choiceKind = choiceKind;
544 this.choices = choices;
545 char lastChar = text.charAt(text.length()-1);
546 this.hasSuffix = doHasSuffix || lastChar == ':' || lastChar == '=';
547 }
549 public String getText() {
550 return text;
551 }
553 public OptionKind getKind() {
554 return kind;
555 }
557 public boolean hasArg() {
558 return argsNameKey != null && !hasSuffix;
559 }
561 public boolean matches(String option) {
562 if (!hasSuffix)
563 return option.equals(text);
565 if (!option.startsWith(text))
566 return false;
568 if (choices != null) {
569 String arg = option.substring(text.length());
570 if (choiceKind == ChoiceKind.ONEOF)
571 return choices.keySet().contains(arg);
572 else {
573 for (String a: arg.split(",+")) {
574 if (!choices.keySet().contains(a))
575 return false;
576 }
577 }
578 }
580 return true;
581 }
583 public boolean process(OptionHelper helper, String option, String arg) {
584 if (choices != null) {
585 if (choiceKind == ChoiceKind.ONEOF) {
586 // some clients like to see just one of option+choice set
587 for (String s: choices.keySet())
588 helper.remove(option + s);
589 String opt = option + arg;
590 helper.put(opt, opt);
591 // some clients like to see option (without trailing ":")
592 // set to arg
593 String nm = option.substring(0, option.length() - 1);
594 helper.put(nm, arg);
595 } else {
596 // set option+word for each word in arg
597 for (String a: arg.split(",+")) {
598 String opt = option + a;
599 helper.put(opt, opt);
600 }
601 }
602 }
603 helper.put(option, arg);
604 return false;
605 }
607 public boolean process(OptionHelper helper, String option) {
608 if (hasSuffix)
609 return process(helper, text, option.substring(text.length()));
610 else
611 return process(helper, option, option);
612 }
614 void help(Log log, OptionKind kind) {
615 if (this.kind != kind)
616 return;
618 log.printRawLines(WriterKind.NOTICE,
619 String.format(" %-26s %s",
620 helpSynopsis(log),
621 log.localize(PrefixKind.JAVAC, descrKey)));
623 }
625 private String helpSynopsis(Log log) {
626 StringBuilder sb = new StringBuilder();
627 sb.append(text);
628 if (argsNameKey == null) {
629 if (choices != null) {
630 String sep = "{";
631 for (Map.Entry<String,Boolean> e: choices.entrySet()) {
632 if (!e.getValue()) {
633 sb.append(sep);
634 sb.append(e.getKey());
635 sep = ",";
636 }
637 }
638 sb.append("}");
639 }
640 } else {
641 if (!hasSuffix)
642 sb.append(" ");
643 sb.append(log.localize(PrefixKind.JAVAC, argsNameKey));
645 }
647 return sb.toString();
648 }
650 // For -XpkgInfo:value
651 public enum PkgInfo {
652 ALWAYS, LEGACY, NONEMPTY;
653 public static PkgInfo get(Options options) {
654 String v = options.get(XPKGINFO);
655 return (v == null
656 ? PkgInfo.LEGACY
657 : PkgInfo.valueOf(v.toUpperCase()));
658 }
659 }
661 private static Map<String,Boolean> getXLintChoices() {
662 Map<String,Boolean> choices = new LinkedHashMap<String,Boolean>();
663 choices.put("all", false);
664 for (Lint.LintCategory c : Lint.LintCategory.values())
665 choices.put(c.option, c.hidden);
666 for (Lint.LintCategory c : Lint.LintCategory.values())
667 choices.put("-" + c.option, c.hidden);
668 choices.put("none", false);
669 return choices;
670 }
672 static Set<Option> getJavaCompilerOptions() {
673 return EnumSet.allOf(Option.class);
674 }
676 public static Set<Option> getJavacFileManagerOptions() {
677 return getOptions(EnumSet.of(FILEMANAGER));
678 }
680 public static Set<Option> getJavacToolOptions() {
681 return getOptions(EnumSet.of(BASIC));
682 }
684 static Set<Option> getOptions(Set<OptionGroup> desired) {
685 Set<Option> options = EnumSet.noneOf(Option.class);
686 for (Option option : Option.values())
687 if (desired.contains(option.group))
688 options.add(option);
689 return Collections.unmodifiableSet(options);
690 }
692 }