author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 11053 | 48713f779b1d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.main; |
|
27 |
||
3661 | 28 |
import java.io.PrintWriter; |
29 |
import java.util.LinkedHashMap; |
|
30 |
import java.util.Map; |
|
10 | 31 |
import com.sun.tools.javac.util.Log; |
32 |
import com.sun.tools.javac.util.Options; |
|
33 |
||
34 |
/** |
|
35 |
* TODO: describe com.sun.tools.javac.main.JavacOption |
|
36 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
37 |
* <p><b>This is NOT part of any supported API. |
10 | 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 { |
|
43 |
||
44 |
OptionKind getKind(); |
|
45 |
||
168 | 46 |
/** Does this option take a (separate) operand? |
47 |
* @return true if this option takes a separate operand |
|
48 |
*/ |
|
10 | 49 |
boolean hasArg(); |
50 |
||
51 |
/** Does argument string match option pattern? |
|
168 | 52 |
* @param arg the command line argument string |
53 |
* @return true if {@code arg} matches this option |
|
10 | 54 |
*/ |
55 |
boolean matches(String arg); |
|
56 |
||
168 | 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 |
|
10 | 62 |
*/ |
63 |
boolean process(Options options, String option, String arg); |
|
64 |
||
168 | 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 |
|
10 | 69 |
*/ |
70 |
boolean process(Options options, String option); |
|
71 |
||
72 |
OptionName getName(); |
|
73 |
||
74 |
enum OptionKind { |
|
75 |
NORMAL, |
|
76 |
EXTENDED, |
|
77 |
HIDDEN, |
|
78 |
} |
|
79 |
||
168 | 80 |
enum ChoiceKind { |
81 |
ONEOF, |
|
82 |
ANYOF |
|
83 |
} |
|
84 |
||
10 | 85 |
/** This class represents an option recognized by the main program |
86 |
*/ |
|
87 |
static class Option implements JavacOption { |
|
88 |
||
89 |
/** Option string. |
|
90 |
*/ |
|
91 |
OptionName name; |
|
92 |
||
93 |
/** Documentation key for arguments. |
|
94 |
*/ |
|
95 |
String argsNameKey; |
|
96 |
||
97 |
/** Documentation key for description. |
|
98 |
*/ |
|
99 |
String descrKey; |
|
100 |
||
101 |
/** Suffix option (-foo=bar or -foo:bar) |
|
102 |
*/ |
|
103 |
boolean hasSuffix; |
|
104 |
||
168 | 105 |
/** The kind of choices for this option, if any. |
106 |
*/ |
|
107 |
ChoiceKind choiceKind; |
|
108 |
||
3661 | 109 |
/** The choices for this option, if any, and whether or not the choices |
110 |
* are hidden |
|
168 | 111 |
*/ |
3661 | 112 |
Map<String,Boolean> choices; |
168 | 113 |
|
10 | 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 |
} |
|
168 | 121 |
|
10 | 122 |
Option(OptionName name, String descrKey) { |
123 |
this(name, null, descrKey); |
|
124 |
} |
|
125 |
||
168 | 126 |
Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) { |
3661 | 127 |
this(name, descrKey, choiceKind, createChoices(choices)); |
168 | 128 |
} |
129 |
||
3661 | 130 |
private static Map<String,Boolean> createChoices(String... choices) { |
131 |
Map<String,Boolean> map = new LinkedHashMap<String,Boolean>(); |
|
132 |
for (String c: choices) |
|
4412 | 133 |
map.put(c, false); |
3661 | 134 |
return map; |
135 |
} |
|
136 |
||
137 |
Option(OptionName name, String descrKey, ChoiceKind choiceKind, |
|
138 |
Map<String,Boolean> choices) { |
|
168 | 139 |
this(name, null, descrKey); |
140 |
if (choiceKind == null || choices == null) |
|
141 |
throw new NullPointerException(); |
|
142 |
this.choiceKind = choiceKind; |
|
143 |
this.choices = choices; |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
10 | 147 |
public String toString() { |
148 |
return name.optionName; |
|
149 |
} |
|
150 |
||
151 |
public boolean hasArg() { |
|
152 |
return argsNameKey != null && !hasSuffix; |
|
153 |
} |
|
154 |
||
168 | 155 |
public boolean matches(String option) { |
156 |
if (!hasSuffix) |
|
157 |
return option.equals(name.optionName); |
|
158 |
||
159 |
if (!option.startsWith(name.optionName)) |
|
160 |
return false; |
|
161 |
||
162 |
if (choices != null) { |
|
163 |
String arg = option.substring(name.optionName.length()); |
|
164 |
if (choiceKind == ChoiceKind.ONEOF) |
|
3661 | 165 |
return choices.keySet().contains(arg); |
168 | 166 |
else { |
167 |
for (String a: arg.split(",+")) { |
|
3661 | 168 |
if (!choices.keySet().contains(a)) |
168 | 169 |
return false; |
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
return true; |
|
10 | 175 |
} |
176 |
||
177 |
/** Print a line of documentation describing this option, if standard. |
|
168 | 178 |
* @param out the stream to which to write the documentation |
10 | 179 |
*/ |
180 |
void help(PrintWriter out) { |
|
181 |
String s = " " + helpSynopsis(); |
|
182 |
out.print(s); |
|
168 | 183 |
for (int j = Math.min(s.length(), 28); j < 29; j++) out.print(" "); |
10 | 184 |
Log.printLines(out, Main.getLocalizedString(descrKey)); |
185 |
} |
|
168 | 186 |
|
10 | 187 |
String helpSynopsis() { |
168 | 188 |
StringBuilder sb = new StringBuilder(); |
189 |
sb.append(name); |
|
190 |
if (argsNameKey == null) { |
|
191 |
if (choices != null) { |
|
192 |
String sep = "{"; |
|
3661 | 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 |
} |
|
168 | 199 |
} |
200 |
sb.append("}"); |
|
201 |
} |
|
202 |
} else { |
|
203 |
if (!hasSuffix) |
|
204 |
sb.append(" "); |
|
205 |
sb.append(Main.getLocalizedString(argsNameKey)); |
|
206 |
} |
|
207 |
||
208 |
return sb.toString(); |
|
10 | 209 |
} |
210 |
||
211 |
/** Print a line of documentation describing this option, if non-standard. |
|
168 | 212 |
* @param out the stream to which to write the documentation |
10 | 213 |
*/ |
214 |
void xhelp(PrintWriter out) {} |
|
215 |
||
216 |
/** Process the option (with arg). Return true if error detected. |
|
217 |
*/ |
|
218 |
public boolean process(Options options, String option, String arg) { |
|
168 | 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 |
|
3661 | 223 |
for (String s: choices.keySet()) |
224 |
options.remove(option + s); |
|
168 | 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 |
} |
|
10 | 239 |
options.put(option, arg); |
168 | 240 |
} |
10 | 241 |
return false; |
242 |
} |
|
243 |
||
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 |
} |
|
252 |
||
253 |
public OptionKind getKind() { return OptionKind.NORMAL; } |
|
254 |
||
255 |
public OptionName getName() { return name; } |
|
256 |
}; |
|
257 |
||
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 |
} |
|
168 | 267 |
XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) { |
268 |
super(name, descrKey, kind, choices); |
|
269 |
} |
|
3661 | 270 |
XOption(OptionName name, String descrKey, ChoiceKind kind, Map<String,Boolean> choices) { |
168 | 271 |
super(name, descrKey, kind, choices); |
272 |
} |
|
273 |
@Override |
|
10 | 274 |
void help(PrintWriter out) {} |
168 | 275 |
@Override |
10 | 276 |
void xhelp(PrintWriter out) { super.help(out); } |
168 | 277 |
@Override |
10 | 278 |
public OptionKind getKind() { return OptionKind.EXTENDED; } |
279 |
}; |
|
280 |
||
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 |
} |
|
168 | 290 |
@Override |
10 | 291 |
void help(PrintWriter out) {} |
168 | 292 |
@Override |
10 | 293 |
void xhelp(PrintWriter out) {} |
168 | 294 |
@Override |
10 | 295 |
public OptionKind getKind() { return OptionKind.HIDDEN; } |
296 |
}; |
|
297 |
||
298 |
} |