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

Fri, 21 Aug 2009 14:58:21 -0700

author
jjg
date
Fri, 21 Aug 2009 14:58:21 -0700
changeset 377
d9febdd5ae21
parent 54
eaf608c64fec
child 438
2ebae181a4ab
permissions
-rw-r--r--

6873845: refine access to symbol file
Reviewed-by: darcy

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
jjg@377 28 import java.io.PrintWriter;
jjg@377 29 import java.util.LinkedHashMap;
jjg@377 30 import java.util.Map;
duke@1 31 import com.sun.tools.javac.util.Log;
duke@1 32 import com.sun.tools.javac.util.Options;
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@377 109 /** The choices for this option, if any, and whether or not the choices
jjg@377 110 * are hidden
jjg@11 111 */
jjg@377 112 Map<String,Boolean> choices;
jjg@11 113
duke@1 114 Option(OptionName name, String argsNameKey, String descrKey) {
duke@1 115 this.name = name;
duke@1 116 this.argsNameKey = argsNameKey;
duke@1 117 this.descrKey = descrKey;
duke@1 118 char lastChar = name.optionName.charAt(name.optionName.length()-1);
duke@1 119 hasSuffix = lastChar == ':' || lastChar == '=';
duke@1 120 }
jjg@11 121
duke@1 122 Option(OptionName name, String descrKey) {
duke@1 123 this(name, null, descrKey);
duke@1 124 }
duke@1 125
jjg@11 126 Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) {
jjg@377 127 this(name, descrKey, choiceKind, createChoices(choices));
jjg@11 128 }
jjg@11 129
jjg@377 130 private static Map<String,Boolean> createChoices(String... choices) {
jjg@377 131 Map<String,Boolean> map = new LinkedHashMap<String,Boolean>();
jjg@377 132 for (String c: choices)
jjg@377 133 map.put(c, true);
jjg@377 134 return map;
jjg@377 135 }
jjg@377 136
jjg@377 137 Option(OptionName name, String descrKey, ChoiceKind choiceKind,
jjg@377 138 Map<String,Boolean> choices) {
jjg@11 139 this(name, null, descrKey);
jjg@11 140 if (choiceKind == null || choices == null)
jjg@11 141 throw new NullPointerException();
jjg@11 142 this.choiceKind = choiceKind;
jjg@11 143 this.choices = choices;
jjg@11 144 }
jjg@11 145
jjg@11 146 @Override
duke@1 147 public String toString() {
duke@1 148 return name.optionName;
duke@1 149 }
duke@1 150
duke@1 151 public boolean hasArg() {
duke@1 152 return argsNameKey != null && !hasSuffix;
duke@1 153 }
duke@1 154
jjg@11 155 public boolean matches(String option) {
jjg@11 156 if (!hasSuffix)
jjg@11 157 return option.equals(name.optionName);
jjg@11 158
jjg@11 159 if (!option.startsWith(name.optionName))
jjg@11 160 return false;
jjg@11 161
jjg@11 162 if (choices != null) {
jjg@11 163 String arg = option.substring(name.optionName.length());
jjg@11 164 if (choiceKind == ChoiceKind.ONEOF)
jjg@377 165 return choices.keySet().contains(arg);
jjg@11 166 else {
jjg@11 167 for (String a: arg.split(",+")) {
jjg@377 168 if (!choices.keySet().contains(a))
jjg@11 169 return false;
jjg@11 170 }
jjg@11 171 }
jjg@11 172 }
jjg@11 173
jjg@11 174 return true;
duke@1 175 }
duke@1 176
duke@1 177 /** Print a line of documentation describing this option, if standard.
jjg@11 178 * @param out the stream to which to write the documentation
duke@1 179 */
duke@1 180 void help(PrintWriter out) {
duke@1 181 String s = " " + helpSynopsis();
duke@1 182 out.print(s);
jjg@11 183 for (int j = Math.min(s.length(), 28); j < 29; j++) out.print(" ");
duke@1 184 Log.printLines(out, Main.getLocalizedString(descrKey));
duke@1 185 }
jjg@11 186
duke@1 187 String helpSynopsis() {
jjg@11 188 StringBuilder sb = new StringBuilder();
jjg@11 189 sb.append(name);
jjg@11 190 if (argsNameKey == null) {
jjg@11 191 if (choices != null) {
jjg@11 192 String sep = "{";
jjg@377 193 for (Map.Entry<String,Boolean> e: choices.entrySet()) {
jjg@377 194 if (!e.getValue()) {
jjg@377 195 sb.append(sep);
jjg@377 196 sb.append(e.getKey());
jjg@377 197 sep = ",";
jjg@377 198 }
jjg@11 199 }
jjg@11 200 sb.append("}");
jjg@11 201 }
jjg@11 202 } else {
jjg@11 203 if (!hasSuffix)
jjg@11 204 sb.append(" ");
jjg@11 205 sb.append(Main.getLocalizedString(argsNameKey));
jjg@11 206 }
jjg@11 207
jjg@11 208 return sb.toString();
duke@1 209 }
duke@1 210
duke@1 211 /** Print a line of documentation describing this option, if non-standard.
jjg@11 212 * @param out the stream to which to write the documentation
duke@1 213 */
duke@1 214 void xhelp(PrintWriter out) {}
duke@1 215
duke@1 216 /** Process the option (with arg). Return true if error detected.
duke@1 217 */
duke@1 218 public boolean process(Options options, String option, String arg) {
jjg@11 219 if (options != null) {
jjg@11 220 if (choices != null) {
jjg@11 221 if (choiceKind == ChoiceKind.ONEOF) {
jjg@11 222 // some clients like to see just one of option+choice set
jjg@377 223 for (String s: choices.keySet())
jjg@377 224 options.remove(option + s);
jjg@11 225 String opt = option + arg;
jjg@11 226 options.put(opt, opt);
jjg@11 227 // some clients like to see option (without trailing ":")
jjg@11 228 // set to arg
jjg@11 229 String nm = option.substring(0, option.length() - 1);
jjg@11 230 options.put(nm, arg);
jjg@11 231 } else {
jjg@11 232 // set option+word for each word in arg
jjg@11 233 for (String a: arg.split(",+")) {
jjg@11 234 String opt = option + a;
jjg@11 235 options.put(opt, opt);
jjg@11 236 }
jjg@11 237 }
jjg@11 238 }
duke@1 239 options.put(option, arg);
jjg@11 240 }
duke@1 241 return false;
duke@1 242 }
duke@1 243
duke@1 244 /** Process the option (without arg). Return true if error detected.
duke@1 245 */
duke@1 246 public boolean process(Options options, String option) {
duke@1 247 if (hasSuffix)
duke@1 248 return process(options, name.optionName, option.substring(name.optionName.length()));
duke@1 249 else
duke@1 250 return process(options, option, option);
duke@1 251 }
duke@1 252
duke@1 253 public OptionKind getKind() { return OptionKind.NORMAL; }
duke@1 254
duke@1 255 public OptionName getName() { return name; }
duke@1 256 };
duke@1 257
duke@1 258 /** A nonstandard or extended (-X) option
duke@1 259 */
duke@1 260 static class XOption extends Option {
duke@1 261 XOption(OptionName name, String argsNameKey, String descrKey) {
duke@1 262 super(name, argsNameKey, descrKey);
duke@1 263 }
duke@1 264 XOption(OptionName name, String descrKey) {
duke@1 265 this(name, null, descrKey);
duke@1 266 }
jjg@11 267 XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) {
jjg@11 268 super(name, descrKey, kind, choices);
jjg@11 269 }
jjg@377 270 XOption(OptionName name, String descrKey, ChoiceKind kind, Map<String,Boolean> choices) {
jjg@11 271 super(name, descrKey, kind, choices);
jjg@11 272 }
jjg@11 273 @Override
duke@1 274 void help(PrintWriter out) {}
jjg@11 275 @Override
duke@1 276 void xhelp(PrintWriter out) { super.help(out); }
jjg@11 277 @Override
duke@1 278 public OptionKind getKind() { return OptionKind.EXTENDED; }
duke@1 279 };
duke@1 280
duke@1 281 /** A hidden (implementor) option
duke@1 282 */
duke@1 283 static class HiddenOption extends Option {
duke@1 284 HiddenOption(OptionName name) {
duke@1 285 super(name, null, null);
duke@1 286 }
duke@1 287 HiddenOption(OptionName name, String argsNameKey) {
duke@1 288 super(name, argsNameKey, null);
duke@1 289 }
jjg@11 290 @Override
duke@1 291 void help(PrintWriter out) {}
jjg@11 292 @Override
duke@1 293 void xhelp(PrintWriter out) {}
jjg@11 294 @Override
duke@1 295 public OptionKind getKind() { return OptionKind.HIDDEN; }
duke@1 296 };
duke@1 297
duke@1 298 }

mercurial