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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 54
eaf608c64fec
child 377
d9febdd5ae21
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@54 2 * Copyright 2006-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.main;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.Log;
duke@1 29 import com.sun.tools.javac.util.Options;
duke@1 30 import java.io.PrintWriter;
jjg@11 31 import java.util.Arrays;
jjg@11 32 import java.util.Collection;
duke@1 33
duke@1 34 /**
duke@1 35 * TODO: describe com.sun.tools.javac.main.JavacOption
duke@1 36 *
duke@1 37 * <p><b>This is NOT part of any API supported by Sun Microsystems.
duke@1 38 * If you write code that depends on this, you do so at your own
duke@1 39 * risk. This code and its internal interfaces are subject to change
duke@1 40 * or deletion without notice.</b></p>
duke@1 41 */
duke@1 42 public interface JavacOption {
duke@1 43
duke@1 44 OptionKind getKind();
duke@1 45
jjg@11 46 /** Does this option take a (separate) operand?
jjg@11 47 * @return true if this option takes a separate operand
jjg@11 48 */
duke@1 49 boolean hasArg();
duke@1 50
duke@1 51 /** Does argument string match option pattern?
jjg@11 52 * @param arg the command line argument string
jjg@11 53 * @return true if {@code arg} matches this option
duke@1 54 */
duke@1 55 boolean matches(String arg);
duke@1 56
jjg@11 57 /** Process an option with an argument.
jjg@11 58 * @param options the accumulated set of analyzed options
jjg@11 59 * @param option the option to be processed
jjg@11 60 * @param arg the arg for the option to be processed
jjg@11 61 * @return true if an error was detected
duke@1 62 */
duke@1 63 boolean process(Options options, String option, String arg);
duke@1 64
jjg@11 65 /** Process the option with no argument.
jjg@11 66 * @param options the accumulated set of analyzed options
jjg@11 67 * @param option the option to be processed
jjg@11 68 * @return true if an error was detected
duke@1 69 */
duke@1 70 boolean process(Options options, String option);
duke@1 71
duke@1 72 OptionName getName();
duke@1 73
duke@1 74 enum OptionKind {
duke@1 75 NORMAL,
duke@1 76 EXTENDED,
duke@1 77 HIDDEN,
duke@1 78 }
duke@1 79
jjg@11 80 enum ChoiceKind {
jjg@11 81 ONEOF,
jjg@11 82 ANYOF
jjg@11 83 }
jjg@11 84
duke@1 85 /** This class represents an option recognized by the main program
duke@1 86 */
duke@1 87 static class Option implements JavacOption {
duke@1 88
duke@1 89 /** Option string.
duke@1 90 */
duke@1 91 OptionName name;
duke@1 92
duke@1 93 /** Documentation key for arguments.
duke@1 94 */
duke@1 95 String argsNameKey;
duke@1 96
duke@1 97 /** Documentation key for description.
duke@1 98 */
duke@1 99 String descrKey;
duke@1 100
duke@1 101 /** Suffix option (-foo=bar or -foo:bar)
duke@1 102 */
duke@1 103 boolean hasSuffix;
duke@1 104
jjg@11 105 /** The kind of choices for this option, if any.
jjg@11 106 */
jjg@11 107 ChoiceKind choiceKind;
jjg@11 108
jjg@11 109 /** The choices for this option, if any.
jjg@11 110 */
jjg@11 111 Collection<String> choices;
jjg@11 112
duke@1 113 Option(OptionName name, String argsNameKey, String descrKey) {
duke@1 114 this.name = name;
duke@1 115 this.argsNameKey = argsNameKey;
duke@1 116 this.descrKey = descrKey;
duke@1 117 char lastChar = name.optionName.charAt(name.optionName.length()-1);
duke@1 118 hasSuffix = lastChar == ':' || lastChar == '=';
duke@1 119 }
jjg@11 120
duke@1 121 Option(OptionName name, String descrKey) {
duke@1 122 this(name, null, descrKey);
duke@1 123 }
duke@1 124
jjg@11 125 Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) {
jjg@11 126 this(name, descrKey, choiceKind, Arrays.asList(choices));
jjg@11 127 }
jjg@11 128
jjg@11 129 Option(OptionName name, String descrKey, ChoiceKind choiceKind, Collection<String> choices) {
jjg@11 130 this(name, null, descrKey);
jjg@11 131 if (choiceKind == null || choices == null)
jjg@11 132 throw new NullPointerException();
jjg@11 133 this.choiceKind = choiceKind;
jjg@11 134 this.choices = choices;
jjg@11 135 }
jjg@11 136
jjg@11 137 @Override
duke@1 138 public String toString() {
duke@1 139 return name.optionName;
duke@1 140 }
duke@1 141
duke@1 142 public boolean hasArg() {
duke@1 143 return argsNameKey != null && !hasSuffix;
duke@1 144 }
duke@1 145
jjg@11 146 public boolean matches(String option) {
jjg@11 147 if (!hasSuffix)
jjg@11 148 return option.equals(name.optionName);
jjg@11 149
jjg@11 150 if (!option.startsWith(name.optionName))
jjg@11 151 return false;
jjg@11 152
jjg@11 153 if (choices != null) {
jjg@11 154 String arg = option.substring(name.optionName.length());
jjg@11 155 if (choiceKind == ChoiceKind.ONEOF)
jjg@11 156 return choices.contains(arg);
jjg@11 157 else {
jjg@11 158 for (String a: arg.split(",+")) {
jjg@11 159 if (!choices.contains(a))
jjg@11 160 return false;
jjg@11 161 }
jjg@11 162 }
jjg@11 163 }
jjg@11 164
jjg@11 165 return true;
duke@1 166 }
duke@1 167
duke@1 168 /** Print a line of documentation describing this option, if standard.
jjg@11 169 * @param out the stream to which to write the documentation
duke@1 170 */
duke@1 171 void help(PrintWriter out) {
duke@1 172 String s = " " + helpSynopsis();
duke@1 173 out.print(s);
jjg@11 174 for (int j = Math.min(s.length(), 28); j < 29; j++) out.print(" ");
duke@1 175 Log.printLines(out, Main.getLocalizedString(descrKey));
duke@1 176 }
jjg@11 177
duke@1 178 String helpSynopsis() {
jjg@11 179 StringBuilder sb = new StringBuilder();
jjg@11 180 sb.append(name);
jjg@11 181 if (argsNameKey == null) {
jjg@11 182 if (choices != null) {
jjg@11 183 String sep = "{";
jjg@11 184 for (String c: choices) {
jjg@11 185 sb.append(sep);
jjg@11 186 sb.append(c);
jjg@11 187 sep = ",";
jjg@11 188 }
jjg@11 189 sb.append("}");
jjg@11 190 }
jjg@11 191 } else {
jjg@11 192 if (!hasSuffix)
jjg@11 193 sb.append(" ");
jjg@11 194 sb.append(Main.getLocalizedString(argsNameKey));
jjg@11 195 }
jjg@11 196
jjg@11 197 return sb.toString();
duke@1 198 }
duke@1 199
duke@1 200 /** Print a line of documentation describing this option, if non-standard.
jjg@11 201 * @param out the stream to which to write the documentation
duke@1 202 */
duke@1 203 void xhelp(PrintWriter out) {}
duke@1 204
duke@1 205 /** Process the option (with arg). Return true if error detected.
duke@1 206 */
duke@1 207 public boolean process(Options options, String option, String arg) {
jjg@11 208 if (options != null) {
jjg@11 209 if (choices != null) {
jjg@11 210 if (choiceKind == ChoiceKind.ONEOF) {
jjg@11 211 // some clients like to see just one of option+choice set
jjg@11 212 for (String c: choices)
jjg@11 213 options.remove(option + c);
jjg@11 214 String opt = option + arg;
jjg@11 215 options.put(opt, opt);
jjg@11 216 // some clients like to see option (without trailing ":")
jjg@11 217 // set to arg
jjg@11 218 String nm = option.substring(0, option.length() - 1);
jjg@11 219 options.put(nm, arg);
jjg@11 220 } else {
jjg@11 221 // set option+word for each word in arg
jjg@11 222 for (String a: arg.split(",+")) {
jjg@11 223 String opt = option + a;
jjg@11 224 options.put(opt, opt);
jjg@11 225 }
jjg@11 226 }
jjg@11 227 }
duke@1 228 options.put(option, arg);
jjg@11 229 }
duke@1 230 return false;
duke@1 231 }
duke@1 232
duke@1 233 /** Process the option (without arg). Return true if error detected.
duke@1 234 */
duke@1 235 public boolean process(Options options, String option) {
duke@1 236 if (hasSuffix)
duke@1 237 return process(options, name.optionName, option.substring(name.optionName.length()));
duke@1 238 else
duke@1 239 return process(options, option, option);
duke@1 240 }
duke@1 241
duke@1 242 public OptionKind getKind() { return OptionKind.NORMAL; }
duke@1 243
duke@1 244 public OptionName getName() { return name; }
duke@1 245 };
duke@1 246
duke@1 247 /** A nonstandard or extended (-X) option
duke@1 248 */
duke@1 249 static class XOption extends Option {
duke@1 250 XOption(OptionName name, String argsNameKey, String descrKey) {
duke@1 251 super(name, argsNameKey, descrKey);
duke@1 252 }
duke@1 253 XOption(OptionName name, String descrKey) {
duke@1 254 this(name, null, descrKey);
duke@1 255 }
jjg@11 256 XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) {
jjg@11 257 super(name, descrKey, kind, choices);
jjg@11 258 }
jjg@11 259 XOption(OptionName name, String descrKey, ChoiceKind kind, Collection<String> choices) {
jjg@11 260 super(name, descrKey, kind, choices);
jjg@11 261 }
jjg@11 262 @Override
duke@1 263 void help(PrintWriter out) {}
jjg@11 264 @Override
duke@1 265 void xhelp(PrintWriter out) { super.help(out); }
jjg@11 266 @Override
duke@1 267 public OptionKind getKind() { return OptionKind.EXTENDED; }
duke@1 268 };
duke@1 269
duke@1 270 /** A hidden (implementor) option
duke@1 271 */
duke@1 272 static class HiddenOption extends Option {
duke@1 273 HiddenOption(OptionName name) {
duke@1 274 super(name, null, null);
duke@1 275 }
duke@1 276 HiddenOption(OptionName name, String argsNameKey) {
duke@1 277 super(name, argsNameKey, null);
duke@1 278 }
jjg@11 279 @Override
duke@1 280 void help(PrintWriter out) {}
jjg@11 281 @Override
duke@1 282 void xhelp(PrintWriter out) {}
jjg@11 283 @Override
duke@1 284 public OptionKind getKind() { return OptionKind.HIDDEN; }
duke@1 285 };
duke@1 286
duke@1 287 }

mercurial