001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.main;
027:
028: import com.sun.tools.javac.code.Source;
029: import com.sun.tools.javac.code.Type;
030: import com.sun.tools.javac.jvm.Target;
031: import com.sun.tools.javac.main.JavacOption.HiddenOption;
032: import com.sun.tools.javac.main.JavacOption.Option;
033: import com.sun.tools.javac.main.JavacOption.XOption;
034: import com.sun.tools.javac.util.List;
035: import com.sun.tools.javac.util.ListBuffer;
036: import com.sun.tools.javac.util.Log;
037: import com.sun.tools.javac.util.Options;
038: import com.sun.tools.javac.util.Version;
039: import com.sun.tools.javac.processing.JavacProcessingEnvironment;
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.io.PrintWriter;
043: import java.util.EnumSet;
044: import java.util.Set;
045: import java.util.StringTokenizer;
046: import javax.lang.model.SourceVersion;
047:
048: import static com.sun.tools.javac.main.OptionName.*;
049:
050: /**
051: * TODO: describe com.sun.tools.javac.main.RecognizedOptions
052: *
053: * <p><b>This is NOT part of any API supported by Sun Microsystems.
054: * If you write code that depends on this, you do so at your own
055: * risk. This code and its internal interfaces are subject to change
056: * or deletion without notice.</b></p>
057: */
058: @Version("@(#)RecognizedOptions.java 1.13 07/05/05")
059: public class RecognizedOptions {
060:
061: private RecognizedOptions() {
062: }
063:
064: public interface OptionHelper {
065:
066: void setOut(PrintWriter out);
067:
068: void error(String key, Object... args);
069:
070: void printVersion();
071:
072: void printFullVersion();
073:
074: void printHelp();
075:
076: void printXhelp();
077:
078: void addFile(File f);
079:
080: void addClassName(String s);
081:
082: }
083:
084: public static class GrumpyHelper implements OptionHelper {
085:
086: public void setOut(PrintWriter out) {
087: throw new IllegalArgumentException();
088: }
089:
090: public void error(String key, Object... args) {
091: throw new IllegalArgumentException(Main.getLocalizedString(
092: key, args));
093: }
094:
095: public void printVersion() {
096: throw new IllegalArgumentException();
097: }
098:
099: public void printFullVersion() {
100: throw new IllegalArgumentException();
101: }
102:
103: public void printHelp() {
104: throw new IllegalArgumentException();
105: }
106:
107: public void printXhelp() {
108: throw new IllegalArgumentException();
109: }
110:
111: public void addFile(File f) {
112: throw new IllegalArgumentException(f.getPath());
113: }
114:
115: public void addClassName(String s) {
116: throw new IllegalArgumentException(s);
117: }
118:
119: }
120:
121: static Set<OptionName> javacOptions = EnumSet.of(G, G_NONE,
122: G_CUSTOM, XLINT, XLINT_CUSTOM, NOWARN, VERBOSE,
123: DEPRECATION, CLASSPATH, CP, SOURCEPATH, BOOTCLASSPATH,
124: XBOOTCLASSPATH_PREPEND, XBOOTCLASSPATH_APPEND,
125: XBOOTCLASSPATH, EXTDIRS, DJAVA_EXT_DIRS, ENDORSEDDIRS,
126: DJAVA_ENDORSED_DIRS, PROC_CUSTOM, PROCESSOR, PROCESSORPATH,
127: D, S, IMPLICIT, ENCODING, SOURCE, TARGET, VERSION,
128: FULLVERSION, HELP, A, X, J, MOREINFO,
129: WERROR,
130: // COMPLEXINFERENCE,
131: PROMPT, DOE, PRINTSOURCE, WARNUNCHECKED, XMAXERRS,
132: XMAXWARNS, XSTDOUT, XPRINT, XPRINTROUNDS,
133: XPRINTPROCESSORINFO, XPREFER, O, XJCOV, XD, SOURCEFILE);
134:
135: static Set<OptionName> javacFileManagerOptions = EnumSet.of(
136: CLASSPATH, CP, SOURCEPATH, BOOTCLASSPATH,
137: XBOOTCLASSPATH_PREPEND, XBOOTCLASSPATH_APPEND,
138: XBOOTCLASSPATH, EXTDIRS, DJAVA_EXT_DIRS, ENDORSEDDIRS,
139: DJAVA_ENDORSED_DIRS, PROCESSORPATH, D, S, ENCODING, SOURCE);
140:
141: static Set<OptionName> javacToolOptions = EnumSet.of(G, G_NONE,
142: G_CUSTOM,
143: XLINT,
144: XLINT_CUSTOM,
145: NOWARN,
146: VERBOSE,
147: DEPRECATION,
148: PROC_CUSTOM,
149: PROCESSOR,
150: IMPLICIT,
151: SOURCE,
152: TARGET,
153: // VERSION,
154: // FULLVERSION,
155: // HELP,
156: A,
157: // X,
158: // J,
159: MOREINFO,
160: WERROR,
161: // COMPLEXINFERENCE,
162: PROMPT, DOE, PRINTSOURCE, WARNUNCHECKED, XMAXERRS,
163: XMAXWARNS,
164: // XSTDOUT,
165: XPRINT, XPRINTROUNDS, XPRINTPROCESSORINFO, XPREFER, O,
166: XJCOV, XD);
167:
168: static Option[] getJavaCompilerOptions(OptionHelper helper) {
169: return getOptions(helper, javacOptions);
170: }
171:
172: public static Option[] getJavacFileManagerOptions(
173: OptionHelper helper) {
174: return getOptions(helper, javacFileManagerOptions);
175: }
176:
177: public static Option[] getJavacToolOptions(OptionHelper helper) {
178: return getOptions(helper, javacToolOptions);
179: }
180:
181: static Option[] getOptions(OptionHelper helper,
182: Set<OptionName> desired) {
183: ListBuffer<Option> options = new ListBuffer<Option>();
184: for (Option option : getAll(helper))
185: if (desired.contains(option.getName()))
186: options.append(option);
187: return options.toArray(new Option[options.length()]);
188: }
189:
190: /**
191: * @param out the writer to use for diagnostic output
192: */
193: public static Option[] getAll(final OptionHelper helper) {
194: return new Option[] {
195: new Option(G, "opt.g"),
196: new Option(G_NONE, "opt.g.none") {
197: public boolean process(Options options,
198: String option) {
199: options.put("-g:", "none");
200: return false;
201: }
202: },
203:
204: new Option(G_CUSTOM, "opt.g.lines.vars.source") {
205: public boolean matches(String s) {
206: return s.startsWith("-g:");
207: }
208:
209: public boolean process(Options options,
210: String option) {
211: String suboptions = option.substring(3);
212: options.put("-g:", suboptions);
213: // enter all the -g suboptions as "-g:suboption"
214: for (StringTokenizer t = new StringTokenizer(
215: suboptions, ","); t.hasMoreTokens();) {
216: String tok = t.nextToken();
217: String opt = "-g:" + tok;
218: options.put(opt, opt);
219: }
220: return false;
221: }
222: },
223:
224: new XOption(XLINT, "opt.Xlint"),
225: new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist") {
226: public boolean matches(String s) {
227: return s.startsWith("-Xlint:");
228: }
229:
230: public boolean process(Options options,
231: String option) {
232: String suboptions = option.substring(7);
233: options.put("-Xlint:", suboptions);
234: // enter all the -Xlint suboptions as "-Xlint:suboption"
235: for (StringTokenizer t = new StringTokenizer(
236: suboptions, ","); t.hasMoreTokens();) {
237: String tok = t.nextToken();
238: String opt = "-Xlint:" + tok;
239: options.put(opt, opt);
240: }
241: return false;
242: }
243: },
244:
245: // -nowarn is retained for command-line backward compatibility
246: new Option(NOWARN, "opt.nowarn") {
247: public boolean process(Options options,
248: String option) {
249: options.put("-Xlint:none", option);
250: return false;
251: }
252: },
253:
254: new Option(VERBOSE, "opt.verbose"),
255:
256: // -deprecation is retained for command-line backward compatibility
257: new Option(DEPRECATION, "opt.deprecation") {
258: public boolean process(Options options,
259: String option) {
260: options.put("-Xlint:deprecation", option);
261: return false;
262: }
263: },
264:
265: new Option(CLASSPATH, "opt.arg.path", "opt.classpath"),
266: new Option(CP, "opt.arg.path", "opt.classpath") {
267: public boolean process(Options options,
268: String option, String arg) {
269: return super
270: .process(options, "-classpath", arg);
271: }
272: },
273: new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath"),
274: new Option(BOOTCLASSPATH, "opt.arg.path",
275: "opt.bootclasspath") {
276: public boolean process(Options options,
277: String option, String arg) {
278: options.remove("-Xbootclasspath/p:");
279: options.remove("-Xbootclasspath/a:");
280: return super .process(options, option, arg);
281: }
282: },
283: new XOption(XBOOTCLASSPATH_PREPEND, "opt.arg.path",
284: "opt.Xbootclasspath.p"),
285: new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path",
286: "opt.Xbootclasspath.a"),
287: new XOption(XBOOTCLASSPATH, "opt.arg.path",
288: "opt.bootclasspath") {
289: public boolean process(Options options,
290: String option, String arg) {
291: options.remove("-Xbootclasspath/p:");
292: options.remove("-Xbootclasspath/a:");
293: return super .process(options, "-bootclasspath",
294: arg);
295: }
296: },
297: new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"),
298: new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs",
299: "opt.extdirs") {
300: public boolean process(Options options,
301: String option, String arg) {
302: return super .process(options, "-extdirs", arg);
303: }
304: },
305: new Option(ENDORSEDDIRS, "opt.arg.dirs",
306: "opt.endorseddirs"),
307: new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs",
308: "opt.endorseddirs") {
309: public boolean process(Options options,
310: String option, String arg) {
311: return super .process(options, "-endorseddirs",
312: arg);
313: }
314: },
315: new Option(PROC_CUSTOM, "opt.proc.none.only") {
316: public boolean matches(String s) {
317: return s.equals("-proc:none")
318: || s.equals("-proc:only");
319: }
320:
321: public boolean process(Options options,
322: String option) {
323: if (option.equals("-proc:none")) {
324: options.remove("-proc:only");
325: } else {
326: options.remove("-proc:none");
327: }
328: options.put(option, option);
329: return false;
330: }
331: },
332: new Option(PROCESSOR, "opt.arg.class.list",
333: "opt.processor"),
334: new Option(PROCESSORPATH, "opt.arg.path",
335: "opt.processorpath"),
336: new Option(D, "opt.arg.directory", "opt.d"),
337: new Option(S, "opt.arg.directory", "opt.sourceDest"),
338: new Option(IMPLICIT, "opt.implicit") {
339: public boolean matches(String s) {
340: return s.equals("-implicit:none")
341: || s.equals("-implicit:class");
342: }
343:
344: public boolean process(Options options,
345: String option, String operand) {
346: int sep = option.indexOf(":");
347: options.put(option.substring(0, sep), option
348: .substring(sep + 1));
349: options.put(option, option);
350: return false;
351: }
352: },
353: new Option(ENCODING, "opt.arg.encoding", "opt.encoding"),
354: new Option(SOURCE, "opt.arg.release", "opt.source") {
355: public boolean process(Options options,
356: String option, String operand) {
357: Source source = Source.lookup(operand);
358: if (source == null) {
359: helper.error("err.invalid.source", operand);
360: return true;
361: }
362: return super .process(options, option, operand);
363: }
364: },
365: new Option(TARGET, "opt.arg.release", "opt.target") {
366: public boolean process(Options options,
367: String option, String operand) {
368: Target target = Target.lookup(operand);
369: if (target == null) {
370: helper.error("err.invalid.target", operand);
371: return true;
372: }
373: return super .process(options, option, operand);
374: }
375: },
376: new Option(VERSION, "opt.version") {
377: public boolean process(Options options,
378: String option) {
379: helper.printVersion();
380: return super .process(options, option);
381: }
382: },
383: new HiddenOption(FULLVERSION) {
384: public boolean process(Options options,
385: String option) {
386: helper.printFullVersion();
387: return super .process(options, option);
388: }
389: },
390: new Option(HELP, "opt.help") {
391: public boolean process(Options options,
392: String option) {
393: helper.printHelp();
394: return super .process(options, option);
395: }
396: },
397: new Option(A, "opt.arg.key.equals.value", "opt.A") {
398: String helpSynopsis() {
399: hasSuffix = true;
400: return super .helpSynopsis();
401: }
402:
403: public boolean matches(String arg) {
404: return arg.startsWith("-A");
405: }
406:
407: public boolean hasArg() {
408: return false;
409: }
410:
411: // Mapping for processor options created in
412: // JavacProcessingEnvironment
413: public boolean process(Options options,
414: String option) {
415: int argLength = option.length();
416: if (argLength == 2) {
417: helper.error("err.empty.A.argument");
418: return true;
419: }
420: int sepIndex = option.indexOf('=');
421: String key = option
422: .substring(2,
423: (sepIndex != -1 ? sepIndex
424: : argLength));
425: if (!JavacProcessingEnvironment
426: .isValidOptionName(key)) {
427: helper.error("err.invalid.A.key", option);
428: return true;
429: }
430: return process(options, option, option);
431: }
432: },
433: new Option(X, "opt.X") {
434: public boolean process(Options options,
435: String option) {
436: helper.printXhelp();
437: return super .process(options, option);
438: }
439: },
440:
441: // This option exists only for the purpose of documenting itself.
442: // It's actually implemented by the launcher.
443: new Option(J, "opt.arg.flag", "opt.J") {
444: String helpSynopsis() {
445: hasSuffix = true;
446: return super .helpSynopsis();
447: }
448:
449: public boolean process(Options options,
450: String option) {
451: throw new AssertionError(
452: "the -J flag should be caught by the launcher.");
453: }
454: },
455:
456: // stop after parsing and attributing.
457: // new HiddenOption("-attrparseonly"),
458:
459: // new Option("-moreinfo", "opt.moreinfo") {
460: new HiddenOption(MOREINFO) {
461: public boolean process(Options options,
462: String option) {
463: Type.moreInfo = true;
464: return super .process(options, option);
465: }
466: },
467:
468: // treat warnings as errors
469: new HiddenOption(WERROR),
470:
471: // use complex inference from context in the position of a method call argument
472: new HiddenOption(COMPLEXINFERENCE),
473:
474: // generare source stubs
475: // new HiddenOption("-stubs"),
476:
477: // relax some constraints to allow compiling from stubs
478: // new HiddenOption("-relax"),
479:
480: // output source after translating away inner classes
481: // new Option("-printflat", "opt.printflat"),
482: // new HiddenOption("-printflat"),
483:
484: // display scope search details
485: // new Option("-printsearch", "opt.printsearch"),
486: // new HiddenOption("-printsearch"),
487:
488: // prompt after each error
489: // new Option("-prompt", "opt.prompt"),
490: new HiddenOption(PROMPT),
491:
492: // dump stack on error
493: new HiddenOption(DOE),
494:
495: // output source after type erasure
496: // new Option("-s", "opt.s"),
497: new HiddenOption(PRINTSOURCE),
498:
499: // output shrouded class files
500: // new Option("-scramble", "opt.scramble"),
501: // new Option("-scrambleall", "opt.scrambleall"),
502:
503: // display warnings for generic unchecked operations
504: new HiddenOption(WARNUNCHECKED) {
505: public boolean process(Options options,
506: String option) {
507: options.put("-Xlint:unchecked", option);
508: return false;
509: }
510: },
511:
512: new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"),
513: new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"),
514: new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
515: public boolean process(Options options,
516: String option, String arg) {
517: try {
518: helper.setOut(new PrintWriter(
519: new FileWriter(arg), true));
520: } catch (java.io.IOException e) {
521: helper.error("err.error.writing.file", arg,
522: e);
523: return true;
524: }
525: return super .process(options, option, arg);
526: }
527: },
528:
529: new XOption(XPRINT, "opt.print"),
530:
531: new XOption(XPRINTROUNDS, "opt.printRounds"),
532:
533: new XOption(XPRINTPROCESSORINFO,
534: "opt.printProcessorInfo"),
535:
536: new XOption(XPREFER, "opt.prefer") {
537: public boolean matches(String s) {
538: return s.equals("-Xprefer:source")
539: || s.equals("-Xprefer:newer");
540: }
541:
542: public boolean process(Options options,
543: String option, String operand) {
544: int sep = option.indexOf(":");
545: options.put(option.substring(0, sep), option
546: .substring(sep + 1));
547: options.put(option, option);
548: return false;
549: }
550: },
551:
552: /* -O is a no-op, accepted for backward compatibility. */
553: new HiddenOption(O),
554:
555: /* -Xjcov produces tables to support the code coverage tool jcov. */
556: new HiddenOption(XJCOV),
557:
558: /* This is a back door to the compiler's option table.
559: * -XDx=y sets the option x to the value y.
560: * -XDx sets the option x to the value x.
561: */
562: new HiddenOption(XD) {
563: String s;
564:
565: public boolean matches(String s) {
566: this .s = s;
567: return s.startsWith(name.optionName);
568: }
569:
570: public boolean process(Options options,
571: String option) {
572: s = s.substring(name.optionName.length());
573: int eq = s.indexOf('=');
574: String key = (eq < 0) ? s : s.substring(0, eq);
575: String value = (eq < 0) ? s : s
576: .substring(eq + 1);
577: options.put(key, value);
578: return false;
579: }
580: },
581:
582: /*
583: * TODO: With apt, the matches method accepts anything if
584: * -XclassAsDecls is used; code elsewhere does the lookup to
585: * see if the class name is both legal and found.
586: *
587: * In apt, the process method adds the candiate class file
588: * name to a separate list.
589: */
590: new HiddenOption(SOURCEFILE) {
591: String s;
592:
593: public boolean matches(String s) {
594: this .s = s;
595: return s.endsWith(".java") // Java source file
596: || SourceVersion.isName(s); // Legal type name
597: }
598:
599: public boolean process(Options options,
600: String option) {
601: if (s.endsWith(".java")) {
602: File f = new File(s);
603: if (!f.exists()) {
604: helper.error("err.file.not.found", f);
605: return true;
606: }
607: if (!f.isFile()) {
608: helper.error("err.file.not.file", f);
609: return true;
610: }
611: helper.addFile(f);
612: } else
613: helper.addClassName(s);
614: return false;
615: }
616: }, };
617: }
618:
619: }
|