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

Mon, 09 Mar 2009 13:29:06 -0700

author
xdono
date
Mon, 09 Mar 2009 13:29:06 -0700
changeset 229
03bcd66bd8e7
parent 54
eaf608c64fec
child 377
d9febdd5ae21
permissions
-rw-r--r--

6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair

     1 /*
     2  * Copyright 2006-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.main;
    28 import com.sun.tools.javac.util.Log;
    29 import com.sun.tools.javac.util.Options;
    30 import java.io.PrintWriter;
    31 import java.util.Arrays;
    32 import java.util.Collection;
    34 /**
    35  * TODO: describe com.sun.tools.javac.main.JavacOption
    36  *
    37  * <p><b>This is NOT part of any API supported by Sun Microsystems.
    38  * If you write code that depends on this, you do so at your own
    39  * risk.  This code and its internal interfaces are subject to change
    40  * or deletion without notice.</b></p>
    41  */
    42 public interface JavacOption {
    44     OptionKind getKind();
    46     /** Does this option take a (separate) operand?
    47      *  @return true if this option takes a separate operand
    48      */
    49     boolean hasArg();
    51     /** Does argument string match option pattern?
    52      *  @param arg   the command line argument string
    53      *  @return true if {@code arg} matches this option
    54      */
    55     boolean matches(String arg);
    57     /** Process an option with an argument.
    58      *  @param options the accumulated set of analyzed options
    59      *  @param option  the option to be processed
    60      *  @param arg     the arg for the option to be processed
    61      *  @return true if an error was detected
    62      */
    63     boolean process(Options options, String option, String arg);
    65     /** Process the option with no argument.
    66      *  @param options the accumulated set of analyzed options
    67      *  @param option  the option to be processed
    68      *  @return true if an error was detected
    69      */
    70     boolean process(Options options, String option);
    72     OptionName getName();
    74     enum OptionKind {
    75         NORMAL,
    76         EXTENDED,
    77         HIDDEN,
    78     }
    80     enum ChoiceKind {
    81         ONEOF,
    82         ANYOF
    83     }
    85     /** This class represents an option recognized by the main program
    86      */
    87     static class Option implements JavacOption {
    89         /** Option string.
    90          */
    91         OptionName name;
    93         /** Documentation key for arguments.
    94          */
    95         String argsNameKey;
    97         /** Documentation key for description.
    98          */
    99         String descrKey;
   101         /** Suffix option (-foo=bar or -foo:bar)
   102          */
   103         boolean hasSuffix;
   105         /** The kind of choices for this option, if any.
   106          */
   107         ChoiceKind choiceKind;
   109         /** The choices for this option, if any.
   110          */
   111         Collection<String> choices;
   113         Option(OptionName name, String argsNameKey, String descrKey) {
   114             this.name = name;
   115             this.argsNameKey = argsNameKey;
   116             this.descrKey = descrKey;
   117             char lastChar = name.optionName.charAt(name.optionName.length()-1);
   118             hasSuffix = lastChar == ':' || lastChar == '=';
   119         }
   121         Option(OptionName name, String descrKey) {
   122             this(name, null, descrKey);
   123         }
   125         Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) {
   126             this(name, descrKey, choiceKind, Arrays.asList(choices));
   127         }
   129         Option(OptionName name, String descrKey, ChoiceKind choiceKind, Collection<String> choices) {
   130             this(name, null, descrKey);
   131             if (choiceKind == null || choices == null)
   132                 throw new NullPointerException();
   133             this.choiceKind = choiceKind;
   134             this.choices = choices;
   135         }
   137         @Override
   138         public String toString() {
   139             return name.optionName;
   140         }
   142         public boolean hasArg() {
   143             return argsNameKey != null && !hasSuffix;
   144         }
   146         public boolean matches(String option) {
   147             if (!hasSuffix)
   148                 return option.equals(name.optionName);
   150             if (!option.startsWith(name.optionName))
   151                 return false;
   153             if (choices != null) {
   154                 String arg = option.substring(name.optionName.length());
   155                 if (choiceKind == ChoiceKind.ONEOF)
   156                     return choices.contains(arg);
   157                 else {
   158                     for (String a: arg.split(",+")) {
   159                         if (!choices.contains(a))
   160                             return false;
   161                     }
   162                 }
   163             }
   165             return true;
   166         }
   168         /** Print a line of documentation describing this option, if standard.
   169          * @param out the stream to which to write the documentation
   170          */
   171         void help(PrintWriter out) {
   172             String s = "  " + helpSynopsis();
   173             out.print(s);
   174             for (int j = Math.min(s.length(), 28); j < 29; j++) out.print(" ");
   175             Log.printLines(out, Main.getLocalizedString(descrKey));
   176         }
   178         String helpSynopsis() {
   179             StringBuilder sb = new StringBuilder();
   180             sb.append(name);
   181             if (argsNameKey == null) {
   182                 if (choices != null) {
   183                     String sep = "{";
   184                     for (String c: choices) {
   185                         sb.append(sep);
   186                         sb.append(c);
   187                         sep = ",";
   188                     }
   189                     sb.append("}");
   190                 }
   191             } else {
   192                 if (!hasSuffix)
   193                     sb.append(" ");
   194                 sb.append(Main.getLocalizedString(argsNameKey));
   195             }
   197             return sb.toString();
   198         }
   200         /** Print a line of documentation describing this option, if non-standard.
   201          *  @param out the stream to which to write the documentation
   202          */
   203         void xhelp(PrintWriter out) {}
   205         /** Process the option (with arg). Return true if error detected.
   206          */
   207         public boolean process(Options options, String option, String arg) {
   208             if (options != null) {
   209                 if (choices != null) {
   210                     if (choiceKind == ChoiceKind.ONEOF) {
   211                         // some clients like to see just one of option+choice set
   212                         for (String c: choices)
   213                             options.remove(option + c);
   214                         String opt = option + arg;
   215                         options.put(opt, opt);
   216                         // some clients like to see option (without trailing ":")
   217                         // set to arg
   218                         String nm = option.substring(0, option.length() - 1);
   219                         options.put(nm, arg);
   220                     } else {
   221                         // set option+word for each word in arg
   222                         for (String a: arg.split(",+")) {
   223                             String opt = option + a;
   224                             options.put(opt, opt);
   225                         }
   226                     }
   227                 }
   228                 options.put(option, arg);
   229             }
   230             return false;
   231         }
   233         /** Process the option (without arg). Return true if error detected.
   234          */
   235         public boolean process(Options options, String option) {
   236             if (hasSuffix)
   237                 return process(options, name.optionName, option.substring(name.optionName.length()));
   238             else
   239                 return process(options, option, option);
   240         }
   242         public OptionKind getKind() { return OptionKind.NORMAL; }
   244         public OptionName getName() { return name; }
   245     };
   247     /** A nonstandard or extended (-X) option
   248      */
   249     static class XOption extends Option {
   250         XOption(OptionName name, String argsNameKey, String descrKey) {
   251             super(name, argsNameKey, descrKey);
   252         }
   253         XOption(OptionName name, String descrKey) {
   254             this(name, null, descrKey);
   255         }
   256         XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) {
   257             super(name, descrKey, kind, choices);
   258         }
   259         XOption(OptionName name, String descrKey, ChoiceKind kind, Collection<String> choices) {
   260             super(name, descrKey, kind, choices);
   261         }
   262         @Override
   263         void help(PrintWriter out) {}
   264         @Override
   265         void xhelp(PrintWriter out) { super.help(out); }
   266         @Override
   267         public OptionKind getKind() { return OptionKind.EXTENDED; }
   268     };
   270     /** A hidden (implementor) option
   271      */
   272     static class HiddenOption extends Option {
   273         HiddenOption(OptionName name) {
   274             super(name, null, null);
   275         }
   276         HiddenOption(OptionName name, String argsNameKey) {
   277             super(name, argsNameKey, null);
   278         }
   279         @Override
   280         void help(PrintWriter out) {}
   281         @Override
   282         void xhelp(PrintWriter out) {}
   283         @Override
   284         public OptionKind getKind() { return OptionKind.HIDDEN; }
   285     };
   287 }

mercurial