duke@1: /*
xdono@54: * Copyright 2006-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.main;
duke@1:
jjg@11: import com.sun.tools.javac.code.Lint;
duke@1: import com.sun.tools.javac.code.Source;
duke@1: import com.sun.tools.javac.code.Type;
duke@1: import com.sun.tools.javac.jvm.Target;
duke@1: import com.sun.tools.javac.main.JavacOption.HiddenOption;
duke@1: import com.sun.tools.javac.main.JavacOption.Option;
duke@1: import com.sun.tools.javac.main.JavacOption.XOption;
duke@1: import com.sun.tools.javac.util.ListBuffer;
duke@1: import com.sun.tools.javac.util.Options;
duke@1: import com.sun.tools.javac.processing.JavacProcessingEnvironment;
duke@1: import java.io.File;
duke@1: import java.io.FileWriter;
duke@1: import java.io.PrintWriter;
jjg@11: import java.util.Collection;
duke@1: import java.util.EnumSet;
jjg@11: import java.util.LinkedHashSet;
duke@1: import java.util.Set;
duke@1: import javax.lang.model.SourceVersion;
duke@1:
duke@1: import static com.sun.tools.javac.main.OptionName.*;
duke@1:
duke@1: /**
duke@1: * TODO: describe com.sun.tools.javac.main.RecognizedOptions
duke@1: *
duke@1: *
This is NOT part of any API supported by Sun Microsystems.
duke@1: * If you write code that depends on this, you do so at your own
duke@1: * risk. This code and its internal interfaces are subject to change
duke@1: * or deletion without notice.
duke@1: */
duke@1: public class RecognizedOptions {
duke@1:
duke@1: private RecognizedOptions() {}
duke@1:
duke@1: public interface OptionHelper {
duke@1:
duke@1: void setOut(PrintWriter out);
duke@1:
duke@1: void error(String key, Object... args);
duke@1:
duke@1: void printVersion();
duke@1:
duke@1: void printFullVersion();
duke@1:
duke@1: void printHelp();
duke@1:
duke@1: void printXhelp();
duke@1:
duke@1: void addFile(File f);
duke@1:
duke@1: void addClassName(String s);
duke@1:
duke@1: }
duke@1:
duke@1: public static class GrumpyHelper implements OptionHelper {
duke@1:
duke@1: public void setOut(PrintWriter out) {
duke@1: throw new IllegalArgumentException();
duke@1: }
duke@1:
duke@1: public void error(String key, Object... args) {
duke@1: throw new IllegalArgumentException(Main.getLocalizedString(key, args));
duke@1: }
duke@1:
duke@1: public void printVersion() {
duke@1: throw new IllegalArgumentException();
duke@1: }
duke@1:
duke@1: public void printFullVersion() {
duke@1: throw new IllegalArgumentException();
duke@1: }
duke@1:
duke@1: public void printHelp() {
duke@1: throw new IllegalArgumentException();
duke@1: }
duke@1:
duke@1: public void printXhelp() {
duke@1: throw new IllegalArgumentException();
duke@1: }
duke@1:
duke@1: public void addFile(File f) {
duke@1: throw new IllegalArgumentException(f.getPath());
duke@1: }
duke@1:
duke@1: public void addClassName(String s) {
duke@1: throw new IllegalArgumentException(s);
duke@1: }
duke@1:
duke@1: }
duke@1:
duke@1: static Set javacOptions = EnumSet.of(
duke@1: G,
duke@1: G_NONE,
duke@1: G_CUSTOM,
duke@1: XLINT,
duke@1: XLINT_CUSTOM,
duke@1: NOWARN,
duke@1: VERBOSE,
duke@1: DEPRECATION,
duke@1: CLASSPATH,
duke@1: CP,
duke@1: SOURCEPATH,
duke@1: BOOTCLASSPATH,
duke@1: XBOOTCLASSPATH_PREPEND,
duke@1: XBOOTCLASSPATH_APPEND,
duke@1: XBOOTCLASSPATH,
duke@1: EXTDIRS,
duke@1: DJAVA_EXT_DIRS,
duke@1: ENDORSEDDIRS,
duke@1: DJAVA_ENDORSED_DIRS,
jjg@11: PROC,
duke@1: PROCESSOR,
duke@1: PROCESSORPATH,
duke@1: D,
duke@1: S,
duke@1: IMPLICIT,
duke@1: ENCODING,
duke@1: SOURCE,
duke@1: TARGET,
duke@1: VERSION,
duke@1: FULLVERSION,
duke@1: HELP,
duke@1: A,
duke@1: X,
duke@1: J,
duke@1: MOREINFO,
duke@1: WERROR,
duke@1: // COMPLEXINFERENCE,
duke@1: PROMPT,
duke@1: DOE,
duke@1: PRINTSOURCE,
duke@1: WARNUNCHECKED,
duke@1: XMAXERRS,
duke@1: XMAXWARNS,
duke@1: XSTDOUT,
duke@1: XPRINT,
duke@1: XPRINTROUNDS,
duke@1: XPRINTPROCESSORINFO,
duke@1: XPREFER,
duke@1: O,
duke@1: XJCOV,
duke@1: XD,
duke@1: SOURCEFILE);
duke@1:
duke@1: static Set javacFileManagerOptions = EnumSet.of(
duke@1: CLASSPATH,
duke@1: CP,
duke@1: SOURCEPATH,
duke@1: BOOTCLASSPATH,
duke@1: XBOOTCLASSPATH_PREPEND,
duke@1: XBOOTCLASSPATH_APPEND,
duke@1: XBOOTCLASSPATH,
duke@1: EXTDIRS,
duke@1: DJAVA_EXT_DIRS,
duke@1: ENDORSEDDIRS,
duke@1: DJAVA_ENDORSED_DIRS,
duke@1: PROCESSORPATH,
duke@1: D,
duke@1: S,
duke@1: ENCODING,
duke@1: SOURCE);
duke@1:
duke@1: static Set javacToolOptions = EnumSet.of(
duke@1: G,
duke@1: G_NONE,
duke@1: G_CUSTOM,
duke@1: XLINT,
duke@1: XLINT_CUSTOM,
duke@1: NOWARN,
duke@1: VERBOSE,
duke@1: DEPRECATION,
jjg@11: PROC,
duke@1: PROCESSOR,
duke@1: IMPLICIT,
duke@1: SOURCE,
duke@1: TARGET,
duke@1: // VERSION,
duke@1: // FULLVERSION,
duke@1: // HELP,
duke@1: A,
duke@1: // X,
duke@1: // J,
duke@1: MOREINFO,
duke@1: WERROR,
duke@1: // COMPLEXINFERENCE,
duke@1: PROMPT,
duke@1: DOE,
duke@1: PRINTSOURCE,
duke@1: WARNUNCHECKED,
duke@1: XMAXERRS,
duke@1: XMAXWARNS,
duke@1: // XSTDOUT,
duke@1: XPRINT,
duke@1: XPRINTROUNDS,
duke@1: XPRINTPROCESSORINFO,
duke@1: XPREFER,
duke@1: O,
duke@1: XJCOV,
duke@1: XD);
duke@1:
duke@1: static Option[] getJavaCompilerOptions(OptionHelper helper) {
duke@1: return getOptions(helper, javacOptions);
duke@1: }
duke@1:
duke@1: public static Option[] getJavacFileManagerOptions(OptionHelper helper) {
duke@1: return getOptions(helper, javacFileManagerOptions);
duke@1: }
duke@1:
duke@1: public static Option[] getJavacToolOptions(OptionHelper helper) {
duke@1: return getOptions(helper, javacToolOptions);
duke@1: }
duke@1:
duke@1: static Option[] getOptions(OptionHelper helper, Set desired) {
duke@1: ListBuffer options = new ListBuffer ();
duke@1: for (Option option : getAll(helper))
duke@1: if (desired.contains(option.getName()))
duke@1: options.append(option);
duke@1: return options.toArray(new Option[options.length()]);
duke@1: }
duke@1:
duke@1: /**
jjg@11: * Get all the recognized options.
jjg@11: * @param helper an {@code OptionHelper} to help when processing options
jjg@11: * @return an array of options
duke@1: */
duke@1: public static Option[] getAll(final OptionHelper helper) {
jjg@11: return new Option[] {
duke@1: new Option(G, "opt.g"),
duke@1: new Option(G_NONE, "opt.g.none") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: options.put("-g:", "none");
duke@1: return false;
duke@1: }
duke@1: },
duke@1:
jjg@11: new Option(G_CUSTOM, "opt.g.lines.vars.source",
jjg@11: Option.ChoiceKind.ANYOF, "lines", "vars", "source"),
jjg@11:
jjg@11: new XOption(XLINT, "opt.Xlint"),
jjg@11: new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist",
jjg@11: Option.ChoiceKind.ANYOF, getXLintChoices()),
jjg@11:
jjg@11: // -nowarn is retained for command-line backward compatibility
jjg@11: new Option(NOWARN, "opt.nowarn") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
jjg@11: options.put("-Xlint:none", option);
duke@1: return false;
duke@1: }
duke@1: },
duke@1:
duke@1: new Option(VERBOSE, "opt.verbose"),
duke@1:
duke@1: // -deprecation is retained for command-line backward compatibility
duke@1: new Option(DEPRECATION, "opt.deprecation") {
jjg@11: @Override
jjg@11: public boolean process(Options options, String option) {
jjg@11: options.put("-Xlint:deprecation", option);
jjg@11: return false;
jjg@11: }
jjg@11: },
duke@1:
duke@1: new Option(CLASSPATH, "opt.arg.path", "opt.classpath"),
duke@1: new Option(CP, "opt.arg.path", "opt.classpath") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: return super.process(options, "-classpath", arg);
duke@1: }
duke@1: },
duke@1: new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath"),
duke@1: new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: options.remove("-Xbootclasspath/p:");
duke@1: options.remove("-Xbootclasspath/a:");
duke@1: return super.process(options, option, arg);
duke@1: }
duke@1: },
duke@1: new XOption(XBOOTCLASSPATH_PREPEND,"opt.arg.path", "opt.Xbootclasspath.p"),
duke@1: new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"),
duke@1: new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: options.remove("-Xbootclasspath/p:");
duke@1: options.remove("-Xbootclasspath/a:");
duke@1: return super.process(options, "-bootclasspath", arg);
duke@1: }
duke@1: },
duke@1: new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"),
duke@1: new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: return super.process(options, "-extdirs", arg);
duke@1: }
duke@1: },
duke@1: new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"),
duke@1: new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: return super.process(options, "-endorseddirs", arg);
duke@1: }
duke@1: },
jjg@11: new Option(PROC, "opt.proc.none.only",
jjg@11: Option.ChoiceKind.ONEOF, "none", "only"),
duke@1: new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"),
duke@1: new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"),
duke@1: new Option(D, "opt.arg.directory", "opt.d"),
duke@1: new Option(S, "opt.arg.directory", "opt.sourceDest"),
jjg@11: new Option(IMPLICIT, "opt.implicit",
jjg@11: Option.ChoiceKind.ONEOF, "none", "class"),
duke@1: new Option(ENCODING, "opt.arg.encoding", "opt.encoding"),
duke@1: new Option(SOURCE, "opt.arg.release", "opt.source") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String operand) {
duke@1: Source source = Source.lookup(operand);
duke@1: if (source == null) {
duke@1: helper.error("err.invalid.source", operand);
duke@1: return true;
duke@1: }
duke@1: return super.process(options, option, operand);
duke@1: }
duke@1: },
duke@1: new Option(TARGET, "opt.arg.release", "opt.target") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String operand) {
duke@1: Target target = Target.lookup(operand);
duke@1: if (target == null) {
duke@1: helper.error("err.invalid.target", operand);
duke@1: return true;
duke@1: }
duke@1: return super.process(options, option, operand);
duke@1: }
duke@1: },
duke@1: new Option(VERSION, "opt.version") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: helper.printVersion();
duke@1: return super.process(options, option);
duke@1: }
duke@1: },
duke@1: new HiddenOption(FULLVERSION) {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: helper.printFullVersion();
duke@1: return super.process(options, option);
duke@1: }
duke@1: },
duke@1: new Option(HELP, "opt.help") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: helper.printHelp();
duke@1: return super.process(options, option);
duke@1: }
duke@1: },
duke@1: new Option(A, "opt.arg.key.equals.value","opt.A") {
jjg@11: @Override
jjg@11: String helpSynopsis() {
jjg@11: hasSuffix = true;
jjg@11: return super.helpSynopsis();
jjg@11: }
jjg@11:
jjg@11: @Override
jjg@11: public boolean matches(String arg) {
jjg@11: return arg.startsWith("-A");
jjg@11: }
jjg@11:
jjg@11: @Override
jjg@11: public boolean hasArg() {
jjg@11: return false;
jjg@11: }
jjg@11: // Mapping for processor options created in
jjg@11: // JavacProcessingEnvironment
jjg@11: @Override
jjg@11: public boolean process(Options options, String option) {
jjg@11: int argLength = option.length();
jjg@11: if (argLength == 2) {
jjg@11: helper.error("err.empty.A.argument");
jjg@11: return true;
duke@1: }
jjg@11: int sepIndex = option.indexOf('=');
jjg@11: String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
jjg@11: if (!JavacProcessingEnvironment.isValidOptionName(key)) {
jjg@11: helper.error("err.invalid.A.key", option);
jjg@11: return true;
duke@1: }
jjg@11: return process(options, option, option);
jjg@11: }
duke@1: },
duke@1: new Option(X, "opt.X") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: helper.printXhelp();
duke@1: return super.process(options, option);
duke@1: }
duke@1: },
duke@1:
duke@1: // This option exists only for the purpose of documenting itself.
duke@1: // It's actually implemented by the launcher.
duke@1: new Option(J, "opt.arg.flag", "opt.J") {
jjg@11: @Override
duke@1: String helpSynopsis() {
duke@1: hasSuffix = true;
duke@1: return super.helpSynopsis();
duke@1: }
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: throw new AssertionError
duke@1: ("the -J flag should be caught by the launcher.");
duke@1: }
duke@1: },
duke@1:
duke@1: // stop after parsing and attributing.
duke@1: // new HiddenOption("-attrparseonly"),
duke@1:
duke@1: // new Option("-moreinfo", "opt.moreinfo") {
duke@1: new HiddenOption(MOREINFO) {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: Type.moreInfo = true;
duke@1: return super.process(options, option);
duke@1: }
duke@1: },
duke@1:
duke@1: // treat warnings as errors
duke@1: new HiddenOption(WERROR),
duke@1:
duke@1: // use complex inference from context in the position of a method call argument
duke@1: new HiddenOption(COMPLEXINFERENCE),
duke@1:
duke@1: // generare source stubs
duke@1: // new HiddenOption("-stubs"),
duke@1:
duke@1: // relax some constraints to allow compiling from stubs
duke@1: // new HiddenOption("-relax"),
duke@1:
duke@1: // output source after translating away inner classes
duke@1: // new Option("-printflat", "opt.printflat"),
duke@1: // new HiddenOption("-printflat"),
duke@1:
duke@1: // display scope search details
duke@1: // new Option("-printsearch", "opt.printsearch"),
duke@1: // new HiddenOption("-printsearch"),
duke@1:
duke@1: // prompt after each error
duke@1: // new Option("-prompt", "opt.prompt"),
duke@1: new HiddenOption(PROMPT),
duke@1:
duke@1: // dump stack on error
duke@1: new HiddenOption(DOE),
duke@1:
duke@1: // output source after type erasure
duke@1: // new Option("-s", "opt.s"),
duke@1: new HiddenOption(PRINTSOURCE),
duke@1:
duke@1: // output shrouded class files
duke@1: // new Option("-scramble", "opt.scramble"),
duke@1: // new Option("-scrambleall", "opt.scrambleall"),
duke@1:
duke@1: // display warnings for generic unchecked operations
duke@1: new HiddenOption(WARNUNCHECKED) {
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: options.put("-Xlint:unchecked", option);
duke@1: return false;
duke@1: }
duke@1: },
duke@1:
duke@1: new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"),
duke@1: new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"),
duke@1: new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
jjg@11: @Override
duke@1: public boolean process(Options options, String option, String arg) {
duke@1: try {
duke@1: helper.setOut(new PrintWriter(new FileWriter(arg), true));
duke@1: } catch (java.io.IOException e) {
duke@1: helper.error("err.error.writing.file", arg, e);
duke@1: return true;
duke@1: }
duke@1: return super.process(options, option, arg);
duke@1: }
duke@1: },
duke@1:
duke@1: new XOption(XPRINT, "opt.print"),
duke@1:
duke@1: new XOption(XPRINTROUNDS, "opt.printRounds"),
duke@1:
duke@1: new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"),
duke@1:
jjg@11: new XOption(XPREFER, "opt.prefer",
jjg@11: Option.ChoiceKind.ONEOF, "source", "newer"),
duke@1:
duke@1: /* -O is a no-op, accepted for backward compatibility. */
duke@1: new HiddenOption(O),
duke@1:
duke@1: /* -Xjcov produces tables to support the code coverage tool jcov. */
duke@1: new HiddenOption(XJCOV),
duke@1:
duke@1: /* This is a back door to the compiler's option table.
duke@1: * -XDx=y sets the option x to the value y.
duke@1: * -XDx sets the option x to the value x.
duke@1: */
duke@1: new HiddenOption(XD) {
duke@1: String s;
jjg@11: @Override
duke@1: public boolean matches(String s) {
duke@1: this.s = s;
duke@1: return s.startsWith(name.optionName);
duke@1: }
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: s = s.substring(name.optionName.length());
duke@1: int eq = s.indexOf('=');
duke@1: String key = (eq < 0) ? s : s.substring(0, eq);
duke@1: String value = (eq < 0) ? s : s.substring(eq+1);
duke@1: options.put(key, value);
duke@1: return false;
duke@1: }
duke@1: },
duke@1:
duke@1: /*
duke@1: * TODO: With apt, the matches method accepts anything if
duke@1: * -XclassAsDecls is used; code elsewhere does the lookup to
duke@1: * see if the class name is both legal and found.
duke@1: *
duke@1: * In apt, the process method adds the candiate class file
duke@1: * name to a separate list.
duke@1: */
duke@1: new HiddenOption(SOURCEFILE) {
duke@1: String s;
jjg@11: @Override
duke@1: public boolean matches(String s) {
duke@1: this.s = s;
duke@1: return s.endsWith(".java") // Java source file
duke@1: || SourceVersion.isName(s); // Legal type name
duke@1: }
jjg@11: @Override
duke@1: public boolean process(Options options, String option) {
duke@1: if (s.endsWith(".java") ) {
duke@1: File f = new File(s);
duke@1: if (!f.exists()) {
duke@1: helper.error("err.file.not.found", f);
duke@1: return true;
duke@1: }
duke@1: if (!f.isFile()) {
duke@1: helper.error("err.file.not.file", f);
duke@1: return true;
duke@1: }
duke@1: helper.addFile(f);
duke@1: }
duke@1: else
duke@1: helper.addClassName(s);
duke@1: return false;
duke@1: }
duke@1: },
duke@1: };
duke@1: }
duke@1:
jjg@11: private static Collection getXLintChoices() {
jjg@11: Collection choices = new LinkedHashSet();
jjg@11: choices.add("all");
jjg@11: for (Lint.LintCategory c : Lint.LintCategory.values())
jjg@11: choices.add(c.option);
jjg@11: for (Lint.LintCategory c : Lint.LintCategory.values())
jjg@11: choices.add("-" + c.option);
jjg@11: choices.add("none");
jjg@11: return choices;
jjg@11: }
jjg@11:
duke@1: }