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