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

Thu, 24 Nov 2011 13:36:20 +0000

author
mcimadamore
date
Thu, 24 Nov 2011 13:36:20 +0000
changeset 1142
c896d95e7469
parent 1136
ae361e7f435a
permissions
-rw-r--r--

7115046: Add AST node for lambda expressions
Summary: Add tree nodes for representing lambda expressions and update relevant visitors interfaces
Reviewed-by: jjg

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

mercurial