Tue, 13 Dec 2011 11:21:28 -0800
7120736: refactor javac option handling
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
jjg@1136 | 2 | * Copyright (c) 2006, 2011, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.main; |
duke@1 | 27 | |
jjg@377 | 28 | import java.util.LinkedHashMap; |
jjg@377 | 29 | import java.util.Map; |
duke@1 | 30 | import com.sun.tools.javac.util.Log; |
jjg@1136 | 31 | import com.sun.tools.javac.util.Log.PrefixKind; |
jjg@1136 | 32 | import com.sun.tools.javac.util.Log.WriterKind; |
duke@1 | 33 | import com.sun.tools.javac.util.Options; |
duke@1 | 34 | |
duke@1 | 35 | /** |
duke@1 | 36 | * TODO: describe com.sun.tools.javac.main.JavacOption |
duke@1 | 37 | * |
jjg@581 | 38 | * <p><b>This is NOT part of any supported API. |
duke@1 | 39 | * If you write code that depends on this, you do so at your own |
duke@1 | 40 | * risk. This code and its internal interfaces are subject to change |
duke@1 | 41 | * or deletion without notice.</b></p> |
duke@1 | 42 | */ |
duke@1 | 43 | public interface JavacOption { |
duke@1 | 44 | |
duke@1 | 45 | OptionKind getKind(); |
duke@1 | 46 | |
jjg@11 | 47 | /** Does this option take a (separate) operand? |
jjg@11 | 48 | * @return true if this option takes a separate operand |
jjg@11 | 49 | */ |
duke@1 | 50 | boolean hasArg(); |
duke@1 | 51 | |
duke@1 | 52 | /** Does argument string match option pattern? |
jjg@11 | 53 | * @param arg the command line argument string |
jjg@11 | 54 | * @return true if {@code arg} matches this option |
duke@1 | 55 | */ |
duke@1 | 56 | boolean matches(String arg); |
duke@1 | 57 | |
jjg@11 | 58 | /** Process an option with an argument. |
jjg@11 | 59 | * @param options the accumulated set of analyzed options |
jjg@11 | 60 | * @param option the option to be processed |
jjg@11 | 61 | * @param arg the arg for the option to be processed |
jjg@11 | 62 | * @return true if an error was detected |
duke@1 | 63 | */ |
duke@1 | 64 | boolean process(Options options, String option, String arg); |
duke@1 | 65 | |
jjg@11 | 66 | /** Process the option with no argument. |
jjg@11 | 67 | * @param options the accumulated set of analyzed options |
jjg@11 | 68 | * @param option the option to be processed |
jjg@11 | 69 | * @return true if an error was detected |
duke@1 | 70 | */ |
duke@1 | 71 | boolean process(Options options, String option); |
duke@1 | 72 | |
duke@1 | 73 | OptionName getName(); |
duke@1 | 74 | |
duke@1 | 75 | enum OptionKind { |
duke@1 | 76 | NORMAL, |
duke@1 | 77 | EXTENDED, |
duke@1 | 78 | HIDDEN, |
duke@1 | 79 | } |
duke@1 | 80 | |
jjg@11 | 81 | enum ChoiceKind { |
jjg@11 | 82 | ONEOF, |
jjg@11 | 83 | ANYOF |
jjg@11 | 84 | } |
jjg@11 | 85 | |
duke@1 | 86 | /** This class represents an option recognized by the main program |
duke@1 | 87 | */ |
duke@1 | 88 | static class Option implements JavacOption { |
duke@1 | 89 | |
duke@1 | 90 | /** Option string. |
duke@1 | 91 | */ |
duke@1 | 92 | OptionName name; |
duke@1 | 93 | |
duke@1 | 94 | /** Documentation key for arguments. |
duke@1 | 95 | */ |
duke@1 | 96 | String argsNameKey; |
duke@1 | 97 | |
duke@1 | 98 | /** Documentation key for description. |
duke@1 | 99 | */ |
duke@1 | 100 | String descrKey; |
duke@1 | 101 | |
duke@1 | 102 | /** Suffix option (-foo=bar or -foo:bar) |
duke@1 | 103 | */ |
duke@1 | 104 | boolean hasSuffix; |
duke@1 | 105 | |
jjg@11 | 106 | /** The kind of choices for this option, if any. |
jjg@11 | 107 | */ |
jjg@11 | 108 | ChoiceKind choiceKind; |
jjg@11 | 109 | |
jjg@377 | 110 | /** The choices for this option, if any, and whether or not the choices |
jjg@377 | 111 | * are hidden |
jjg@11 | 112 | */ |
jjg@377 | 113 | Map<String,Boolean> choices; |
jjg@11 | 114 | |
duke@1 | 115 | Option(OptionName name, String argsNameKey, String descrKey) { |
duke@1 | 116 | this.name = name; |
duke@1 | 117 | this.argsNameKey = argsNameKey; |
duke@1 | 118 | this.descrKey = descrKey; |
duke@1 | 119 | char lastChar = name.optionName.charAt(name.optionName.length()-1); |
duke@1 | 120 | hasSuffix = lastChar == ':' || lastChar == '='; |
duke@1 | 121 | } |
jjg@11 | 122 | |
duke@1 | 123 | Option(OptionName name, String descrKey) { |
duke@1 | 124 | this(name, null, descrKey); |
duke@1 | 125 | } |
duke@1 | 126 | |
jjg@11 | 127 | Option(OptionName name, String descrKey, ChoiceKind choiceKind, String... choices) { |
jjg@377 | 128 | this(name, descrKey, choiceKind, createChoices(choices)); |
jjg@11 | 129 | } |
jjg@11 | 130 | |
jjg@377 | 131 | private static Map<String,Boolean> createChoices(String... choices) { |
jjg@377 | 132 | Map<String,Boolean> map = new LinkedHashMap<String,Boolean>(); |
jjg@377 | 133 | for (String c: choices) |
jjg@438 | 134 | map.put(c, false); |
jjg@377 | 135 | return map; |
jjg@377 | 136 | } |
jjg@377 | 137 | |
jjg@377 | 138 | Option(OptionName name, String descrKey, ChoiceKind choiceKind, |
jjg@377 | 139 | Map<String,Boolean> choices) { |
jjg@11 | 140 | this(name, null, descrKey); |
jjg@11 | 141 | if (choiceKind == null || choices == null) |
jjg@11 | 142 | throw new NullPointerException(); |
jjg@11 | 143 | this.choiceKind = choiceKind; |
jjg@11 | 144 | this.choices = choices; |
jjg@11 | 145 | } |
jjg@11 | 146 | |
jjg@11 | 147 | @Override |
duke@1 | 148 | public String toString() { |
duke@1 | 149 | return name.optionName; |
duke@1 | 150 | } |
duke@1 | 151 | |
duke@1 | 152 | public boolean hasArg() { |
duke@1 | 153 | return argsNameKey != null && !hasSuffix; |
duke@1 | 154 | } |
duke@1 | 155 | |
jjg@11 | 156 | public boolean matches(String option) { |
jjg@11 | 157 | if (!hasSuffix) |
jjg@11 | 158 | return option.equals(name.optionName); |
jjg@11 | 159 | |
jjg@11 | 160 | if (!option.startsWith(name.optionName)) |
jjg@11 | 161 | return false; |
jjg@11 | 162 | |
jjg@11 | 163 | if (choices != null) { |
jjg@11 | 164 | String arg = option.substring(name.optionName.length()); |
jjg@11 | 165 | if (choiceKind == ChoiceKind.ONEOF) |
jjg@377 | 166 | return choices.keySet().contains(arg); |
jjg@11 | 167 | else { |
jjg@11 | 168 | for (String a: arg.split(",+")) { |
jjg@377 | 169 | if (!choices.keySet().contains(a)) |
jjg@11 | 170 | return false; |
jjg@11 | 171 | } |
jjg@11 | 172 | } |
jjg@11 | 173 | } |
jjg@11 | 174 | |
jjg@11 | 175 | return true; |
duke@1 | 176 | } |
duke@1 | 177 | |
duke@1 | 178 | /** Print a line of documentation describing this option, if standard. |
jjg@11 | 179 | * @param out the stream to which to write the documentation |
duke@1 | 180 | */ |
jjg@1136 | 181 | void help(Log log) { |
jjg@1136 | 182 | log.printRawLines(WriterKind.NOTICE, |
jjg@1136 | 183 | String.format(" %-26s %s", |
jjg@1136 | 184 | helpSynopsis(log), |
jjg@1136 | 185 | log.localize(PrefixKind.JAVAC, descrKey))); |
duke@1 | 186 | } |
jjg@11 | 187 | |
jjg@1136 | 188 | String helpSynopsis(Log log) { |
jjg@11 | 189 | StringBuilder sb = new StringBuilder(); |
jjg@11 | 190 | sb.append(name); |
jjg@11 | 191 | if (argsNameKey == null) { |
jjg@11 | 192 | if (choices != null) { |
jjg@11 | 193 | String sep = "{"; |
jjg@377 | 194 | for (Map.Entry<String,Boolean> e: choices.entrySet()) { |
jjg@377 | 195 | if (!e.getValue()) { |
jjg@377 | 196 | sb.append(sep); |
jjg@377 | 197 | sb.append(e.getKey()); |
jjg@377 | 198 | sep = ","; |
jjg@377 | 199 | } |
jjg@11 | 200 | } |
jjg@11 | 201 | sb.append("}"); |
jjg@11 | 202 | } |
jjg@11 | 203 | } else { |
jjg@11 | 204 | if (!hasSuffix) |
jjg@11 | 205 | sb.append(" "); |
jjg@1136 | 206 | sb.append(log.localize(PrefixKind.JAVAC, argsNameKey)); |
jjg@11 | 207 | } |
jjg@11 | 208 | |
jjg@11 | 209 | return sb.toString(); |
duke@1 | 210 | } |
duke@1 | 211 | |
duke@1 | 212 | /** Print a line of documentation describing this option, if non-standard. |
jjg@11 | 213 | * @param out the stream to which to write the documentation |
duke@1 | 214 | */ |
jjg@1136 | 215 | void xhelp(Log log) {} |
duke@1 | 216 | |
duke@1 | 217 | /** Process the option (with arg). Return true if error detected. |
duke@1 | 218 | */ |
duke@1 | 219 | public boolean process(Options options, String option, String arg) { |
jjg@11 | 220 | if (options != null) { |
jjg@11 | 221 | if (choices != null) { |
jjg@11 | 222 | if (choiceKind == ChoiceKind.ONEOF) { |
jjg@11 | 223 | // some clients like to see just one of option+choice set |
jjg@377 | 224 | for (String s: choices.keySet()) |
jjg@377 | 225 | options.remove(option + s); |
jjg@11 | 226 | String opt = option + arg; |
jjg@11 | 227 | options.put(opt, opt); |
jjg@11 | 228 | // some clients like to see option (without trailing ":") |
jjg@11 | 229 | // set to arg |
jjg@11 | 230 | String nm = option.substring(0, option.length() - 1); |
jjg@11 | 231 | options.put(nm, arg); |
jjg@11 | 232 | } else { |
jjg@11 | 233 | // set option+word for each word in arg |
jjg@11 | 234 | for (String a: arg.split(",+")) { |
jjg@11 | 235 | String opt = option + a; |
jjg@11 | 236 | options.put(opt, opt); |
jjg@11 | 237 | } |
jjg@11 | 238 | } |
jjg@11 | 239 | } |
duke@1 | 240 | options.put(option, arg); |
jjg@11 | 241 | } |
duke@1 | 242 | return false; |
duke@1 | 243 | } |
duke@1 | 244 | |
duke@1 | 245 | /** Process the option (without arg). Return true if error detected. |
duke@1 | 246 | */ |
duke@1 | 247 | public boolean process(Options options, String option) { |
duke@1 | 248 | if (hasSuffix) |
duke@1 | 249 | return process(options, name.optionName, option.substring(name.optionName.length())); |
duke@1 | 250 | else |
duke@1 | 251 | return process(options, option, option); |
duke@1 | 252 | } |
duke@1 | 253 | |
duke@1 | 254 | public OptionKind getKind() { return OptionKind.NORMAL; } |
duke@1 | 255 | |
duke@1 | 256 | public OptionName getName() { return name; } |
duke@1 | 257 | }; |
duke@1 | 258 | |
duke@1 | 259 | /** A nonstandard or extended (-X) option |
duke@1 | 260 | */ |
duke@1 | 261 | static class XOption extends Option { |
duke@1 | 262 | XOption(OptionName name, String argsNameKey, String descrKey) { |
duke@1 | 263 | super(name, argsNameKey, descrKey); |
duke@1 | 264 | } |
duke@1 | 265 | XOption(OptionName name, String descrKey) { |
duke@1 | 266 | this(name, null, descrKey); |
duke@1 | 267 | } |
jjg@11 | 268 | XOption(OptionName name, String descrKey, ChoiceKind kind, String... choices) { |
jjg@11 | 269 | super(name, descrKey, kind, choices); |
jjg@11 | 270 | } |
jjg@377 | 271 | XOption(OptionName name, String descrKey, ChoiceKind kind, Map<String,Boolean> choices) { |
jjg@11 | 272 | super(name, descrKey, kind, choices); |
jjg@11 | 273 | } |
jjg@11 | 274 | @Override |
jjg@1136 | 275 | void help(Log log) {} |
jjg@11 | 276 | @Override |
jjg@1136 | 277 | void xhelp(Log log) { super.help(log); } |
jjg@11 | 278 | @Override |
duke@1 | 279 | public OptionKind getKind() { return OptionKind.EXTENDED; } |
duke@1 | 280 | }; |
duke@1 | 281 | |
duke@1 | 282 | /** A hidden (implementor) option |
duke@1 | 283 | */ |
duke@1 | 284 | static class HiddenOption extends Option { |
duke@1 | 285 | HiddenOption(OptionName name) { |
duke@1 | 286 | super(name, null, null); |
duke@1 | 287 | } |
duke@1 | 288 | HiddenOption(OptionName name, String argsNameKey) { |
duke@1 | 289 | super(name, argsNameKey, null); |
duke@1 | 290 | } |
jjg@11 | 291 | @Override |
jjg@1136 | 292 | void help(Log log) {} |
jjg@11 | 293 | @Override |
jjg@1136 | 294 | void xhelp(Log log) {} |
jjg@11 | 295 | @Override |
duke@1 | 296 | public OptionKind getKind() { return OptionKind.HIDDEN; } |
duke@1 | 297 | }; |
duke@1 | 298 | |
duke@1 | 299 | } |