Wed, 30 Apr 2014 23:26:43 +0100
8038023: Compiler crash ClassCastException
Summary: Add additional checks on results of ClassReader.readPool
Reviewed-by: vromero
jjg@1158 | 1 | /* |
jjg@1485 | 2 | * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1158 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1158 | 4 | * |
jjg@1158 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1158 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1158 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@1158 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@1158 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@1158 | 10 | * |
jjg@1158 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1158 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1158 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1158 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1158 | 15 | * accompanied this code). |
jjg@1158 | 16 | * |
jjg@1158 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@1158 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1158 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1158 | 20 | * |
jjg@1158 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1158 | 22 | * or visit www.oracle.com if you need additional information or have any |
jjg@1158 | 23 | * questions. |
jjg@1158 | 24 | */ |
jjg@1158 | 25 | |
jjg@1158 | 26 | package com.sun.tools.javac.main; |
jjg@1158 | 27 | |
jjg@1463 | 28 | import java.io.File; |
jjg@1463 | 29 | import java.io.FileWriter; |
jjg@1463 | 30 | import java.io.PrintWriter; |
jjg@1158 | 31 | import java.util.Collections; |
jjg@1463 | 32 | import java.util.EnumSet; |
jjg@1463 | 33 | import java.util.LinkedHashMap; |
jjg@1463 | 34 | import java.util.Map; |
jjg@1463 | 35 | import java.util.Set; |
jjg@1463 | 36 | |
jjg@1463 | 37 | import javax.lang.model.SourceVersion; |
jjg@1463 | 38 | |
jjg@1463 | 39 | import com.sun.tools.doclint.DocLint; |
jjg@1158 | 40 | import com.sun.tools.javac.code.Lint; |
jjg@1158 | 41 | import com.sun.tools.javac.code.Source; |
jjg@1158 | 42 | import com.sun.tools.javac.code.Type; |
jjg@1569 | 43 | import com.sun.tools.javac.jvm.Profile; |
jjg@1158 | 44 | import com.sun.tools.javac.jvm.Target; |
jjg@1463 | 45 | import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
jjg@1463 | 46 | import com.sun.tools.javac.util.Log; |
jjg@1463 | 47 | import com.sun.tools.javac.util.Log.PrefixKind; |
jjg@1463 | 48 | import com.sun.tools.javac.util.Log.WriterKind; |
jjg@1158 | 49 | import com.sun.tools.javac.util.Options; |
jjg@1158 | 50 | import static com.sun.tools.javac.main.Option.ChoiceKind.*; |
jjg@1463 | 51 | import static com.sun.tools.javac.main.Option.OptionGroup.*; |
jjg@1158 | 52 | import static com.sun.tools.javac.main.Option.OptionKind.*; |
jjg@1158 | 53 | |
jjg@1158 | 54 | /** |
jjg@1158 | 55 | * Options for javac. The specific Option to handle a command-line option |
jjg@1158 | 56 | * is identified by searching the members of this enum in order, looking |
jjg@1158 | 57 | * the first {@link #matches match}. The action for an Option is performed |
jjg@1158 | 58 | * by calling {@link #process process}, and by providing a suitable |
jjg@1158 | 59 | * {@link OptionHelper} to provide access the compiler state. |
jjg@1158 | 60 | * |
jjg@1158 | 61 | * <p><b>This is NOT part of any supported API. |
jjg@1158 | 62 | * If you write code that depends on this, you do so at your own |
jjg@1158 | 63 | * risk. This code and its internal interfaces are subject to change |
jjg@1158 | 64 | * or deletion without notice.</b></p> |
jjg@1158 | 65 | */ |
jjg@1158 | 66 | public enum Option { |
jjg@1158 | 67 | G("-g", "opt.g", STANDARD, BASIC), |
jjg@1158 | 68 | |
jjg@1158 | 69 | G_NONE("-g:none", "opt.g.none", STANDARD, BASIC) { |
jjg@1158 | 70 | @Override |
jjg@1158 | 71 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 72 | helper.put("-g:", "none"); |
jjg@1158 | 73 | return false; |
jjg@1158 | 74 | } |
jjg@1158 | 75 | }, |
jjg@1158 | 76 | |
jjg@1158 | 77 | G_CUSTOM("-g:", "opt.g.lines.vars.source", |
jjg@1158 | 78 | STANDARD, BASIC, ANYOF, "lines", "vars", "source"), |
jjg@1158 | 79 | |
jjg@1158 | 80 | XLINT("-Xlint", "opt.Xlint", EXTENDED, BASIC), |
jjg@1158 | 81 | |
jjg@1158 | 82 | XLINT_CUSTOM("-Xlint:", "opt.Xlint.suboptlist", |
jjg@1158 | 83 | EXTENDED, BASIC, ANYOF, getXLintChoices()), |
jjg@1158 | 84 | |
jjg@1463 | 85 | XDOCLINT("-Xdoclint", "opt.Xdoclint", EXTENDED, BASIC), |
jjg@1463 | 86 | |
jjg@1463 | 87 | XDOCLINT_CUSTOM("-Xdoclint:", "opt.Xdoclint.subopts", "opt.Xdoclint.custom", EXTENDED, BASIC) { |
jjg@1463 | 88 | @Override |
jjg@1463 | 89 | public boolean matches(String option) { |
jjg@1463 | 90 | return DocLint.isValidOption( |
jjg@1463 | 91 | option.replace(XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX)); |
jjg@1463 | 92 | } |
jjg@1463 | 93 | |
jjg@1463 | 94 | @Override |
jjg@1463 | 95 | public boolean process(OptionHelper helper, String option) { |
jjg@1463 | 96 | String prev = helper.get(XDOCLINT_CUSTOM); |
jjg@1463 | 97 | String next = (prev == null) ? option : (prev + " " + option); |
jjg@1463 | 98 | helper.put(XDOCLINT_CUSTOM.text, next); |
jjg@1463 | 99 | return false; |
jjg@1463 | 100 | } |
jjg@1463 | 101 | }, |
jjg@1463 | 102 | |
jjg@1158 | 103 | // -nowarn is retained for command-line backward compatibility |
jjg@1158 | 104 | NOWARN("-nowarn", "opt.nowarn", STANDARD, BASIC) { |
jjg@1158 | 105 | @Override |
jjg@1158 | 106 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 107 | helper.put("-Xlint:none", option); |
jjg@1158 | 108 | return false; |
jjg@1158 | 109 | } |
jjg@1158 | 110 | }, |
jjg@1158 | 111 | |
jjg@1158 | 112 | VERBOSE("-verbose", "opt.verbose", STANDARD, BASIC), |
jjg@1158 | 113 | |
jjg@1158 | 114 | // -deprecation is retained for command-line backward compatibility |
jjg@1158 | 115 | DEPRECATION("-deprecation", "opt.deprecation", STANDARD, BASIC) { |
jjg@1158 | 116 | @Override |
jjg@1158 | 117 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 118 | helper.put("-Xlint:deprecation", option); |
jjg@1158 | 119 | return false; |
jjg@1158 | 120 | } |
jjg@1158 | 121 | }, |
jjg@1158 | 122 | |
jjg@1158 | 123 | CLASSPATH("-classpath", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER), |
jjg@1158 | 124 | |
jjg@1158 | 125 | CP("-cp", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER) { |
jjg@1158 | 126 | @Override |
jjg@1158 | 127 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 128 | return super.process(helper, "-classpath", arg); |
jjg@1158 | 129 | } |
jjg@1158 | 130 | }, |
jjg@1158 | 131 | |
jjg@1158 | 132 | SOURCEPATH("-sourcepath", "opt.arg.path", "opt.sourcepath", STANDARD, FILEMANAGER), |
jjg@1158 | 133 | |
jjg@1158 | 134 | BOOTCLASSPATH("-bootclasspath", "opt.arg.path", "opt.bootclasspath", STANDARD, FILEMANAGER) { |
jjg@1158 | 135 | @Override |
jjg@1158 | 136 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 137 | helper.remove("-Xbootclasspath/p:"); |
jjg@1158 | 138 | helper.remove("-Xbootclasspath/a:"); |
jjg@1158 | 139 | return super.process(helper, option, arg); |
jjg@1158 | 140 | } |
jjg@1158 | 141 | }, |
jjg@1158 | 142 | |
jjg@1158 | 143 | XBOOTCLASSPATH_PREPEND("-Xbootclasspath/p:", "opt.arg.path", "opt.Xbootclasspath.p", EXTENDED, FILEMANAGER), |
jjg@1158 | 144 | |
jjg@1158 | 145 | XBOOTCLASSPATH_APPEND("-Xbootclasspath/a:", "opt.arg.path", "opt.Xbootclasspath.a", EXTENDED, FILEMANAGER), |
jjg@1158 | 146 | |
jjg@1158 | 147 | XBOOTCLASSPATH("-Xbootclasspath:", "opt.arg.path", "opt.bootclasspath", EXTENDED, FILEMANAGER) { |
jjg@1158 | 148 | @Override |
jjg@1158 | 149 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 150 | helper.remove("-Xbootclasspath/p:"); |
jjg@1158 | 151 | helper.remove("-Xbootclasspath/a:"); |
jjg@1158 | 152 | return super.process(helper, "-bootclasspath", arg); |
jjg@1158 | 153 | } |
jjg@1158 | 154 | }, |
jjg@1158 | 155 | |
jjg@1158 | 156 | EXTDIRS("-extdirs", "opt.arg.dirs", "opt.extdirs", STANDARD, FILEMANAGER), |
jjg@1158 | 157 | |
jjg@1158 | 158 | DJAVA_EXT_DIRS("-Djava.ext.dirs=", "opt.arg.dirs", "opt.extdirs", EXTENDED, FILEMANAGER) { |
jjg@1158 | 159 | @Override |
jjg@1158 | 160 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 161 | return super.process(helper, "-extdirs", arg); |
jjg@1158 | 162 | } |
jjg@1158 | 163 | }, |
jjg@1158 | 164 | |
jjg@1158 | 165 | ENDORSEDDIRS("-endorseddirs", "opt.arg.dirs", "opt.endorseddirs", STANDARD, FILEMANAGER), |
jjg@1158 | 166 | |
jjg@1158 | 167 | DJAVA_ENDORSED_DIRS("-Djava.endorsed.dirs=", "opt.arg.dirs", "opt.endorseddirs", EXTENDED, FILEMANAGER) { |
jjg@1158 | 168 | @Override |
jjg@1158 | 169 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 170 | return super.process(helper, "-endorseddirs", arg); |
jjg@1158 | 171 | } |
jjg@1158 | 172 | }, |
jjg@1158 | 173 | |
jjg@1158 | 174 | PROC("-proc:", "opt.proc.none.only", STANDARD, BASIC, ONEOF, "none", "only"), |
jjg@1158 | 175 | |
jjg@1158 | 176 | PROCESSOR("-processor", "opt.arg.class.list", "opt.processor", STANDARD, BASIC), |
jjg@1158 | 177 | |
jjg@1158 | 178 | PROCESSORPATH("-processorpath", "opt.arg.path", "opt.processorpath", STANDARD, FILEMANAGER), |
jjg@1158 | 179 | |
jjg@1473 | 180 | PARAMETERS("-parameters","opt.parameters", STANDARD, BASIC), |
jjg@1473 | 181 | |
jjg@1158 | 182 | D("-d", "opt.arg.directory", "opt.d", STANDARD, FILEMANAGER), |
jjg@1158 | 183 | |
jjg@1158 | 184 | S("-s", "opt.arg.directory", "opt.sourceDest", STANDARD, FILEMANAGER), |
jjg@1158 | 185 | |
jjg@1230 | 186 | H("-h", "opt.arg.directory", "opt.headerDest", STANDARD, FILEMANAGER), |
jjg@1230 | 187 | |
jjg@1158 | 188 | IMPLICIT("-implicit:", "opt.implicit", STANDARD, BASIC, ONEOF, "none", "class"), |
jjg@1158 | 189 | |
jjg@1158 | 190 | ENCODING("-encoding", "opt.arg.encoding", "opt.encoding", STANDARD, FILEMANAGER) { |
jjg@1158 | 191 | @Override |
jjg@1158 | 192 | public boolean process(OptionHelper helper, String option, String operand) { |
jjg@1158 | 193 | return super.process(helper, option, operand); |
jjg@1158 | 194 | } |
jjg@1158 | 195 | |
jjg@1158 | 196 | }, |
jjg@1158 | 197 | |
jjg@1158 | 198 | SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) { |
jjg@1158 | 199 | @Override |
jjg@1158 | 200 | public boolean process(OptionHelper helper, String option, String operand) { |
jjg@1158 | 201 | Source source = Source.lookup(operand); |
jjg@1158 | 202 | if (source == null) { |
jjg@1158 | 203 | helper.error("err.invalid.source", operand); |
jjg@1158 | 204 | return true; |
jjg@1158 | 205 | } |
jjg@1158 | 206 | return super.process(helper, option, operand); |
jjg@1158 | 207 | } |
jjg@1158 | 208 | }, |
jjg@1158 | 209 | |
jjg@1158 | 210 | TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) { |
jjg@1158 | 211 | @Override |
jjg@1158 | 212 | public boolean process(OptionHelper helper, String option, String operand) { |
jjg@1158 | 213 | Target target = Target.lookup(operand); |
jjg@1158 | 214 | if (target == null) { |
jjg@1158 | 215 | helper.error("err.invalid.target", operand); |
jjg@1158 | 216 | return true; |
jjg@1158 | 217 | } |
jjg@1158 | 218 | return super.process(helper, option, operand); |
jjg@1158 | 219 | } |
jjg@1158 | 220 | }, |
jjg@1158 | 221 | |
jjg@1569 | 222 | PROFILE("-profile", "opt.arg.profile", "opt.profile", STANDARD, BASIC) { |
jjg@1569 | 223 | @Override |
jjg@1569 | 224 | public boolean process(OptionHelper helper, String option, String operand) { |
jjg@1569 | 225 | Profile profile = Profile.lookup(operand); |
jjg@1569 | 226 | if (profile == null) { |
jjg@1569 | 227 | helper.error("err.invalid.profile", operand); |
jjg@1569 | 228 | return true; |
jjg@1569 | 229 | } |
jjg@1569 | 230 | return super.process(helper, option, operand); |
jjg@1569 | 231 | } |
jjg@1569 | 232 | }, |
jjg@1569 | 233 | |
jjg@1158 | 234 | VERSION("-version", "opt.version", STANDARD, INFO) { |
jjg@1158 | 235 | @Override |
jjg@1158 | 236 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 237 | Log log = helper.getLog(); |
jjg@1158 | 238 | String ownName = helper.getOwnName(); |
jjg@1158 | 239 | log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version()); |
jjg@1158 | 240 | return super.process(helper, option); |
jjg@1158 | 241 | } |
jjg@1158 | 242 | }, |
jjg@1158 | 243 | |
jjg@1158 | 244 | FULLVERSION("-fullversion", null, HIDDEN, INFO) { |
jjg@1158 | 245 | @Override |
jjg@1158 | 246 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 247 | Log log = helper.getLog(); |
jjg@1158 | 248 | String ownName = helper.getOwnName(); |
jjg@1160 | 249 | log.printLines(PrefixKind.JAVAC, "fullVersion", ownName, JavaCompiler.fullVersion()); |
jjg@1158 | 250 | return super.process(helper, option); |
jjg@1158 | 251 | } |
jjg@1158 | 252 | }, |
jjg@1158 | 253 | |
jjg@1158 | 254 | DIAGS("-XDdiags=", null, HIDDEN, INFO) { |
jjg@1158 | 255 | @Override |
jjg@1158 | 256 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 257 | option = option.substring(option.indexOf('=') + 1); |
jjg@1158 | 258 | String diagsOption = option.contains("%") ? |
jjg@1158 | 259 | "-XDdiagsFormat=" : |
jjg@1158 | 260 | "-XDdiags="; |
jjg@1158 | 261 | diagsOption += option; |
jjg@1158 | 262 | if (XD.matches(diagsOption)) |
jjg@1158 | 263 | return XD.process(helper, diagsOption); |
jjg@1158 | 264 | else |
jjg@1158 | 265 | return false; |
jjg@1158 | 266 | } |
jjg@1158 | 267 | }, |
jjg@1158 | 268 | |
jjg@1158 | 269 | HELP("-help", "opt.help", STANDARD, INFO) { |
jjg@1158 | 270 | @Override |
jjg@1158 | 271 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 272 | Log log = helper.getLog(); |
jjg@1158 | 273 | String ownName = helper.getOwnName(); |
jjg@1158 | 274 | log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName); |
jjg@1158 | 275 | for (Option o: getJavaCompilerOptions()) { |
jjg@1158 | 276 | o.help(log, OptionKind.STANDARD); |
jjg@1158 | 277 | } |
jjg@1158 | 278 | log.printNewline(); |
jjg@1158 | 279 | return super.process(helper, option); |
jjg@1158 | 280 | } |
jjg@1158 | 281 | }, |
jjg@1158 | 282 | |
vromero@1442 | 283 | A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC, true) { |
jjg@1158 | 284 | @Override |
jjg@1158 | 285 | public boolean matches(String arg) { |
jjg@1158 | 286 | return arg.startsWith("-A"); |
jjg@1158 | 287 | } |
jjg@1158 | 288 | |
jjg@1158 | 289 | @Override |
jjg@1158 | 290 | public boolean hasArg() { |
jjg@1158 | 291 | return false; |
jjg@1158 | 292 | } |
jjg@1158 | 293 | // Mapping for processor options created in |
jjg@1158 | 294 | // JavacProcessingEnvironment |
jjg@1158 | 295 | @Override |
jjg@1158 | 296 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 297 | int argLength = option.length(); |
jjg@1158 | 298 | if (argLength == 2) { |
jjg@1158 | 299 | helper.error("err.empty.A.argument"); |
jjg@1158 | 300 | return true; |
jjg@1158 | 301 | } |
jjg@1158 | 302 | int sepIndex = option.indexOf('='); |
jjg@1158 | 303 | String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) ); |
jjg@1158 | 304 | if (!JavacProcessingEnvironment.isValidOptionName(key)) { |
jjg@1158 | 305 | helper.error("err.invalid.A.key", option); |
jjg@1158 | 306 | return true; |
jjg@1158 | 307 | } |
jjg@1158 | 308 | return process(helper, option, option); |
jjg@1158 | 309 | } |
jjg@1158 | 310 | }, |
jjg@1158 | 311 | |
jjg@1158 | 312 | X("-X", "opt.X", STANDARD, INFO) { |
jjg@1158 | 313 | @Override |
jjg@1158 | 314 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 315 | Log log = helper.getLog(); |
jjg@1158 | 316 | for (Option o: getJavaCompilerOptions()) { |
jjg@1158 | 317 | o.help(log, OptionKind.EXTENDED); |
jjg@1158 | 318 | } |
jjg@1158 | 319 | log.printNewline(); |
jjg@1158 | 320 | log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer"); |
jjg@1158 | 321 | return super.process(helper, option); |
jjg@1158 | 322 | } |
jjg@1158 | 323 | }, |
jjg@1158 | 324 | |
jjg@1158 | 325 | // This option exists only for the purpose of documenting itself. |
jjg@1158 | 326 | // It's actually implemented by the launcher. |
jjg@1485 | 327 | J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO, true) { |
jjg@1158 | 328 | @Override |
jjg@1158 | 329 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 330 | throw new AssertionError |
jjg@1158 | 331 | ("the -J flag should be caught by the launcher."); |
jjg@1158 | 332 | } |
jjg@1158 | 333 | }, |
jjg@1158 | 334 | |
jjg@1158 | 335 | MOREINFO("-moreinfo", null, HIDDEN, BASIC) { |
jjg@1158 | 336 | @Override |
jjg@1158 | 337 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 338 | Type.moreInfo = true; |
jjg@1158 | 339 | return super.process(helper, option); |
jjg@1158 | 340 | } |
jjg@1158 | 341 | }, |
jjg@1158 | 342 | |
jjg@1158 | 343 | // treat warnings as errors |
jjg@1158 | 344 | WERROR("-Werror", "opt.Werror", STANDARD, BASIC), |
jjg@1158 | 345 | |
jjg@1158 | 346 | // prompt after each error |
jjg@1158 | 347 | // new Option("-prompt", "opt.prompt"), |
jjg@1158 | 348 | PROMPT("-prompt", null, HIDDEN, BASIC), |
jjg@1158 | 349 | |
jjg@1158 | 350 | // dump stack on error |
jjg@1158 | 351 | DOE("-doe", null, HIDDEN, BASIC), |
jjg@1158 | 352 | |
jjg@1158 | 353 | // output source after type erasure |
jjg@1158 | 354 | PRINTSOURCE("-printsource", null, HIDDEN, BASIC), |
jjg@1158 | 355 | |
jjg@1158 | 356 | // display warnings for generic unchecked operations |
jjg@1158 | 357 | WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) { |
jjg@1158 | 358 | @Override |
jjg@1158 | 359 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 360 | helper.put("-Xlint:unchecked", option); |
jjg@1158 | 361 | return false; |
jjg@1158 | 362 | } |
jjg@1158 | 363 | }, |
jjg@1158 | 364 | |
jjg@1158 | 365 | XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC), |
jjg@1158 | 366 | |
jjg@1158 | 367 | XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC), |
jjg@1158 | 368 | |
jjg@1159 | 369 | XSTDOUT("-Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) { |
jjg@1158 | 370 | @Override |
jjg@1158 | 371 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 372 | try { |
jjg@1158 | 373 | Log log = helper.getLog(); |
jjg@1158 | 374 | // TODO: this file should be closed at the end of compilation |
jjg@1158 | 375 | log.setWriters(new PrintWriter(new FileWriter(arg), true)); |
jjg@1158 | 376 | } catch (java.io.IOException e) { |
jjg@1158 | 377 | helper.error("err.error.writing.file", arg, e); |
jjg@1158 | 378 | return true; |
jjg@1158 | 379 | } |
jjg@1158 | 380 | return super.process(helper, option, arg); |
jjg@1158 | 381 | } |
jjg@1158 | 382 | }, |
jjg@1158 | 383 | |
jjg@1158 | 384 | XPRINT("-Xprint", "opt.print", EXTENDED, BASIC), |
jjg@1158 | 385 | |
jjg@1158 | 386 | XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC), |
jjg@1158 | 387 | |
jjg@1158 | 388 | XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC), |
jjg@1158 | 389 | |
jjg@1158 | 390 | XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"), |
jjg@1158 | 391 | |
jjg@1988 | 392 | // see enum PkgInfo |
jjg@1158 | 393 | XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"), |
jjg@1158 | 394 | |
jjg@1158 | 395 | /* -O is a no-op, accepted for backward compatibility. */ |
jjg@1158 | 396 | O("-O", null, HIDDEN, BASIC), |
jjg@1158 | 397 | |
jjg@1158 | 398 | /* -Xjcov produces tables to support the code coverage tool jcov. */ |
jjg@1158 | 399 | XJCOV("-Xjcov", null, HIDDEN, BASIC), |
jjg@1158 | 400 | |
jjg@1416 | 401 | PLUGIN("-Xplugin:", "opt.arg.plugin", "opt.plugin", EXTENDED, BASIC) { |
jjg@1416 | 402 | @Override |
jjg@1416 | 403 | public boolean process(OptionHelper helper, String option) { |
jjg@1416 | 404 | String p = option.substring(option.indexOf(':') + 1); |
jjg@1416 | 405 | String prev = helper.get(PLUGIN); |
jjg@1416 | 406 | helper.put(PLUGIN.text, (prev == null) ? p : prev + '\0' + p.trim()); |
jjg@1416 | 407 | return false; |
jjg@1416 | 408 | } |
jjg@1416 | 409 | }, |
jjg@1416 | 410 | |
mcimadamore@1759 | 411 | XDIAGS("-Xdiags:", "opt.diags", EXTENDED, BASIC, ONEOF, "compact", "verbose"), |
mcimadamore@1759 | 412 | |
jjg@1158 | 413 | /* This is a back door to the compiler's option table. |
jjg@1158 | 414 | * -XDx=y sets the option x to the value y. |
jjg@1158 | 415 | * -XDx sets the option x to the value x. |
jjg@1158 | 416 | */ |
jjg@1158 | 417 | XD("-XD", null, HIDDEN, BASIC) { |
jjg@1158 | 418 | @Override |
jjg@1158 | 419 | public boolean matches(String s) { |
jjg@1158 | 420 | return s.startsWith(text); |
jjg@1158 | 421 | } |
jjg@1158 | 422 | @Override |
jjg@1158 | 423 | public boolean process(OptionHelper helper, String option) { |
vromero@1442 | 424 | option = option.substring(text.length()); |
vromero@1442 | 425 | int eq = option.indexOf('='); |
vromero@1442 | 426 | String key = (eq < 0) ? option : option.substring(0, eq); |
vromero@1442 | 427 | String value = (eq < 0) ? option : option.substring(eq+1); |
jjg@1158 | 428 | helper.put(key, value); |
jjg@1158 | 429 | return false; |
jjg@1158 | 430 | } |
jjg@1158 | 431 | }, |
jjg@1158 | 432 | |
jjg@1158 | 433 | // This option exists only for the purpose of documenting itself. |
jjg@1158 | 434 | // It's actually implemented by the CommandLine class. |
jjg@1485 | 435 | AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO, true) { |
jjg@1158 | 436 | @Override |
jjg@1158 | 437 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 438 | throw new AssertionError("the @ flag should be caught by CommandLine."); |
jjg@1158 | 439 | } |
jjg@1158 | 440 | }, |
jjg@1158 | 441 | |
jjg@1158 | 442 | /* |
jjg@1158 | 443 | * TODO: With apt, the matches method accepts anything if |
jjg@1158 | 444 | * -XclassAsDecls is used; code elsewhere does the lookup to |
jjg@1158 | 445 | * see if the class name is both legal and found. |
jjg@1158 | 446 | * |
jjg@1158 | 447 | * In apt, the process method adds the candidate class file |
jjg@1158 | 448 | * name to a separate list. |
jjg@1158 | 449 | */ |
jjg@1158 | 450 | SOURCEFILE("sourcefile", null, HIDDEN, INFO) { |
jjg@1158 | 451 | @Override |
jjg@1158 | 452 | public boolean matches(String s) { |
jjg@1158 | 453 | return s.endsWith(".java") // Java source file |
jjg@1158 | 454 | || SourceVersion.isName(s); // Legal type name |
jjg@1158 | 455 | } |
jjg@1158 | 456 | @Override |
jjg@1158 | 457 | public boolean process(OptionHelper helper, String option) { |
vromero@1442 | 458 | if (option.endsWith(".java") ) { |
vromero@1442 | 459 | File f = new File(option); |
jjg@1158 | 460 | if (!f.exists()) { |
jjg@1158 | 461 | helper.error("err.file.not.found", f); |
jjg@1158 | 462 | return true; |
jjg@1158 | 463 | } |
jjg@1158 | 464 | if (!f.isFile()) { |
jjg@1158 | 465 | helper.error("err.file.not.file", f); |
jjg@1158 | 466 | return true; |
jjg@1158 | 467 | } |
jjg@1158 | 468 | helper.addFile(f); |
vromero@1442 | 469 | } else { |
vromero@1442 | 470 | helper.addClassName(option); |
jjg@1158 | 471 | } |
jjg@1158 | 472 | return false; |
jjg@1158 | 473 | } |
jjg@1158 | 474 | }; |
jjg@1158 | 475 | |
jjg@1158 | 476 | /** The kind of an Option. This is used by the -help and -X options. */ |
jjg@1158 | 477 | public enum OptionKind { |
jjg@1158 | 478 | /** A standard option, documented by -help. */ |
jjg@1158 | 479 | STANDARD, |
jjg@1158 | 480 | /** An extended option, documented by -X. */ |
jjg@1158 | 481 | EXTENDED, |
jjg@1158 | 482 | /** A hidden option, not documented. */ |
jjg@1158 | 483 | HIDDEN, |
jjg@1158 | 484 | } |
jjg@1158 | 485 | |
jjg@1158 | 486 | /** The group for an Option. This determines the situations in which the |
jjg@1158 | 487 | * option is applicable. */ |
jjg@1158 | 488 | enum OptionGroup { |
jjg@1158 | 489 | /** A basic option, available for use on the command line or via the |
jjg@1158 | 490 | * Compiler API. */ |
jjg@1158 | 491 | BASIC, |
jjg@1158 | 492 | /** An option for javac's standard JavaFileManager. Other file managers |
jjg@1158 | 493 | * may or may not support these options. */ |
jjg@1158 | 494 | FILEMANAGER, |
jjg@1158 | 495 | /** A command-line option that requests information, such as -help. */ |
jjg@1158 | 496 | INFO, |
jjg@1158 | 497 | /** A command-line "option" representing a file or class name. */ |
jjg@1158 | 498 | OPERAND |
jjg@1158 | 499 | } |
jjg@1158 | 500 | |
jjg@1158 | 501 | /** The kind of choice for "choice" options. */ |
jjg@1158 | 502 | enum ChoiceKind { |
jjg@1158 | 503 | /** The expected value is exactly one of the set of choices. */ |
jjg@1158 | 504 | ONEOF, |
jjg@1158 | 505 | /** The expected value is one of more of the set of choices. */ |
jjg@1158 | 506 | ANYOF |
jjg@1158 | 507 | } |
jjg@1158 | 508 | |
jjg@1158 | 509 | public final String text; |
jjg@1158 | 510 | |
jjg@1158 | 511 | final OptionKind kind; |
jjg@1158 | 512 | |
jjg@1158 | 513 | final OptionGroup group; |
jjg@1158 | 514 | |
jjg@1158 | 515 | /** Documentation key for arguments. |
jjg@1158 | 516 | */ |
jjg@1158 | 517 | final String argsNameKey; |
jjg@1158 | 518 | |
jjg@1158 | 519 | /** Documentation key for description. |
jjg@1158 | 520 | */ |
jjg@1158 | 521 | final String descrKey; |
jjg@1158 | 522 | |
jjg@1158 | 523 | /** Suffix option (-foo=bar or -foo:bar) |
jjg@1158 | 524 | */ |
vromero@1442 | 525 | final boolean hasSuffix; |
jjg@1158 | 526 | |
jjg@1158 | 527 | /** The kind of choices for this option, if any. |
jjg@1158 | 528 | */ |
jjg@1158 | 529 | final ChoiceKind choiceKind; |
jjg@1158 | 530 | |
jjg@1158 | 531 | /** The choices for this option, if any, and whether or not the choices |
jjg@1158 | 532 | * are hidden |
jjg@1158 | 533 | */ |
jjg@1158 | 534 | final Map<String,Boolean> choices; |
jjg@1158 | 535 | |
jjg@1158 | 536 | |
jjg@1158 | 537 | Option(String text, String descrKey, |
jjg@1158 | 538 | OptionKind kind, OptionGroup group) { |
vromero@1442 | 539 | this(text, null, descrKey, kind, group, null, null, false); |
jjg@1158 | 540 | } |
jjg@1158 | 541 | |
jjg@1158 | 542 | Option(String text, String argsNameKey, String descrKey, |
jjg@1158 | 543 | OptionKind kind, OptionGroup group) { |
vromero@1442 | 544 | this(text, argsNameKey, descrKey, kind, group, null, null, false); |
vromero@1442 | 545 | } |
vromero@1442 | 546 | |
vromero@1442 | 547 | Option(String text, String argsNameKey, String descrKey, |
vromero@1442 | 548 | OptionKind kind, OptionGroup group, boolean doHasSuffix) { |
vromero@1442 | 549 | this(text, argsNameKey, descrKey, kind, group, null, null, doHasSuffix); |
jjg@1158 | 550 | } |
jjg@1158 | 551 | |
jjg@1158 | 552 | Option(String text, String descrKey, |
jjg@1158 | 553 | OptionKind kind, OptionGroup group, |
jjg@1158 | 554 | ChoiceKind choiceKind, Map<String,Boolean> choices) { |
vromero@1442 | 555 | this(text, null, descrKey, kind, group, choiceKind, choices, false); |
jjg@1158 | 556 | } |
jjg@1158 | 557 | |
jjg@1158 | 558 | Option(String text, String descrKey, |
jjg@1158 | 559 | OptionKind kind, OptionGroup group, |
jjg@1158 | 560 | ChoiceKind choiceKind, String... choices) { |
vromero@1442 | 561 | this(text, null, descrKey, kind, group, choiceKind, |
vromero@1442 | 562 | createChoices(choices), false); |
jjg@1158 | 563 | } |
jjg@1158 | 564 | // where |
jjg@1158 | 565 | private static Map<String,Boolean> createChoices(String... choices) { |
jjg@1158 | 566 | Map<String,Boolean> map = new LinkedHashMap<String,Boolean>(); |
jjg@1158 | 567 | for (String c: choices) |
jjg@1158 | 568 | map.put(c, false); |
jjg@1158 | 569 | return map; |
jjg@1158 | 570 | } |
jjg@1158 | 571 | |
jjg@1158 | 572 | private Option(String text, String argsNameKey, String descrKey, |
jjg@1158 | 573 | OptionKind kind, OptionGroup group, |
vromero@1442 | 574 | ChoiceKind choiceKind, Map<String,Boolean> choices, |
vromero@1442 | 575 | boolean doHasSuffix) { |
jjg@1158 | 576 | this.text = text; |
jjg@1158 | 577 | this.argsNameKey = argsNameKey; |
jjg@1158 | 578 | this.descrKey = descrKey; |
jjg@1158 | 579 | this.kind = kind; |
jjg@1158 | 580 | this.group = group; |
jjg@1158 | 581 | this.choiceKind = choiceKind; |
jjg@1158 | 582 | this.choices = choices; |
jjg@1158 | 583 | char lastChar = text.charAt(text.length()-1); |
vromero@1442 | 584 | this.hasSuffix = doHasSuffix || lastChar == ':' || lastChar == '='; |
jjg@1158 | 585 | } |
jjg@1158 | 586 | |
jjg@1158 | 587 | public String getText() { |
jjg@1158 | 588 | return text; |
jjg@1158 | 589 | } |
jjg@1158 | 590 | |
jjg@1158 | 591 | public OptionKind getKind() { |
jjg@1158 | 592 | return kind; |
jjg@1158 | 593 | } |
jjg@1158 | 594 | |
jjg@1158 | 595 | public boolean hasArg() { |
jjg@1158 | 596 | return argsNameKey != null && !hasSuffix; |
jjg@1158 | 597 | } |
jjg@1158 | 598 | |
jjg@1158 | 599 | public boolean matches(String option) { |
jjg@1158 | 600 | if (!hasSuffix) |
jjg@1158 | 601 | return option.equals(text); |
jjg@1158 | 602 | |
jjg@1158 | 603 | if (!option.startsWith(text)) |
jjg@1158 | 604 | return false; |
jjg@1158 | 605 | |
jjg@1158 | 606 | if (choices != null) { |
jjg@1158 | 607 | String arg = option.substring(text.length()); |
jjg@1158 | 608 | if (choiceKind == ChoiceKind.ONEOF) |
jjg@1158 | 609 | return choices.keySet().contains(arg); |
jjg@1158 | 610 | else { |
jjg@1158 | 611 | for (String a: arg.split(",+")) { |
jjg@1158 | 612 | if (!choices.keySet().contains(a)) |
jjg@1158 | 613 | return false; |
jjg@1158 | 614 | } |
jjg@1158 | 615 | } |
jjg@1158 | 616 | } |
jjg@1158 | 617 | |
jjg@1158 | 618 | return true; |
jjg@1158 | 619 | } |
jjg@1158 | 620 | |
jjg@1158 | 621 | public boolean process(OptionHelper helper, String option, String arg) { |
jjg@1158 | 622 | if (choices != null) { |
jjg@1158 | 623 | if (choiceKind == ChoiceKind.ONEOF) { |
jjg@1158 | 624 | // some clients like to see just one of option+choice set |
jjg@1158 | 625 | for (String s: choices.keySet()) |
jjg@1158 | 626 | helper.remove(option + s); |
jjg@1158 | 627 | String opt = option + arg; |
jjg@1158 | 628 | helper.put(opt, opt); |
jjg@1158 | 629 | // some clients like to see option (without trailing ":") |
jjg@1158 | 630 | // set to arg |
jjg@1158 | 631 | String nm = option.substring(0, option.length() - 1); |
jjg@1158 | 632 | helper.put(nm, arg); |
jjg@1158 | 633 | } else { |
jjg@1158 | 634 | // set option+word for each word in arg |
jjg@1158 | 635 | for (String a: arg.split(",+")) { |
jjg@1158 | 636 | String opt = option + a; |
jjg@1158 | 637 | helper.put(opt, opt); |
jjg@1158 | 638 | } |
jjg@1158 | 639 | } |
jjg@1158 | 640 | } |
jjg@1158 | 641 | helper.put(option, arg); |
jjg@1158 | 642 | return false; |
jjg@1158 | 643 | } |
jjg@1158 | 644 | |
jjg@1158 | 645 | public boolean process(OptionHelper helper, String option) { |
jjg@1158 | 646 | if (hasSuffix) |
jjg@1158 | 647 | return process(helper, text, option.substring(text.length())); |
jjg@1158 | 648 | else |
jjg@1158 | 649 | return process(helper, option, option); |
jjg@1158 | 650 | } |
jjg@1158 | 651 | |
jjg@1158 | 652 | void help(Log log, OptionKind kind) { |
jjg@1158 | 653 | if (this.kind != kind) |
jjg@1158 | 654 | return; |
jjg@1158 | 655 | |
jjg@1158 | 656 | log.printRawLines(WriterKind.NOTICE, |
jjg@1158 | 657 | String.format(" %-26s %s", |
jjg@1158 | 658 | helpSynopsis(log), |
jjg@1158 | 659 | log.localize(PrefixKind.JAVAC, descrKey))); |
jjg@1158 | 660 | |
jjg@1158 | 661 | } |
jjg@1158 | 662 | |
jjg@1158 | 663 | private String helpSynopsis(Log log) { |
jjg@1158 | 664 | StringBuilder sb = new StringBuilder(); |
jjg@1158 | 665 | sb.append(text); |
jjg@1158 | 666 | if (argsNameKey == null) { |
jjg@1158 | 667 | if (choices != null) { |
jjg@1158 | 668 | String sep = "{"; |
jjg@1158 | 669 | for (Map.Entry<String,Boolean> e: choices.entrySet()) { |
jjg@1158 | 670 | if (!e.getValue()) { |
jjg@1158 | 671 | sb.append(sep); |
jjg@1158 | 672 | sb.append(e.getKey()); |
jjg@1158 | 673 | sep = ","; |
jjg@1158 | 674 | } |
jjg@1158 | 675 | } |
jjg@1158 | 676 | sb.append("}"); |
jjg@1158 | 677 | } |
jjg@1158 | 678 | } else { |
jjg@1158 | 679 | if (!hasSuffix) |
jjg@1158 | 680 | sb.append(" "); |
jjg@1158 | 681 | sb.append(log.localize(PrefixKind.JAVAC, argsNameKey)); |
jjg@1158 | 682 | |
jjg@1158 | 683 | } |
jjg@1158 | 684 | |
jjg@1158 | 685 | return sb.toString(); |
jjg@1158 | 686 | } |
jjg@1158 | 687 | |
jjg@1158 | 688 | // For -XpkgInfo:value |
jjg@1158 | 689 | public enum PkgInfo { |
jjg@1988 | 690 | /** |
jjg@1988 | 691 | * Always generate package-info.class for every package-info.java file. |
jjg@1988 | 692 | * The file may be empty if there annotations with a RetentionPolicy |
jjg@1988 | 693 | * of CLASS or RUNTIME. This option may be useful in conjunction with |
jjg@1988 | 694 | * build systems (such as Ant) that expect javac to generate at least |
jjg@1988 | 695 | * one .class file for every .java file. |
jjg@1988 | 696 | */ |
jjg@1988 | 697 | ALWAYS, |
jjg@1988 | 698 | /** |
jjg@1988 | 699 | * Generate a package-info.class file if package-info.java contains |
jjg@1988 | 700 | * annotations. The file may be empty if all the annotations have |
jjg@1988 | 701 | * a RetentionPolicy of SOURCE. |
jjg@1988 | 702 | * This value is just for backwards compatibility with earlier behavior. |
jjg@1988 | 703 | * Either of the other two values are to be preferred to using this one. |
jjg@1988 | 704 | */ |
jjg@1988 | 705 | LEGACY, |
jjg@1988 | 706 | /** |
jjg@1988 | 707 | * Generate a package-info.class file if and only if there are annotations |
jjg@1988 | 708 | * in package-info.java to be written into it. |
jjg@1988 | 709 | */ |
jjg@1988 | 710 | NONEMPTY; |
jjg@1988 | 711 | |
jjg@1158 | 712 | public static PkgInfo get(Options options) { |
jjg@1158 | 713 | String v = options.get(XPKGINFO); |
jjg@1158 | 714 | return (v == null |
jjg@1158 | 715 | ? PkgInfo.LEGACY |
jjg@1158 | 716 | : PkgInfo.valueOf(v.toUpperCase())); |
jjg@1158 | 717 | } |
jjg@1158 | 718 | } |
jjg@1158 | 719 | |
jjg@1158 | 720 | private static Map<String,Boolean> getXLintChoices() { |
jjg@1158 | 721 | Map<String,Boolean> choices = new LinkedHashMap<String,Boolean>(); |
jjg@1158 | 722 | choices.put("all", false); |
jjg@1158 | 723 | for (Lint.LintCategory c : Lint.LintCategory.values()) |
jjg@1158 | 724 | choices.put(c.option, c.hidden); |
jjg@1158 | 725 | for (Lint.LintCategory c : Lint.LintCategory.values()) |
jjg@1158 | 726 | choices.put("-" + c.option, c.hidden); |
jjg@1158 | 727 | choices.put("none", false); |
jjg@1158 | 728 | return choices; |
jjg@1158 | 729 | } |
jjg@1158 | 730 | |
jjg@1158 | 731 | static Set<Option> getJavaCompilerOptions() { |
jjg@1158 | 732 | return EnumSet.allOf(Option.class); |
jjg@1158 | 733 | } |
jjg@1158 | 734 | |
jjg@1158 | 735 | public static Set<Option> getJavacFileManagerOptions() { |
jjg@1158 | 736 | return getOptions(EnumSet.of(FILEMANAGER)); |
jjg@1158 | 737 | } |
jjg@1158 | 738 | |
jjg@1158 | 739 | public static Set<Option> getJavacToolOptions() { |
jjg@1158 | 740 | return getOptions(EnumSet.of(BASIC)); |
jjg@1158 | 741 | } |
jjg@1158 | 742 | |
jjg@1158 | 743 | static Set<Option> getOptions(Set<OptionGroup> desired) { |
jjg@1158 | 744 | Set<Option> options = EnumSet.noneOf(Option.class); |
jjg@1158 | 745 | for (Option option : Option.values()) |
jjg@1158 | 746 | if (desired.contains(option.group)) |
jjg@1158 | 747 | options.add(option); |
jjg@1158 | 748 | return Collections.unmodifiableSet(options); |
jjg@1158 | 749 | } |
jjg@1158 | 750 | |
jjg@1158 | 751 | } |