Wed, 20 May 2009 13:36:23 -0700
6827026: Change javac source and target default to 7
Reviewed-by: darcy, ohair
1 /*
2 * Copyright 2006-2009 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.javac.main;
28 import com.sun.tools.javac.code.Lint;
29 import com.sun.tools.javac.code.Source;
30 import com.sun.tools.javac.code.Type;
31 import com.sun.tools.javac.jvm.Target;
32 import com.sun.tools.javac.main.JavacOption.HiddenOption;
33 import com.sun.tools.javac.main.JavacOption.Option;
34 import com.sun.tools.javac.main.JavacOption.XOption;
35 import com.sun.tools.javac.util.ListBuffer;
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.Collection;
42 import java.util.EnumSet;
43 import java.util.LinkedHashSet;
44 import java.util.Set;
45 import javax.lang.model.SourceVersion;
47 import static com.sun.tools.javac.main.OptionName.*;
49 /**
50 * TODO: describe com.sun.tools.javac.main.RecognizedOptions
51 *
52 * <p><b>This is NOT part of any API supported by Sun Microsystems.
53 * If you write code that depends on this, you do so at your own
54 * risk. This code and its internal interfaces are subject to change
55 * or deletion without notice.</b></p>
56 */
57 public class RecognizedOptions {
59 private RecognizedOptions() {}
61 public interface OptionHelper {
63 void setOut(PrintWriter out);
65 void error(String key, Object... args);
67 void printVersion();
69 void printFullVersion();
71 void printHelp();
73 void printXhelp();
75 void addFile(File f);
77 void addClassName(String s);
79 }
81 public static class GrumpyHelper implements OptionHelper {
83 public void setOut(PrintWriter out) {
84 throw new IllegalArgumentException();
85 }
87 public void error(String key, Object... args) {
88 throw new IllegalArgumentException(Main.getLocalizedString(key, args));
89 }
91 public void printVersion() {
92 throw new IllegalArgumentException();
93 }
95 public void printFullVersion() {
96 throw new IllegalArgumentException();
97 }
99 public void printHelp() {
100 throw new IllegalArgumentException();
101 }
103 public void printXhelp() {
104 throw new IllegalArgumentException();
105 }
107 public void addFile(File f) {
108 throw new IllegalArgumentException(f.getPath());
109 }
111 public void addClassName(String s) {
112 throw new IllegalArgumentException(s);
113 }
115 }
117 static Set<OptionName> javacOptions = EnumSet.of(
118 G,
119 G_NONE,
120 G_CUSTOM,
121 XLINT,
122 XLINT_CUSTOM,
123 NOWARN,
124 VERBOSE,
125 DEPRECATION,
126 CLASSPATH,
127 CP,
128 SOURCEPATH,
129 BOOTCLASSPATH,
130 XBOOTCLASSPATH_PREPEND,
131 XBOOTCLASSPATH_APPEND,
132 XBOOTCLASSPATH,
133 EXTDIRS,
134 DJAVA_EXT_DIRS,
135 ENDORSEDDIRS,
136 DJAVA_ENDORSED_DIRS,
137 PROC,
138 PROCESSOR,
139 PROCESSORPATH,
140 D,
141 S,
142 IMPLICIT,
143 ENCODING,
144 SOURCE,
145 TARGET,
146 VERSION,
147 FULLVERSION,
148 DIAGS,
149 HELP,
150 A,
151 X,
152 J,
153 MOREINFO,
154 WERROR,
155 // COMPLEXINFERENCE,
156 PROMPT,
157 DOE,
158 PRINTSOURCE,
159 WARNUNCHECKED,
160 XMAXERRS,
161 XMAXWARNS,
162 XSTDOUT,
163 XPRINT,
164 XPRINTROUNDS,
165 XPRINTPROCESSORINFO,
166 XPREFER,
167 O,
168 XJCOV,
169 XD,
170 SOURCEFILE);
172 static Set<OptionName> javacFileManagerOptions = EnumSet.of(
173 CLASSPATH,
174 CP,
175 SOURCEPATH,
176 BOOTCLASSPATH,
177 XBOOTCLASSPATH_PREPEND,
178 XBOOTCLASSPATH_APPEND,
179 XBOOTCLASSPATH,
180 EXTDIRS,
181 DJAVA_EXT_DIRS,
182 ENDORSEDDIRS,
183 DJAVA_ENDORSED_DIRS,
184 PROCESSORPATH,
185 D,
186 S,
187 ENCODING,
188 SOURCE);
190 static Set<OptionName> javacToolOptions = EnumSet.of(
191 G,
192 G_NONE,
193 G_CUSTOM,
194 XLINT,
195 XLINT_CUSTOM,
196 NOWARN,
197 VERBOSE,
198 DEPRECATION,
199 PROC,
200 PROCESSOR,
201 IMPLICIT,
202 SOURCE,
203 TARGET,
204 // VERSION,
205 // FULLVERSION,
206 // HELP,
207 A,
208 // X,
209 // J,
210 MOREINFO,
211 WERROR,
212 // COMPLEXINFERENCE,
213 PROMPT,
214 DOE,
215 PRINTSOURCE,
216 WARNUNCHECKED,
217 XMAXERRS,
218 XMAXWARNS,
219 // XSTDOUT,
220 XPRINT,
221 XPRINTROUNDS,
222 XPRINTPROCESSORINFO,
223 XPREFER,
224 O,
225 XJCOV,
226 XD);
228 static Option[] getJavaCompilerOptions(OptionHelper helper) {
229 return getOptions(helper, javacOptions);
230 }
232 public static Option[] getJavacFileManagerOptions(OptionHelper helper) {
233 return getOptions(helper, javacFileManagerOptions);
234 }
236 public static Option[] getJavacToolOptions(OptionHelper helper) {
237 return getOptions(helper, javacToolOptions);
238 }
240 static Option[] getOptions(OptionHelper helper, Set<OptionName> desired) {
241 ListBuffer<Option> options = new ListBuffer<Option>();
242 for (Option option : getAll(helper))
243 if (desired.contains(option.getName()))
244 options.append(option);
245 return options.toArray(new Option[options.length()]);
246 }
248 /**
249 * Get all the recognized options.
250 * @param helper an {@code OptionHelper} to help when processing options
251 * @return an array of options
252 */
253 public static Option[] getAll(final OptionHelper helper) {
254 return new Option[] {
255 new Option(G, "opt.g"),
256 new Option(G_NONE, "opt.g.none") {
257 @Override
258 public boolean process(Options options, String option) {
259 options.put("-g:", "none");
260 return false;
261 }
262 },
264 new Option(G_CUSTOM, "opt.g.lines.vars.source",
265 Option.ChoiceKind.ANYOF, "lines", "vars", "source"),
267 new XOption(XLINT, "opt.Xlint"),
268 new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist",
269 Option.ChoiceKind.ANYOF, getXLintChoices()),
271 // -nowarn is retained for command-line backward compatibility
272 new Option(NOWARN, "opt.nowarn") {
273 @Override
274 public boolean process(Options options, String option) {
275 options.put("-Xlint:none", option);
276 return false;
277 }
278 },
280 new Option(VERBOSE, "opt.verbose"),
282 // -deprecation is retained for command-line backward compatibility
283 new Option(DEPRECATION, "opt.deprecation") {
284 @Override
285 public boolean process(Options options, String option) {
286 options.put("-Xlint:deprecation", option);
287 return false;
288 }
289 },
291 new Option(CLASSPATH, "opt.arg.path", "opt.classpath"),
292 new Option(CP, "opt.arg.path", "opt.classpath") {
293 @Override
294 public boolean process(Options options, String option, String arg) {
295 return super.process(options, "-classpath", arg);
296 }
297 },
298 new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath"),
299 new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
300 @Override
301 public boolean process(Options options, String option, String arg) {
302 options.remove("-Xbootclasspath/p:");
303 options.remove("-Xbootclasspath/a:");
304 return super.process(options, option, arg);
305 }
306 },
307 new XOption(XBOOTCLASSPATH_PREPEND,"opt.arg.path", "opt.Xbootclasspath.p"),
308 new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"),
309 new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
310 @Override
311 public boolean process(Options options, String option, String arg) {
312 options.remove("-Xbootclasspath/p:");
313 options.remove("-Xbootclasspath/a:");
314 return super.process(options, "-bootclasspath", arg);
315 }
316 },
317 new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"),
318 new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {
319 @Override
320 public boolean process(Options options, String option, String arg) {
321 return super.process(options, "-extdirs", arg);
322 }
323 },
324 new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"),
325 new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {
326 @Override
327 public boolean process(Options options, String option, String arg) {
328 return super.process(options, "-endorseddirs", arg);
329 }
330 },
331 new Option(PROC, "opt.proc.none.only",
332 Option.ChoiceKind.ONEOF, "none", "only"),
333 new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"),
334 new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"),
335 new Option(D, "opt.arg.directory", "opt.d"),
336 new Option(S, "opt.arg.directory", "opt.sourceDest"),
337 new Option(IMPLICIT, "opt.implicit",
338 Option.ChoiceKind.ONEOF, "none", "class"),
339 new Option(ENCODING, "opt.arg.encoding", "opt.encoding"),
340 new Option(SOURCE, "opt.arg.release", "opt.source") {
341 @Override
342 public boolean process(Options options, String option, String operand) {
343 Source source = Source.lookup(operand);
344 if (source == null) {
345 helper.error("err.invalid.source", operand);
346 return true;
347 }
348 return super.process(options, option, operand);
349 }
350 },
351 new Option(TARGET, "opt.arg.release", "opt.target") {
352 @Override
353 public boolean process(Options options, String option, String operand) {
354 Target target = Target.lookup(operand);
355 if (target == null) {
356 helper.error("err.invalid.target", operand);
357 return true;
358 }
359 return super.process(options, option, operand);
360 }
361 },
362 new Option(VERSION, "opt.version") {
363 @Override
364 public boolean process(Options options, String option) {
365 helper.printVersion();
366 return super.process(options, option);
367 }
368 },
369 new HiddenOption(FULLVERSION) {
370 @Override
371 public boolean process(Options options, String option) {
372 helper.printFullVersion();
373 return super.process(options, option);
374 }
375 },
376 new HiddenOption(DIAGS) {
377 @Override
378 public boolean process(Options options, String option) {
379 Option xd = getOptions(helper, EnumSet.of(XD))[0];
380 option = option.substring(option.indexOf('=') + 1);
381 String diagsOption = option.contains("%") ?
382 "-XDdiagsFormat=" :
383 "-XDdiags=";
384 diagsOption += option;
385 if (xd.matches(diagsOption))
386 return xd.process(options, diagsOption);
387 else
388 return false;
389 }
390 },
391 new Option(HELP, "opt.help") {
392 @Override
393 public boolean process(Options options, String option) {
394 helper.printHelp();
395 return super.process(options, option);
396 }
397 },
398 new Option(A, "opt.arg.key.equals.value","opt.A") {
399 @Override
400 String helpSynopsis() {
401 hasSuffix = true;
402 return super.helpSynopsis();
403 }
405 @Override
406 public boolean matches(String arg) {
407 return arg.startsWith("-A");
408 }
410 @Override
411 public boolean hasArg() {
412 return false;
413 }
414 // Mapping for processor options created in
415 // JavacProcessingEnvironment
416 @Override
417 public boolean process(Options options, String option) {
418 int argLength = option.length();
419 if (argLength == 2) {
420 helper.error("err.empty.A.argument");
421 return true;
422 }
423 int sepIndex = option.indexOf('=');
424 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
425 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
426 helper.error("err.invalid.A.key", option);
427 return true;
428 }
429 return process(options, option, option);
430 }
431 },
432 new Option(X, "opt.X") {
433 @Override
434 public boolean process(Options options, String option) {
435 helper.printXhelp();
436 return super.process(options, option);
437 }
438 },
440 // This option exists only for the purpose of documenting itself.
441 // It's actually implemented by the launcher.
442 new Option(J, "opt.arg.flag", "opt.J") {
443 @Override
444 String helpSynopsis() {
445 hasSuffix = true;
446 return super.helpSynopsis();
447 }
448 @Override
449 public boolean process(Options options, String option) {
450 throw new AssertionError
451 ("the -J flag should be caught by the launcher.");
452 }
453 },
455 // stop after parsing and attributing.
456 // new HiddenOption("-attrparseonly"),
458 // new Option("-moreinfo", "opt.moreinfo") {
459 new HiddenOption(MOREINFO) {
460 @Override
461 public boolean process(Options options, String option) {
462 Type.moreInfo = true;
463 return super.process(options, option);
464 }
465 },
467 // treat warnings as errors
468 new Option(WERROR, "opt.Werror"),
470 // use complex inference from context in the position of a method call argument
471 new HiddenOption(COMPLEXINFERENCE),
473 // generare source stubs
474 // new HiddenOption("-stubs"),
476 // relax some constraints to allow compiling from stubs
477 // new HiddenOption("-relax"),
479 // output source after translating away inner classes
480 // new Option("-printflat", "opt.printflat"),
481 // new HiddenOption("-printflat"),
483 // display scope search details
484 // new Option("-printsearch", "opt.printsearch"),
485 // new HiddenOption("-printsearch"),
487 // prompt after each error
488 // new Option("-prompt", "opt.prompt"),
489 new HiddenOption(PROMPT),
491 // dump stack on error
492 new HiddenOption(DOE),
494 // output source after type erasure
495 // new Option("-s", "opt.s"),
496 new HiddenOption(PRINTSOURCE),
498 // output shrouded class files
499 // new Option("-scramble", "opt.scramble"),
500 // new Option("-scrambleall", "opt.scrambleall"),
502 // display warnings for generic unchecked operations
503 new HiddenOption(WARNUNCHECKED) {
504 @Override
505 public boolean process(Options options, String option) {
506 options.put("-Xlint:unchecked", option);
507 return false;
508 }
509 },
511 new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"),
512 new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"),
513 new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
514 @Override
515 public boolean process(Options options, String option, String arg) {
516 try {
517 helper.setOut(new PrintWriter(new FileWriter(arg), true));
518 } catch (java.io.IOException e) {
519 helper.error("err.error.writing.file", arg, e);
520 return true;
521 }
522 return super.process(options, option, arg);
523 }
524 },
526 new XOption(XPRINT, "opt.print"),
528 new XOption(XPRINTROUNDS, "opt.printRounds"),
530 new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"),
532 new XOption(XPREFER, "opt.prefer",
533 Option.ChoiceKind.ONEOF, "source", "newer"),
535 /* -O is a no-op, accepted for backward compatibility. */
536 new HiddenOption(O),
538 /* -Xjcov produces tables to support the code coverage tool jcov. */
539 new HiddenOption(XJCOV),
541 /* This is a back door to the compiler's option table.
542 * -XDx=y sets the option x to the value y.
543 * -XDx sets the option x to the value x.
544 */
545 new HiddenOption(XD) {
546 String s;
547 @Override
548 public boolean matches(String s) {
549 this.s = s;
550 return s.startsWith(name.optionName);
551 }
552 @Override
553 public boolean process(Options options, String option) {
554 s = s.substring(name.optionName.length());
555 int eq = s.indexOf('=');
556 String key = (eq < 0) ? s : s.substring(0, eq);
557 String value = (eq < 0) ? s : s.substring(eq+1);
558 options.put(key, value);
559 return false;
560 }
561 },
563 /*
564 * TODO: With apt, the matches method accepts anything if
565 * -XclassAsDecls is used; code elsewhere does the lookup to
566 * see if the class name is both legal and found.
567 *
568 * In apt, the process method adds the candiate class file
569 * name to a separate list.
570 */
571 new HiddenOption(SOURCEFILE) {
572 String s;
573 @Override
574 public boolean matches(String s) {
575 this.s = s;
576 return s.endsWith(".java") // Java source file
577 || SourceVersion.isName(s); // Legal type name
578 }
579 @Override
580 public boolean process(Options options, String option) {
581 if (s.endsWith(".java") ) {
582 File f = new File(s);
583 if (!f.exists()) {
584 helper.error("err.file.not.found", f);
585 return true;
586 }
587 if (!f.isFile()) {
588 helper.error("err.file.not.file", f);
589 return true;
590 }
591 helper.addFile(f);
592 }
593 else
594 helper.addClassName(s);
595 return false;
596 }
597 },
598 };
599 }
601 private static Collection<String> getXLintChoices() {
602 Collection<String> choices = new LinkedHashSet<String>();
603 choices.add("all");
604 for (Lint.LintCategory c : Lint.LintCategory.values())
605 choices.add(c.option);
606 for (Lint.LintCategory c : Lint.LintCategory.values())
607 choices.add("-" + c.option);
608 choices.add("none");
609 return choices;
610 }
612 }