Tue, 13 Dec 2011 14:33:39 -0800
7121164: renamed files not committed
Reviewed-by: ksrini
1 /*
2 * Copyright (c) 2006, 2011, 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 IMPLICIT("-implicit:", "opt.implicit", STANDARD, BASIC, ONEOF, "none", "class"),
165 ENCODING("-encoding", "opt.arg.encoding", "opt.encoding", STANDARD, FILEMANAGER) {
166 @Override
167 public boolean process(OptionHelper helper, String option, String operand) {
168 // System.err.println("process encoding " + operand);
169 return super.process(helper, option, operand);
170 }
172 },
174 SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) {
175 @Override
176 public boolean process(OptionHelper helper, String option, String operand) {
177 Source source = Source.lookup(operand);
178 if (source == null) {
179 helper.error("err.invalid.source", operand);
180 return true;
181 }
182 return super.process(helper, option, operand);
183 }
184 },
186 TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) {
187 @Override
188 public boolean process(OptionHelper helper, String option, String operand) {
189 Target target = Target.lookup(operand);
190 if (target == null) {
191 helper.error("err.invalid.target", operand);
192 return true;
193 }
194 return super.process(helper, option, operand);
195 }
196 },
198 VERSION("-version", "opt.version", STANDARD, INFO) {
199 @Override
200 public boolean process(OptionHelper helper, String option) {
201 Log log = helper.getLog();
202 String ownName = helper.getOwnName();
203 log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version());
204 return super.process(helper, option);
205 }
206 },
208 FULLVERSION("-fullversion", null, HIDDEN, INFO) {
209 @Override
210 public boolean process(OptionHelper helper, String option) {
211 Log log = helper.getLog();
212 String ownName = helper.getOwnName();
213 log.printLines(PrefixKind.JAVAC, "fullversion", ownName, JavaCompiler.fullVersion());
214 return super.process(helper, option);
215 }
216 },
218 DIAGS("-XDdiags=", null, HIDDEN, INFO) {
219 @Override
220 public boolean process(OptionHelper helper, String option) {
221 option = option.substring(option.indexOf('=') + 1);
222 String diagsOption = option.contains("%") ?
223 "-XDdiagsFormat=" :
224 "-XDdiags=";
225 diagsOption += option;
226 if (XD.matches(diagsOption))
227 return XD.process(helper, diagsOption);
228 else
229 return false;
230 }
231 },
233 HELP("-help", "opt.help", STANDARD, INFO) {
234 @Override
235 public boolean process(OptionHelper helper, String option) {
236 Log log = helper.getLog();
237 String ownName = helper.getOwnName();
238 log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName);
239 for (Option o: getJavaCompilerOptions()) {
240 o.help(log, OptionKind.STANDARD);
241 }
242 log.printNewline();
243 return super.process(helper, option);
244 }
245 },
247 A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC) {
248 { hasSuffix = true; }
250 @Override
251 public boolean matches(String arg) {
252 return arg.startsWith("-A");
253 }
255 @Override
256 public boolean hasArg() {
257 return false;
258 }
259 // Mapping for processor options created in
260 // JavacProcessingEnvironment
261 @Override
262 public boolean process(OptionHelper helper, String option) {
263 int argLength = option.length();
264 if (argLength == 2) {
265 helper.error("err.empty.A.argument");
266 return true;
267 }
268 int sepIndex = option.indexOf('=');
269 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
270 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
271 helper.error("err.invalid.A.key", option);
272 return true;
273 }
274 return process(helper, option, option);
275 }
276 },
278 X("-X", "opt.X", STANDARD, INFO) {
279 @Override
280 public boolean process(OptionHelper helper, String option) {
281 Log log = helper.getLog();
282 for (Option o: getJavaCompilerOptions()) {
283 o.help(log, OptionKind.EXTENDED);
284 }
285 log.printNewline();
286 log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer");
287 return super.process(helper, option);
288 }
289 },
291 // This option exists only for the purpose of documenting itself.
292 // It's actually implemented by the launcher.
293 J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO) {
294 { hasSuffix = true; }
296 @Override
297 public boolean process(OptionHelper helper, String option) {
298 throw new AssertionError
299 ("the -J flag should be caught by the launcher.");
300 }
301 },
303 // stop after parsing and attributing.
304 // new HiddenOption("-attrparseonly"),
306 // new Option("-moreinfo", "opt.moreinfo") {
307 MOREINFO("-moreinfo", null, HIDDEN, BASIC) {
308 @Override
309 public boolean process(OptionHelper helper, String option) {
310 Type.moreInfo = true;
311 return super.process(helper, option);
312 }
313 },
315 // treat warnings as errors
316 WERROR("-Werror", "opt.Werror", STANDARD, BASIC),
318 // // use complex inference from context in the position of a method call argument
319 // COMPLEXINFERENCE("-complexinference", null, HIDDEN, BASIC),
321 // generare source stubs
322 // new HiddenOption("-stubs"),
324 // relax some constraints to allow compiling from stubs
325 // new HiddenOption("-relax"),
327 // output source after translating away inner classes
328 // new Option("-printflat", "opt.printflat"),
329 // new HiddenOption("-printflat"),
331 // display scope search details
332 // new Option("-printsearch", "opt.printsearch"),
333 // new HiddenOption("-printsearch"),
335 // prompt after each error
336 // new Option("-prompt", "opt.prompt"),
337 PROMPT("-prompt", null, HIDDEN, BASIC),
339 // dump stack on error
340 DOE("-doe", null, HIDDEN, BASIC),
342 // output source after type erasure
343 // new Option("-s", "opt.s"),
344 PRINTSOURCE("-printsource", null, HIDDEN, BASIC),
346 // output shrouded class files
347 // new Option("-scramble", "opt.scramble"),
348 // new Option("-scrambleall", "opt.scrambleall"),
350 // display warnings for generic unchecked operations
351 WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) {
352 @Override
353 public boolean process(OptionHelper helper, String option) {
354 helper.put("-Xlint:unchecked", option);
355 return false;
356 }
357 },
359 XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC),
361 XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC),
363 XSTDOUT("Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) {
364 @Override
365 public boolean process(OptionHelper helper, String option, String arg) {
366 try {
367 Log log = helper.getLog();
368 // TODO: this file should be closed at the end of compilation
369 log.setWriters(new PrintWriter(new FileWriter(arg), true));
370 } catch (java.io.IOException e) {
371 helper.error("err.error.writing.file", arg, e);
372 return true;
373 }
374 return super.process(helper, option, arg);
375 }
376 },
378 XPRINT("-Xprint", "opt.print", EXTENDED, BASIC),
380 XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC),
382 XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC),
384 XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"),
386 XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"),
388 /* -O is a no-op, accepted for backward compatibility. */
389 O("-O", null, HIDDEN, BASIC),
391 /* -Xjcov produces tables to support the code coverage tool jcov. */
392 XJCOV("-Xjcov", null, HIDDEN, BASIC),
394 /* This is a back door to the compiler's option table.
395 * -XDx=y sets the option x to the value y.
396 * -XDx sets the option x to the value x.
397 */
398 XD("-XD", null, HIDDEN, BASIC) {
399 String s;
400 @Override
401 public boolean matches(String s) {
402 this.s = s;
403 return s.startsWith(text);
404 }
405 @Override
406 public boolean process(OptionHelper helper, String option) {
407 s = s.substring(text.length());
408 int eq = s.indexOf('=');
409 String key = (eq < 0) ? s : s.substring(0, eq);
410 String value = (eq < 0) ? s : s.substring(eq+1);
411 helper.put(key, value);
412 return false;
413 }
414 },
416 // This option exists only for the purpose of documenting itself.
417 // It's actually implemented by the CommandLine class.
418 AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO) {
419 { hasSuffix = true; }
421 @Override
422 public boolean process(OptionHelper helper, String option) {
423 throw new AssertionError("the @ flag should be caught by CommandLine.");
424 }
425 },
427 /*
428 * TODO: With apt, the matches method accepts anything if
429 * -XclassAsDecls is used; code elsewhere does the lookup to
430 * see if the class name is both legal and found.
431 *
432 * In apt, the process method adds the candidate class file
433 * name to a separate list.
434 */
435 SOURCEFILE("sourcefile", null, HIDDEN, INFO) {
436 String s;
437 @Override
438 public boolean matches(String s) {
439 this.s = s;
440 return s.endsWith(".java") // Java source file
441 || SourceVersion.isName(s); // Legal type name
442 }
443 @Override
444 public boolean process(OptionHelper helper, String option) {
445 if (s.endsWith(".java") ) {
446 File f = new File(s);
447 if (!f.exists()) {
448 helper.error("err.file.not.found", f);
449 return true;
450 }
451 if (!f.isFile()) {
452 helper.error("err.file.not.file", f);
453 return true;
454 }
455 helper.addFile(f);
456 }
457 else
458 helper.addClassName(s);
459 return false;
460 }
461 };
463 /** The kind of an Option. This is used by the -help and -X options. */
464 public enum OptionKind {
465 /** A standard option, documented by -help. */
466 STANDARD,
467 /** An extended option, documented by -X. */
468 EXTENDED,
469 /** A hidden option, not documented. */
470 HIDDEN,
471 }
473 /** The group for an Option. This determines the situations in which the
474 * option is applicable. */
475 enum OptionGroup {
476 /** A basic option, available for use on the command line or via the
477 * Compiler API. */
478 BASIC,
479 /** An option for javac's standard JavaFileManager. Other file managers
480 * may or may not support these options. */
481 FILEMANAGER,
482 /** A command-line option that requests information, such as -help. */
483 INFO,
484 /** A command-line "option" representing a file or class name. */
485 OPERAND
486 }
488 /** The kind of choice for "choice" options. */
489 enum ChoiceKind {
490 /** The expected value is exactly one of the set of choices. */
491 ONEOF,
492 /** The expected value is one of more of the set of choices. */
493 ANYOF
494 }
496 public final String text;
498 final OptionKind kind;
500 final OptionGroup group;
502 /** Documentation key for arguments.
503 */
504 final String argsNameKey;
506 /** Documentation key for description.
507 */
508 final String descrKey;
510 /** Suffix option (-foo=bar or -foo:bar)
511 */
512 boolean hasSuffix;
514 /** The kind of choices for this option, if any.
515 */
516 final ChoiceKind choiceKind;
518 /** The choices for this option, if any, and whether or not the choices
519 * are hidden
520 */
521 final Map<String,Boolean> choices;
524 Option(String text, String descrKey,
525 OptionKind kind, OptionGroup group) {
526 this(text, null, descrKey, kind, group, null, null);
527 }
529 Option(String text, String argsNameKey, String descrKey,
530 OptionKind kind, OptionGroup group) {
531 this(text, argsNameKey, descrKey, kind, group, null, null);
532 }
534 Option(String text, String descrKey,
535 OptionKind kind, OptionGroup group,
536 ChoiceKind choiceKind, Map<String,Boolean> choices) {
537 this(text, null, descrKey, kind, group, choiceKind, choices);
538 }
540 Option(String text, String descrKey,
541 OptionKind kind, OptionGroup group,
542 ChoiceKind choiceKind, String... choices) {
543 this(text, null, descrKey, kind, group, choiceKind, createChoices(choices));
544 }
545 // where
546 private static Map<String,Boolean> createChoices(String... choices) {
547 Map<String,Boolean> map = new LinkedHashMap<String,Boolean>();
548 for (String c: choices)
549 map.put(c, false);
550 return map;
551 }
553 private Option(String text, String argsNameKey, String descrKey,
554 OptionKind kind, OptionGroup group,
555 ChoiceKind choiceKind, Map<String,Boolean> choices) {
556 this.text = text;
557 this.argsNameKey = argsNameKey;
558 this.descrKey = descrKey;
559 this.kind = kind;
560 this.group = group;
561 this.choiceKind = choiceKind;
562 this.choices = choices;
563 char lastChar = text.charAt(text.length()-1);
564 hasSuffix = lastChar == ':' || lastChar == '=';
565 }
567 public String getText() {
568 return text;
569 }
571 public OptionKind getKind() {
572 return kind;
573 }
575 public boolean hasArg() {
576 return argsNameKey != null && !hasSuffix;
577 }
579 public boolean matches(String option) {
580 if (!hasSuffix)
581 return option.equals(text);
583 if (!option.startsWith(text))
584 return false;
586 if (choices != null) {
587 String arg = option.substring(text.length());
588 if (choiceKind == ChoiceKind.ONEOF)
589 return choices.keySet().contains(arg);
590 else {
591 for (String a: arg.split(",+")) {
592 if (!choices.keySet().contains(a))
593 return false;
594 }
595 }
596 }
598 return true;
599 }
601 public boolean process(OptionHelper helper, String option, String arg) {
602 if (choices != null) {
603 if (choiceKind == ChoiceKind.ONEOF) {
604 // some clients like to see just one of option+choice set
605 for (String s: choices.keySet())
606 helper.remove(option + s);
607 String opt = option + arg;
608 helper.put(opt, opt);
609 // some clients like to see option (without trailing ":")
610 // set to arg
611 String nm = option.substring(0, option.length() - 1);
612 helper.put(nm, arg);
613 } else {
614 // set option+word for each word in arg
615 for (String a: arg.split(",+")) {
616 String opt = option + a;
617 helper.put(opt, opt);
618 }
619 }
620 }
621 helper.put(option, arg);
622 return false;
623 }
625 public boolean process(OptionHelper helper, String option) {
626 if (hasSuffix)
627 return process(helper, text, option.substring(text.length()));
628 else
629 return process(helper, option, option);
630 }
632 void help(Log log, OptionKind kind) {
633 if (this.kind != kind)
634 return;
636 log.printRawLines(WriterKind.NOTICE,
637 String.format(" %-26s %s",
638 helpSynopsis(log),
639 log.localize(PrefixKind.JAVAC, descrKey)));
641 }
643 private String helpSynopsis(Log log) {
644 StringBuilder sb = new StringBuilder();
645 sb.append(text);
646 if (argsNameKey == null) {
647 if (choices != null) {
648 String sep = "{";
649 for (Map.Entry<String,Boolean> e: choices.entrySet()) {
650 if (!e.getValue()) {
651 sb.append(sep);
652 sb.append(e.getKey());
653 sep = ",";
654 }
655 }
656 sb.append("}");
657 }
658 } else {
659 if (!hasSuffix)
660 sb.append(" ");
661 sb.append(log.localize(PrefixKind.JAVAC, argsNameKey));
663 }
665 return sb.toString();
666 }
668 // For -XpkgInfo:value
669 public enum PkgInfo {
670 ALWAYS, LEGACY, NONEMPTY;
671 public static PkgInfo get(Options options) {
672 String v = options.get(XPKGINFO);
673 return (v == null
674 ? PkgInfo.LEGACY
675 : PkgInfo.valueOf(v.toUpperCase()));
676 }
677 }
679 private static Map<String,Boolean> getXLintChoices() {
680 Map<String,Boolean> choices = new LinkedHashMap<String,Boolean>();
681 choices.put("all", false);
682 for (Lint.LintCategory c : Lint.LintCategory.values())
683 choices.put(c.option, c.hidden);
684 for (Lint.LintCategory c : Lint.LintCategory.values())
685 choices.put("-" + c.option, c.hidden);
686 choices.put("none", false);
687 return choices;
688 }
690 static Set<Option> getJavaCompilerOptions() {
691 return EnumSet.allOf(Option.class);
692 }
694 public static Set<Option> getJavacFileManagerOptions() {
695 return getOptions(EnumSet.of(FILEMANAGER));
696 }
698 public static Set<Option> getJavacToolOptions() {
699 return getOptions(EnumSet.of(BASIC));
700 }
702 static Set<Option> getOptions(Set<OptionGroup> desired) {
703 Set<Option> options = EnumSet.noneOf(Option.class);
704 for (Option option : Option.values())
705 if (desired.contains(option.group))
706 options.add(option);
707 return Collections.unmodifiableSet(options);
708 }
710 }