src/share/classes/com/sun/tools/javac/main/RecognizedOptions.java

changeset 11
b66d15dfd001
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
     1.1 --- a/src/share/classes/com/sun/tools/javac/main/RecognizedOptions.java	Thu Mar 06 10:25:04 2008 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/main/RecognizedOptions.java	Tue Mar 11 13:14:55 2008 -0700
     1.3 @@ -25,21 +25,23 @@
     1.4  
     1.5  package com.sun.tools.javac.main;
     1.6  
     1.7 +import com.sun.tools.javac.code.Lint;
     1.8  import com.sun.tools.javac.code.Source;
     1.9  import com.sun.tools.javac.code.Type;
    1.10  import com.sun.tools.javac.jvm.Target;
    1.11  import com.sun.tools.javac.main.JavacOption.HiddenOption;
    1.12  import com.sun.tools.javac.main.JavacOption.Option;
    1.13  import com.sun.tools.javac.main.JavacOption.XOption;
    1.14 -import com.sun.tools.javac.util.List;
    1.15  import com.sun.tools.javac.util.ListBuffer;
    1.16 -import com.sun.tools.javac.util.Log;
    1.17  import com.sun.tools.javac.util.Options;
    1.18  import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    1.19  import java.io.File;
    1.20  import java.io.FileWriter;
    1.21  import java.io.PrintWriter;
    1.22 +import java.util.Arrays;
    1.23 +import java.util.Collection;
    1.24  import java.util.EnumSet;
    1.25 +import java.util.LinkedHashSet;
    1.26  import java.util.Set;
    1.27  import java.util.StringTokenizer;
    1.28  import javax.lang.model.SourceVersion;
    1.29 @@ -134,7 +136,7 @@
    1.30          DJAVA_EXT_DIRS,
    1.31          ENDORSEDDIRS,
    1.32          DJAVA_ENDORSED_DIRS,
    1.33 -        PROC_CUSTOM,
    1.34 +        PROC,
    1.35          PROCESSOR,
    1.36          PROCESSORPATH,
    1.37          D,
    1.38 @@ -195,7 +197,7 @@
    1.39          NOWARN,
    1.40          VERBOSE,
    1.41          DEPRECATION,
    1.42 -        PROC_CUSTOM,
    1.43 +        PROC,
    1.44          PROCESSOR,
    1.45          IMPLICIT,
    1.46          SOURCE,
    1.47 @@ -245,79 +247,58 @@
    1.48      }
    1.49  
    1.50      /**
    1.51 -     * @param out the writer to use for diagnostic output
    1.52 +     * Get all the recognized options.
    1.53 +     * @param helper an {@code OptionHelper} to help when processing options
    1.54 +     * @return an array of options
    1.55       */
    1.56      public static Option[] getAll(final OptionHelper helper) {
    1.57 -        return new Option[]{
    1.58 +        return new Option[] {
    1.59          new Option(G,                                           "opt.g"),
    1.60          new Option(G_NONE,                                      "opt.g.none") {
    1.61 +            @Override
    1.62              public boolean process(Options options, String option) {
    1.63                  options.put("-g:", "none");
    1.64                  return false;
    1.65              }
    1.66          },
    1.67  
    1.68 -        new Option(G_CUSTOM,                                    "opt.g.lines.vars.source") {
    1.69 -            public boolean matches(String s) {
    1.70 -                return s.startsWith("-g:");
    1.71 -            }
    1.72 +        new Option(G_CUSTOM,                                    "opt.g.lines.vars.source",
    1.73 +                Option.ChoiceKind.ANYOF, "lines", "vars", "source"),
    1.74 +
    1.75 +        new XOption(XLINT,                                      "opt.Xlint"),
    1.76 +        new XOption(XLINT_CUSTOM,                               "opt.Xlint.suboptlist",
    1.77 +                Option.ChoiceKind.ANYOF, getXLintChoices()),
    1.78 +
    1.79 +        // -nowarn is retained for command-line backward compatibility
    1.80 +        new Option(NOWARN,                                      "opt.nowarn") {
    1.81 +            @Override
    1.82              public boolean process(Options options, String option) {
    1.83 -                String suboptions = option.substring(3);
    1.84 -                options.put("-g:", suboptions);
    1.85 -                // enter all the -g suboptions as "-g:suboption"
    1.86 -                for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
    1.87 -                    String tok = t.nextToken();
    1.88 -                    String opt = "-g:" + tok;
    1.89 -                    options.put(opt, opt);
    1.90 -                }
    1.91 +                options.put("-Xlint:none", option);
    1.92                  return false;
    1.93              }
    1.94          },
    1.95  
    1.96 -        new XOption(XLINT,                                      "opt.Xlint"),
    1.97 -        new XOption(XLINT_CUSTOM,                               "opt.Xlint.suboptlist") {
    1.98 -            public boolean matches(String s) {
    1.99 -                return s.startsWith("-Xlint:");
   1.100 -            }
   1.101 -            public boolean process(Options options, String option) {
   1.102 -                String suboptions = option.substring(7);
   1.103 -                options.put("-Xlint:", suboptions);
   1.104 -                // enter all the -Xlint suboptions as "-Xlint:suboption"
   1.105 -                for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
   1.106 -                    String tok = t.nextToken();
   1.107 -                    String opt = "-Xlint:" + tok;
   1.108 -                    options.put(opt, opt);
   1.109 -                }
   1.110 -                return false;
   1.111 -            }
   1.112 -        },
   1.113 -
   1.114 -        // -nowarn is retained for command-line backward compatibility
   1.115 -        new Option(NOWARN,                                      "opt.nowarn") {
   1.116 -                public boolean process(Options options, String option) {
   1.117 -                    options.put("-Xlint:none", option);
   1.118 -                    return false;
   1.119 -                }
   1.120 -            },
   1.121 -
   1.122          new Option(VERBOSE,                                     "opt.verbose"),
   1.123  
   1.124          // -deprecation is retained for command-line backward compatibility
   1.125          new Option(DEPRECATION,                                 "opt.deprecation") {
   1.126 -                public boolean process(Options options, String option) {
   1.127 -                    options.put("-Xlint:deprecation", option);
   1.128 -                    return false;
   1.129 -                }
   1.130 -            },
   1.131 +            @Override
   1.132 +            public boolean process(Options options, String option) {
   1.133 +                options.put("-Xlint:deprecation", option);
   1.134 +                return false;
   1.135 +            }
   1.136 +        },
   1.137  
   1.138          new Option(CLASSPATH,              "opt.arg.path",      "opt.classpath"),
   1.139          new Option(CP,                     "opt.arg.path",      "opt.classpath") {
   1.140 +            @Override
   1.141              public boolean process(Options options, String option, String arg) {
   1.142                  return super.process(options, "-classpath", arg);
   1.143              }
   1.144          },
   1.145          new Option(SOURCEPATH,             "opt.arg.path",      "opt.sourcepath"),
   1.146          new Option(BOOTCLASSPATH,          "opt.arg.path",      "opt.bootclasspath") {
   1.147 +            @Override
   1.148              public boolean process(Options options, String option, String arg) {
   1.149                  options.remove("-Xbootclasspath/p:");
   1.150                  options.remove("-Xbootclasspath/a:");
   1.151 @@ -327,6 +308,7 @@
   1.152          new XOption(XBOOTCLASSPATH_PREPEND,"opt.arg.path", "opt.Xbootclasspath.p"),
   1.153          new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"),
   1.154          new XOption(XBOOTCLASSPATH,        "opt.arg.path", "opt.bootclasspath") {
   1.155 +            @Override
   1.156              public boolean process(Options options, String option, String arg) {
   1.157                  options.remove("-Xbootclasspath/p:");
   1.158                  options.remove("-Xbootclasspath/a:");
   1.159 @@ -335,48 +317,29 @@
   1.160          },
   1.161          new Option(EXTDIRS,                "opt.arg.dirs",      "opt.extdirs"),
   1.162          new XOption(DJAVA_EXT_DIRS,        "opt.arg.dirs",      "opt.extdirs") {
   1.163 +            @Override
   1.164              public boolean process(Options options, String option, String arg) {
   1.165                  return super.process(options, "-extdirs", arg);
   1.166              }
   1.167          },
   1.168          new Option(ENDORSEDDIRS,            "opt.arg.dirs",     "opt.endorseddirs"),
   1.169          new XOption(DJAVA_ENDORSED_DIRS,    "opt.arg.dirs",     "opt.endorseddirs") {
   1.170 +            @Override
   1.171              public boolean process(Options options, String option, String arg) {
   1.172                  return super.process(options, "-endorseddirs", arg);
   1.173              }
   1.174          },
   1.175 -        new Option(PROC_CUSTOM,                                 "opt.proc.none.only") {
   1.176 -            public boolean matches(String s) {
   1.177 -                return s.equals("-proc:none") || s.equals("-proc:only");
   1.178 -            }
   1.179 -
   1.180 -            public boolean process(Options options, String option) {
   1.181 -                if (option.equals("-proc:none")) {
   1.182 -                    options.remove("-proc:only");
   1.183 -                } else {
   1.184 -                    options.remove("-proc:none");
   1.185 -                }
   1.186 -                options.put(option, option);
   1.187 -                return false;
   1.188 -            }
   1.189 -        },
   1.190 +        new Option(PROC,                                 "opt.proc.none.only",
   1.191 +                Option.ChoiceKind.ONEOF, "none", "only"),
   1.192          new Option(PROCESSOR,           "opt.arg.class.list",   "opt.processor"),
   1.193          new Option(PROCESSORPATH,       "opt.arg.path",         "opt.processorpath"),
   1.194          new Option(D,                   "opt.arg.directory",    "opt.d"),
   1.195          new Option(S,                   "opt.arg.directory",    "opt.sourceDest"),
   1.196 -        new Option(IMPLICIT,                                    "opt.implicit") {
   1.197 -            public boolean matches(String s) {
   1.198 -                return s.equals("-implicit:none") || s.equals("-implicit:class");
   1.199 -            }
   1.200 -            public boolean process(Options options, String option, String operand) {
   1.201 -                int sep = option.indexOf(":");
   1.202 -                options.put(option.substring(0, sep), option.substring(sep+1));
   1.203 -                options.put(option,option);
   1.204 -                return false;
   1.205 -            }
   1.206 -        },
   1.207 +        new Option(IMPLICIT,                                    "opt.implicit",
   1.208 +                Option.ChoiceKind.ONEOF, "none", "class"),
   1.209          new Option(ENCODING,            "opt.arg.encoding",     "opt.encoding"),
   1.210          new Option(SOURCE,              "opt.arg.release",      "opt.source") {
   1.211 +            @Override
   1.212              public boolean process(Options options, String option, String operand) {
   1.213                  Source source = Source.lookup(operand);
   1.214                  if (source == null) {
   1.215 @@ -387,6 +350,7 @@
   1.216              }
   1.217          },
   1.218          new Option(TARGET,              "opt.arg.release",      "opt.target") {
   1.219 +            @Override
   1.220              public boolean process(Options options, String option, String operand) {
   1.221                  Target target = Target.lookup(operand);
   1.222                  if (target == null) {
   1.223 @@ -397,54 +361,62 @@
   1.224              }
   1.225          },
   1.226          new Option(VERSION,                                     "opt.version") {
   1.227 +            @Override
   1.228              public boolean process(Options options, String option) {
   1.229                  helper.printVersion();
   1.230                  return super.process(options, option);
   1.231              }
   1.232          },
   1.233          new HiddenOption(FULLVERSION) {
   1.234 +            @Override
   1.235              public boolean process(Options options, String option) {
   1.236                  helper.printFullVersion();
   1.237                  return super.process(options, option);
   1.238              }
   1.239          },
   1.240          new Option(HELP,                                        "opt.help") {
   1.241 +            @Override
   1.242              public boolean process(Options options, String option) {
   1.243                  helper.printHelp();
   1.244                  return super.process(options, option);
   1.245              }
   1.246          },
   1.247          new Option(A,                "opt.arg.key.equals.value","opt.A") {
   1.248 -                String helpSynopsis() {
   1.249 -                    hasSuffix = true;
   1.250 -                    return super.helpSynopsis();
   1.251 +            @Override
   1.252 +            String helpSynopsis() {
   1.253 +                hasSuffix = true;
   1.254 +                return super.helpSynopsis();
   1.255 +            }
   1.256 +
   1.257 +            @Override
   1.258 +            public boolean matches(String arg) {
   1.259 +                return arg.startsWith("-A");
   1.260 +            }
   1.261 +
   1.262 +            @Override
   1.263 +            public boolean hasArg() {
   1.264 +                return false;
   1.265 +            }
   1.266 +            // Mapping for processor options created in
   1.267 +            // JavacProcessingEnvironment
   1.268 +            @Override
   1.269 +            public boolean process(Options options, String option) {
   1.270 +                int argLength = option.length();
   1.271 +                if (argLength == 2) {
   1.272 +                    helper.error("err.empty.A.argument");
   1.273 +                    return true;
   1.274                  }
   1.275 -
   1.276 -                public boolean matches(String arg) {
   1.277 -                    return arg.startsWith("-A");
   1.278 +                int sepIndex = option.indexOf('=');
   1.279 +                String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
   1.280 +                if (!JavacProcessingEnvironment.isValidOptionName(key)) {
   1.281 +                    helper.error("err.invalid.A.key", option);
   1.282 +                    return true;
   1.283                  }
   1.284 -
   1.285 -                public boolean hasArg() {
   1.286 -                    return false;
   1.287 -                }
   1.288 -                // Mapping for processor options created in
   1.289 -                // JavacProcessingEnvironment
   1.290 -                public boolean process(Options options, String option) {
   1.291 -                    int argLength = option.length();
   1.292 -                    if (argLength == 2) {
   1.293 -                        helper.error("err.empty.A.argument");
   1.294 -                        return true;
   1.295 -                    }
   1.296 -                    int sepIndex = option.indexOf('=');
   1.297 -                    String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
   1.298 -                    if (!JavacProcessingEnvironment.isValidOptionName(key)) {
   1.299 -                        helper.error("err.invalid.A.key", option);
   1.300 -                        return true;
   1.301 -                    }
   1.302 -                    return process(options, option, option);
   1.303 -                }
   1.304 +                return process(options, option, option);
   1.305 +            }
   1.306          },
   1.307          new Option(X,                                           "opt.X") {
   1.308 +            @Override
   1.309              public boolean process(Options options, String option) {
   1.310                  helper.printXhelp();
   1.311                  return super.process(options, option);
   1.312 @@ -454,10 +426,12 @@
   1.313          // This option exists only for the purpose of documenting itself.
   1.314          // It's actually implemented by the launcher.
   1.315          new Option(J,                   "opt.arg.flag",         "opt.J") {
   1.316 +            @Override
   1.317              String helpSynopsis() {
   1.318                  hasSuffix = true;
   1.319                  return super.helpSynopsis();
   1.320              }
   1.321 +            @Override
   1.322              public boolean process(Options options, String option) {
   1.323                  throw new AssertionError
   1.324                      ("the -J flag should be caught by the launcher.");
   1.325 @@ -469,6 +443,7 @@
   1.326  
   1.327          // new Option("-moreinfo",                                      "opt.moreinfo") {
   1.328          new HiddenOption(MOREINFO) {
   1.329 +            @Override
   1.330              public boolean process(Options options, String option) {
   1.331                  Type.moreInfo = true;
   1.332                  return super.process(options, option);
   1.333 @@ -512,6 +487,7 @@
   1.334  
   1.335          // display warnings for generic unchecked operations
   1.336          new HiddenOption(WARNUNCHECKED) {
   1.337 +            @Override
   1.338              public boolean process(Options options, String option) {
   1.339                  options.put("-Xlint:unchecked", option);
   1.340                  return false;
   1.341 @@ -521,6 +497,7 @@
   1.342          new XOption(XMAXERRS,           "opt.arg.number",       "opt.maxerrs"),
   1.343          new XOption(XMAXWARNS,          "opt.arg.number",       "opt.maxwarns"),
   1.344          new XOption(XSTDOUT,            "opt.arg.file",         "opt.Xstdout") {
   1.345 +            @Override
   1.346              public boolean process(Options options, String option, String arg) {
   1.347                  try {
   1.348                      helper.setOut(new PrintWriter(new FileWriter(arg), true));
   1.349 @@ -538,17 +515,8 @@
   1.350  
   1.351          new XOption(XPRINTPROCESSORINFO,                        "opt.printProcessorInfo"),
   1.352  
   1.353 -        new XOption(XPREFER,                                     "opt.prefer") {
   1.354 -            public boolean matches(String s) {
   1.355 -                return s.equals("-Xprefer:source") || s.equals("-Xprefer:newer");
   1.356 -            }
   1.357 -            public boolean process(Options options, String option, String operand) {
   1.358 -                int sep = option.indexOf(":");
   1.359 -                options.put(option.substring(0, sep), option.substring(sep+1));
   1.360 -                options.put(option,option);
   1.361 -                return false;
   1.362 -            }
   1.363 -        },
   1.364 +        new XOption(XPREFER,                                    "opt.prefer",
   1.365 +                Option.ChoiceKind.ONEOF, "source", "newer"),
   1.366  
   1.367          /* -O is a no-op, accepted for backward compatibility. */
   1.368          new HiddenOption(O),
   1.369 @@ -562,10 +530,12 @@
   1.370           */
   1.371          new HiddenOption(XD) {
   1.372              String s;
   1.373 +            @Override
   1.374              public boolean matches(String s) {
   1.375                  this.s = s;
   1.376                  return s.startsWith(name.optionName);
   1.377              }
   1.378 +            @Override
   1.379              public boolean process(Options options, String option) {
   1.380                  s = s.substring(name.optionName.length());
   1.381                  int eq = s.indexOf('=');
   1.382 @@ -586,11 +556,13 @@
   1.383           */
   1.384          new HiddenOption(SOURCEFILE) {
   1.385              String s;
   1.386 +            @Override
   1.387              public boolean matches(String s) {
   1.388                  this.s = s;
   1.389                  return s.endsWith(".java")  // Java source file
   1.390                      || SourceVersion.isName(s);   // Legal type name
   1.391              }
   1.392 +            @Override
   1.393              public boolean process(Options options, String option) {
   1.394                  if (s.endsWith(".java") ) {
   1.395                      File f = new File(s);
   1.396 @@ -612,4 +584,15 @@
   1.397      };
   1.398      }
   1.399  
   1.400 +    private static Collection<String> getXLintChoices() {
   1.401 +        Collection<String> choices = new LinkedHashSet<String>();
   1.402 +        choices.add("all");
   1.403 +        for (Lint.LintCategory c : Lint.LintCategory.values())
   1.404 +            choices.add(c.option);
   1.405 +        for (Lint.LintCategory c : Lint.LintCategory.values())
   1.406 +            choices.add("-" + c.option);
   1.407 +        choices.add("none");
   1.408 +        return choices;
   1.409 +    }
   1.410 +
   1.411  }

mercurial