jjg@1158: /* jjg@1230: * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved. jjg@1158: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1158: * jjg@1158: * This code is free software; you can redistribute it and/or modify it jjg@1158: * under the terms of the GNU General Public License version 2 only, as jjg@1158: * published by the Free Software Foundation. Oracle designates this jjg@1158: * particular file as subject to the "Classpath" exception as provided jjg@1158: * by Oracle in the LICENSE file that accompanied this code. jjg@1158: * jjg@1158: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1158: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1158: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1158: * version 2 for more details (a copy is included in the LICENSE file that jjg@1158: * accompanied this code). jjg@1158: * jjg@1158: * You should have received a copy of the GNU General Public License version jjg@1158: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1158: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1158: * jjg@1158: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1158: * or visit www.oracle.com if you need additional information or have any jjg@1158: * questions. jjg@1158: */ jjg@1158: jjg@1158: package com.sun.tools.javac.main; jjg@1158: jjg@1158: import java.util.Collections; jjg@1158: import com.sun.tools.javac.util.Log.PrefixKind; jjg@1158: import com.sun.tools.javac.util.Log.WriterKind; jjg@1158: import com.sun.tools.javac.util.Log; jjg@1158: import com.sun.tools.javac.code.Lint; jjg@1158: import com.sun.tools.javac.code.Source; jjg@1158: import com.sun.tools.javac.code.Type; jjg@1158: import com.sun.tools.javac.jvm.Target; jjg@1158: import com.sun.tools.javac.util.Options; jjg@1158: import com.sun.tools.javac.processing.JavacProcessingEnvironment; jjg@1158: import java.io.File; jjg@1158: import java.io.FileWriter; jjg@1158: import java.io.PrintWriter; jjg@1158: import java.util.EnumSet; jjg@1158: import java.util.LinkedHashMap; jjg@1158: import java.util.Map; jjg@1158: import java.util.Set; jjg@1158: import javax.lang.model.SourceVersion; jjg@1158: jjg@1158: import static com.sun.tools.javac.main.Option.ChoiceKind.*; jjg@1158: import static com.sun.tools.javac.main.Option.OptionKind.*; jjg@1158: import static com.sun.tools.javac.main.Option.OptionGroup.*; jjg@1158: jjg@1158: /** jjg@1158: * Options for javac. The specific Option to handle a command-line option jjg@1158: * is identified by searching the members of this enum in order, looking jjg@1158: * the first {@link #matches match}. The action for an Option is performed jjg@1158: * by calling {@link #process process}, and by providing a suitable jjg@1158: * {@link OptionHelper} to provide access the compiler state. jjg@1158: * jjg@1158: *

This is NOT part of any supported API. jjg@1158: * If you write code that depends on this, you do so at your own jjg@1158: * risk. This code and its internal interfaces are subject to change jjg@1158: * or deletion without notice.

jjg@1158: */ jjg@1158: public enum Option { jjg@1158: G("-g", "opt.g", STANDARD, BASIC), jjg@1158: jjg@1158: G_NONE("-g:none", "opt.g.none", STANDARD, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: helper.put("-g:", "none"); jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: G_CUSTOM("-g:", "opt.g.lines.vars.source", jjg@1158: STANDARD, BASIC, ANYOF, "lines", "vars", "source"), jjg@1158: jjg@1158: XLINT("-Xlint", "opt.Xlint", EXTENDED, BASIC), jjg@1158: jjg@1158: XLINT_CUSTOM("-Xlint:", "opt.Xlint.suboptlist", jjg@1158: EXTENDED, BASIC, ANYOF, getXLintChoices()), jjg@1158: jjg@1158: // -nowarn is retained for command-line backward compatibility jjg@1158: NOWARN("-nowarn", "opt.nowarn", STANDARD, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: helper.put("-Xlint:none", option); jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: VERBOSE("-verbose", "opt.verbose", STANDARD, BASIC), jjg@1158: jjg@1158: // -deprecation is retained for command-line backward compatibility jjg@1158: DEPRECATION("-deprecation", "opt.deprecation", STANDARD, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: helper.put("-Xlint:deprecation", option); jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: CLASSPATH("-classpath", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: CP("-cp", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: return super.process(helper, "-classpath", arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: SOURCEPATH("-sourcepath", "opt.arg.path", "opt.sourcepath", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: BOOTCLASSPATH("-bootclasspath", "opt.arg.path", "opt.bootclasspath", STANDARD, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: helper.remove("-Xbootclasspath/p:"); jjg@1158: helper.remove("-Xbootclasspath/a:"); jjg@1158: return super.process(helper, option, arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: XBOOTCLASSPATH_PREPEND("-Xbootclasspath/p:", "opt.arg.path", "opt.Xbootclasspath.p", EXTENDED, FILEMANAGER), jjg@1158: jjg@1158: XBOOTCLASSPATH_APPEND("-Xbootclasspath/a:", "opt.arg.path", "opt.Xbootclasspath.a", EXTENDED, FILEMANAGER), jjg@1158: jjg@1158: XBOOTCLASSPATH("-Xbootclasspath:", "opt.arg.path", "opt.bootclasspath", EXTENDED, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: helper.remove("-Xbootclasspath/p:"); jjg@1158: helper.remove("-Xbootclasspath/a:"); jjg@1158: return super.process(helper, "-bootclasspath", arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: EXTDIRS("-extdirs", "opt.arg.dirs", "opt.extdirs", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: DJAVA_EXT_DIRS("-Djava.ext.dirs=", "opt.arg.dirs", "opt.extdirs", EXTENDED, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: return super.process(helper, "-extdirs", arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: ENDORSEDDIRS("-endorseddirs", "opt.arg.dirs", "opt.endorseddirs", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: DJAVA_ENDORSED_DIRS("-Djava.endorsed.dirs=", "opt.arg.dirs", "opt.endorseddirs", EXTENDED, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: return super.process(helper, "-endorseddirs", arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: PROC("-proc:", "opt.proc.none.only", STANDARD, BASIC, ONEOF, "none", "only"), jjg@1158: jjg@1158: PROCESSOR("-processor", "opt.arg.class.list", "opt.processor", STANDARD, BASIC), jjg@1158: jjg@1158: PROCESSORPATH("-processorpath", "opt.arg.path", "opt.processorpath", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: D("-d", "opt.arg.directory", "opt.d", STANDARD, FILEMANAGER), jjg@1158: jjg@1158: S("-s", "opt.arg.directory", "opt.sourceDest", STANDARD, FILEMANAGER), jjg@1158: jjg@1230: H("-h", "opt.arg.directory", "opt.headerDest", STANDARD, FILEMANAGER), jjg@1230: jjg@1158: IMPLICIT("-implicit:", "opt.implicit", STANDARD, BASIC, ONEOF, "none", "class"), jjg@1158: jjg@1158: ENCODING("-encoding", "opt.arg.encoding", "opt.encoding", STANDARD, FILEMANAGER) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String operand) { jjg@1158: return super.process(helper, option, operand); jjg@1158: } jjg@1158: jjg@1158: }, jjg@1158: jjg@1158: SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String operand) { jjg@1158: Source source = Source.lookup(operand); jjg@1158: if (source == null) { jjg@1158: helper.error("err.invalid.source", operand); jjg@1158: return true; jjg@1158: } jjg@1158: return super.process(helper, option, operand); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String operand) { jjg@1158: Target target = Target.lookup(operand); jjg@1158: if (target == null) { jjg@1158: helper.error("err.invalid.target", operand); jjg@1158: return true; jjg@1158: } jjg@1158: return super.process(helper, option, operand); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: VERSION("-version", "opt.version", STANDARD, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: Log log = helper.getLog(); jjg@1158: String ownName = helper.getOwnName(); jjg@1158: log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version()); jjg@1158: return super.process(helper, option); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: FULLVERSION("-fullversion", null, HIDDEN, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: Log log = helper.getLog(); jjg@1158: String ownName = helper.getOwnName(); jjg@1160: log.printLines(PrefixKind.JAVAC, "fullVersion", ownName, JavaCompiler.fullVersion()); jjg@1158: return super.process(helper, option); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: DIAGS("-XDdiags=", null, HIDDEN, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: option = option.substring(option.indexOf('=') + 1); jjg@1158: String diagsOption = option.contains("%") ? jjg@1158: "-XDdiagsFormat=" : jjg@1158: "-XDdiags="; jjg@1158: diagsOption += option; jjg@1158: if (XD.matches(diagsOption)) jjg@1158: return XD.process(helper, diagsOption); jjg@1158: else jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: HELP("-help", "opt.help", STANDARD, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: Log log = helper.getLog(); jjg@1158: String ownName = helper.getOwnName(); jjg@1158: log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName); jjg@1158: for (Option o: getJavaCompilerOptions()) { jjg@1158: o.help(log, OptionKind.STANDARD); jjg@1158: } jjg@1158: log.printNewline(); jjg@1158: return super.process(helper, option); jjg@1158: } jjg@1158: }, jjg@1158: vromero@1442: A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC, true) { jjg@1158: @Override jjg@1158: public boolean matches(String arg) { jjg@1158: return arg.startsWith("-A"); jjg@1158: } jjg@1158: jjg@1158: @Override jjg@1158: public boolean hasArg() { jjg@1158: return false; jjg@1158: } jjg@1158: // Mapping for processor options created in jjg@1158: // JavacProcessingEnvironment jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: int argLength = option.length(); jjg@1158: if (argLength == 2) { jjg@1158: helper.error("err.empty.A.argument"); jjg@1158: return true; jjg@1158: } jjg@1158: int sepIndex = option.indexOf('='); jjg@1158: String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) ); jjg@1158: if (!JavacProcessingEnvironment.isValidOptionName(key)) { jjg@1158: helper.error("err.invalid.A.key", option); jjg@1158: return true; jjg@1158: } jjg@1158: return process(helper, option, option); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: X("-X", "opt.X", STANDARD, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: Log log = helper.getLog(); jjg@1158: for (Option o: getJavaCompilerOptions()) { jjg@1158: o.help(log, OptionKind.EXTENDED); jjg@1158: } jjg@1158: log.printNewline(); jjg@1158: log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer"); jjg@1158: return super.process(helper, option); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: // This option exists only for the purpose of documenting itself. jjg@1158: // It's actually implemented by the launcher. jjg@1158: J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: throw new AssertionError jjg@1158: ("the -J flag should be caught by the launcher."); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: MOREINFO("-moreinfo", null, HIDDEN, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: Type.moreInfo = true; jjg@1158: return super.process(helper, option); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: // treat warnings as errors jjg@1158: WERROR("-Werror", "opt.Werror", STANDARD, BASIC), jjg@1158: jjg@1158: // prompt after each error jjg@1158: // new Option("-prompt", "opt.prompt"), jjg@1158: PROMPT("-prompt", null, HIDDEN, BASIC), jjg@1158: jjg@1158: // dump stack on error jjg@1158: DOE("-doe", null, HIDDEN, BASIC), jjg@1158: jjg@1158: // output source after type erasure jjg@1158: PRINTSOURCE("-printsource", null, HIDDEN, BASIC), jjg@1158: jjg@1158: // display warnings for generic unchecked operations jjg@1158: WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: helper.put("-Xlint:unchecked", option); jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC), jjg@1158: jjg@1158: XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC), jjg@1158: jjg@1159: XSTDOUT("-Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: try { jjg@1158: Log log = helper.getLog(); jjg@1158: // TODO: this file should be closed at the end of compilation jjg@1158: log.setWriters(new PrintWriter(new FileWriter(arg), true)); jjg@1158: } catch (java.io.IOException e) { jjg@1158: helper.error("err.error.writing.file", arg, e); jjg@1158: return true; jjg@1158: } jjg@1158: return super.process(helper, option, arg); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: XPRINT("-Xprint", "opt.print", EXTENDED, BASIC), jjg@1158: jjg@1158: XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC), jjg@1158: jjg@1158: XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC), jjg@1158: jjg@1158: XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"), jjg@1158: jjg@1158: XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"), jjg@1158: jjg@1158: /* -O is a no-op, accepted for backward compatibility. */ jjg@1158: O("-O", null, HIDDEN, BASIC), jjg@1158: jjg@1158: /* -Xjcov produces tables to support the code coverage tool jcov. */ jjg@1158: XJCOV("-Xjcov", null, HIDDEN, BASIC), jjg@1158: jjg@1416: PLUGIN("-Xplugin:", "opt.arg.plugin", "opt.plugin", EXTENDED, BASIC) { jjg@1416: @Override jjg@1416: public boolean process(OptionHelper helper, String option) { jjg@1416: String p = option.substring(option.indexOf(':') + 1); jjg@1416: String prev = helper.get(PLUGIN); jjg@1416: helper.put(PLUGIN.text, (prev == null) ? p : prev + '\0' + p.trim()); jjg@1416: return false; jjg@1416: } jjg@1416: }, jjg@1416: jjg@1158: /* This is a back door to the compiler's option table. jjg@1158: * -XDx=y sets the option x to the value y. jjg@1158: * -XDx sets the option x to the value x. jjg@1158: */ jjg@1158: XD("-XD", null, HIDDEN, BASIC) { jjg@1158: @Override jjg@1158: public boolean matches(String s) { jjg@1158: return s.startsWith(text); jjg@1158: } jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { vromero@1442: option = option.substring(text.length()); vromero@1442: int eq = option.indexOf('='); vromero@1442: String key = (eq < 0) ? option : option.substring(0, eq); vromero@1442: String value = (eq < 0) ? option : option.substring(eq+1); jjg@1158: helper.put(key, value); jjg@1158: return false; jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: // This option exists only for the purpose of documenting itself. jjg@1158: // It's actually implemented by the CommandLine class. jjg@1158: AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO) { jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: throw new AssertionError("the @ flag should be caught by CommandLine."); jjg@1158: } jjg@1158: }, jjg@1158: jjg@1158: /* jjg@1158: * TODO: With apt, the matches method accepts anything if jjg@1158: * -XclassAsDecls is used; code elsewhere does the lookup to jjg@1158: * see if the class name is both legal and found. jjg@1158: * jjg@1158: * In apt, the process method adds the candidate class file jjg@1158: * name to a separate list. jjg@1158: */ jjg@1158: SOURCEFILE("sourcefile", null, HIDDEN, INFO) { jjg@1158: @Override jjg@1158: public boolean matches(String s) { jjg@1158: return s.endsWith(".java") // Java source file jjg@1158: || SourceVersion.isName(s); // Legal type name jjg@1158: } jjg@1158: @Override jjg@1158: public boolean process(OptionHelper helper, String option) { vromero@1442: if (option.endsWith(".java") ) { vromero@1442: File f = new File(option); jjg@1158: if (!f.exists()) { jjg@1158: helper.error("err.file.not.found", f); jjg@1158: return true; jjg@1158: } jjg@1158: if (!f.isFile()) { jjg@1158: helper.error("err.file.not.file", f); jjg@1158: return true; jjg@1158: } jjg@1158: helper.addFile(f); vromero@1442: } else { vromero@1442: helper.addClassName(option); jjg@1158: } jjg@1158: return false; jjg@1158: } jjg@1158: }; jjg@1158: jjg@1158: /** The kind of an Option. This is used by the -help and -X options. */ jjg@1158: public enum OptionKind { jjg@1158: /** A standard option, documented by -help. */ jjg@1158: STANDARD, jjg@1158: /** An extended option, documented by -X. */ jjg@1158: EXTENDED, jjg@1158: /** A hidden option, not documented. */ jjg@1158: HIDDEN, jjg@1158: } jjg@1158: jjg@1158: /** The group for an Option. This determines the situations in which the jjg@1158: * option is applicable. */ jjg@1158: enum OptionGroup { jjg@1158: /** A basic option, available for use on the command line or via the jjg@1158: * Compiler API. */ jjg@1158: BASIC, jjg@1158: /** An option for javac's standard JavaFileManager. Other file managers jjg@1158: * may or may not support these options. */ jjg@1158: FILEMANAGER, jjg@1158: /** A command-line option that requests information, such as -help. */ jjg@1158: INFO, jjg@1158: /** A command-line "option" representing a file or class name. */ jjg@1158: OPERAND jjg@1158: } jjg@1158: jjg@1158: /** The kind of choice for "choice" options. */ jjg@1158: enum ChoiceKind { jjg@1158: /** The expected value is exactly one of the set of choices. */ jjg@1158: ONEOF, jjg@1158: /** The expected value is one of more of the set of choices. */ jjg@1158: ANYOF jjg@1158: } jjg@1158: jjg@1158: public final String text; jjg@1158: jjg@1158: final OptionKind kind; jjg@1158: jjg@1158: final OptionGroup group; jjg@1158: jjg@1158: /** Documentation key for arguments. jjg@1158: */ jjg@1158: final String argsNameKey; jjg@1158: jjg@1158: /** Documentation key for description. jjg@1158: */ jjg@1158: final String descrKey; jjg@1158: jjg@1158: /** Suffix option (-foo=bar or -foo:bar) jjg@1158: */ vromero@1442: final boolean hasSuffix; jjg@1158: jjg@1158: /** The kind of choices for this option, if any. jjg@1158: */ jjg@1158: final ChoiceKind choiceKind; jjg@1158: jjg@1158: /** The choices for this option, if any, and whether or not the choices jjg@1158: * are hidden jjg@1158: */ jjg@1158: final Map choices; jjg@1158: jjg@1158: jjg@1158: Option(String text, String descrKey, jjg@1158: OptionKind kind, OptionGroup group) { vromero@1442: this(text, null, descrKey, kind, group, null, null, false); jjg@1158: } jjg@1158: jjg@1158: Option(String text, String argsNameKey, String descrKey, jjg@1158: OptionKind kind, OptionGroup group) { vromero@1442: this(text, argsNameKey, descrKey, kind, group, null, null, false); vromero@1442: } vromero@1442: vromero@1442: Option(String text, String argsNameKey, String descrKey, vromero@1442: OptionKind kind, OptionGroup group, boolean doHasSuffix) { vromero@1442: this(text, argsNameKey, descrKey, kind, group, null, null, doHasSuffix); jjg@1158: } jjg@1158: jjg@1158: Option(String text, String descrKey, jjg@1158: OptionKind kind, OptionGroup group, jjg@1158: ChoiceKind choiceKind, Map choices) { vromero@1442: this(text, null, descrKey, kind, group, choiceKind, choices, false); jjg@1158: } jjg@1158: jjg@1158: Option(String text, String descrKey, jjg@1158: OptionKind kind, OptionGroup group, jjg@1158: ChoiceKind choiceKind, String... choices) { vromero@1442: this(text, null, descrKey, kind, group, choiceKind, vromero@1442: createChoices(choices), false); jjg@1158: } jjg@1158: // where jjg@1158: private static Map createChoices(String... choices) { jjg@1158: Map map = new LinkedHashMap(); jjg@1158: for (String c: choices) jjg@1158: map.put(c, false); jjg@1158: return map; jjg@1158: } jjg@1158: jjg@1158: private Option(String text, String argsNameKey, String descrKey, jjg@1158: OptionKind kind, OptionGroup group, vromero@1442: ChoiceKind choiceKind, Map choices, vromero@1442: boolean doHasSuffix) { jjg@1158: this.text = text; jjg@1158: this.argsNameKey = argsNameKey; jjg@1158: this.descrKey = descrKey; jjg@1158: this.kind = kind; jjg@1158: this.group = group; jjg@1158: this.choiceKind = choiceKind; jjg@1158: this.choices = choices; jjg@1158: char lastChar = text.charAt(text.length()-1); vromero@1442: this.hasSuffix = doHasSuffix || lastChar == ':' || lastChar == '='; jjg@1158: } jjg@1158: jjg@1158: public String getText() { jjg@1158: return text; jjg@1158: } jjg@1158: jjg@1158: public OptionKind getKind() { jjg@1158: return kind; jjg@1158: } jjg@1158: jjg@1158: public boolean hasArg() { jjg@1158: return argsNameKey != null && !hasSuffix; jjg@1158: } jjg@1158: jjg@1158: public boolean matches(String option) { jjg@1158: if (!hasSuffix) jjg@1158: return option.equals(text); jjg@1158: jjg@1158: if (!option.startsWith(text)) jjg@1158: return false; jjg@1158: jjg@1158: if (choices != null) { jjg@1158: String arg = option.substring(text.length()); jjg@1158: if (choiceKind == ChoiceKind.ONEOF) jjg@1158: return choices.keySet().contains(arg); jjg@1158: else { jjg@1158: for (String a: arg.split(",+")) { jjg@1158: if (!choices.keySet().contains(a)) jjg@1158: return false; jjg@1158: } jjg@1158: } jjg@1158: } jjg@1158: jjg@1158: return true; jjg@1158: } jjg@1158: jjg@1158: public boolean process(OptionHelper helper, String option, String arg) { jjg@1158: if (choices != null) { jjg@1158: if (choiceKind == ChoiceKind.ONEOF) { jjg@1158: // some clients like to see just one of option+choice set jjg@1158: for (String s: choices.keySet()) jjg@1158: helper.remove(option + s); jjg@1158: String opt = option + arg; jjg@1158: helper.put(opt, opt); jjg@1158: // some clients like to see option (without trailing ":") jjg@1158: // set to arg jjg@1158: String nm = option.substring(0, option.length() - 1); jjg@1158: helper.put(nm, arg); jjg@1158: } else { jjg@1158: // set option+word for each word in arg jjg@1158: for (String a: arg.split(",+")) { jjg@1158: String opt = option + a; jjg@1158: helper.put(opt, opt); jjg@1158: } jjg@1158: } jjg@1158: } jjg@1158: helper.put(option, arg); jjg@1158: return false; jjg@1158: } jjg@1158: jjg@1158: public boolean process(OptionHelper helper, String option) { jjg@1158: if (hasSuffix) jjg@1158: return process(helper, text, option.substring(text.length())); jjg@1158: else jjg@1158: return process(helper, option, option); jjg@1158: } jjg@1158: jjg@1158: void help(Log log, OptionKind kind) { jjg@1158: if (this.kind != kind) jjg@1158: return; jjg@1158: jjg@1158: log.printRawLines(WriterKind.NOTICE, jjg@1158: String.format(" %-26s %s", jjg@1158: helpSynopsis(log), jjg@1158: log.localize(PrefixKind.JAVAC, descrKey))); jjg@1158: jjg@1158: } jjg@1158: jjg@1158: private String helpSynopsis(Log log) { jjg@1158: StringBuilder sb = new StringBuilder(); jjg@1158: sb.append(text); jjg@1158: if (argsNameKey == null) { jjg@1158: if (choices != null) { jjg@1158: String sep = "{"; jjg@1158: for (Map.Entry e: choices.entrySet()) { jjg@1158: if (!e.getValue()) { jjg@1158: sb.append(sep); jjg@1158: sb.append(e.getKey()); jjg@1158: sep = ","; jjg@1158: } jjg@1158: } jjg@1158: sb.append("}"); jjg@1158: } jjg@1158: } else { jjg@1158: if (!hasSuffix) jjg@1158: sb.append(" "); jjg@1158: sb.append(log.localize(PrefixKind.JAVAC, argsNameKey)); jjg@1158: jjg@1158: } jjg@1158: jjg@1158: return sb.toString(); jjg@1158: } jjg@1158: jjg@1158: // For -XpkgInfo:value jjg@1158: public enum PkgInfo { jjg@1158: ALWAYS, LEGACY, NONEMPTY; jjg@1158: public static PkgInfo get(Options options) { jjg@1158: String v = options.get(XPKGINFO); jjg@1158: return (v == null jjg@1158: ? PkgInfo.LEGACY jjg@1158: : PkgInfo.valueOf(v.toUpperCase())); jjg@1158: } jjg@1158: } jjg@1158: jjg@1158: private static Map getXLintChoices() { jjg@1158: Map choices = new LinkedHashMap(); jjg@1158: choices.put("all", false); jjg@1158: for (Lint.LintCategory c : Lint.LintCategory.values()) jjg@1158: choices.put(c.option, c.hidden); jjg@1158: for (Lint.LintCategory c : Lint.LintCategory.values()) jjg@1158: choices.put("-" + c.option, c.hidden); jjg@1158: choices.put("none", false); jjg@1158: return choices; jjg@1158: } jjg@1158: jjg@1158: static Set