Option.java revision 3528:5538ba41cb97
1/* 2 * Copyright (c) 2006, 2016, 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 */ 25 26package com.sun.tools.javac.main; 27 28import java.io.FileWriter; 29import java.io.PrintWriter; 30import java.nio.file.Files; 31import java.nio.file.Path; 32import java.nio.file.Paths; 33import java.util.Collections; 34import java.util.EnumSet; 35import java.util.LinkedHashMap; 36import java.util.Map; 37import java.util.ServiceLoader; 38import java.util.Set; 39import java.util.TreeSet; 40import java.util.stream.Collectors; 41import java.util.stream.StreamSupport; 42 43import javax.lang.model.SourceVersion; 44 45import com.sun.tools.doclint.DocLint; 46import com.sun.tools.javac.code.Lint; 47import com.sun.tools.javac.code.Lint.LintCategory; 48import com.sun.tools.javac.code.Source; 49import com.sun.tools.javac.code.Type; 50import com.sun.tools.javac.jvm.Profile; 51import com.sun.tools.javac.jvm.Target; 52import com.sun.tools.javac.platform.PlatformProvider; 53import com.sun.tools.javac.processing.JavacProcessingEnvironment; 54import com.sun.tools.javac.util.Log; 55import com.sun.tools.javac.util.Log.PrefixKind; 56import com.sun.tools.javac.util.Log.WriterKind; 57import com.sun.tools.javac.util.Options; 58import com.sun.tools.javac.util.StringUtils; 59 60import static com.sun.tools.javac.main.Option.ChoiceKind.*; 61import static com.sun.tools.javac.main.Option.OptionGroup.*; 62import static com.sun.tools.javac.main.Option.OptionKind.*; 63 64/** 65 * Options for javac. The specific Option to handle a command-line option 66 * is identified by searching the members of this enum in order, looking for 67 * the first {@link #matches match}. The action for an Option is performed 68 * by calling {@link #process process}, and by providing a suitable 69 * {@link OptionHelper} to provide access the compiler state. 70 * 71 * <p><b>This is NOT part of any supported API. 72 * If you write code that depends on this, you do so at your own 73 * risk. This code and its internal interfaces are subject to change 74 * or deletion without notice.</b></p> 75 */ 76public enum Option { 77 G("-g", "opt.g", STANDARD, BASIC), 78 79 G_NONE("-g:none", "opt.g.none", STANDARD, BASIC) { 80 @Override 81 public boolean process(OptionHelper helper, String option) { 82 helper.put("-g:", "none"); 83 return false; 84 } 85 }, 86 87 G_CUSTOM("-g:", "opt.g.lines.vars.source", 88 STANDARD, BASIC, ANYOF, "lines", "vars", "source"), 89 90 XLINT("-Xlint", "opt.Xlint", EXTENDED, BASIC), 91 92 XLINT_CUSTOM("-Xlint:", EXTENDED, BASIC, ANYOF, getXLintChoices()) { 93 private static final String LINT_KEY_FORMAT = " %-19s %s"; 94 @Override 95 void help(Log log, OptionKind kind) { 96 if (this.kind != kind) 97 return; 98 99 log.printRawLines(WriterKind.NOTICE, 100 String.format(HELP_LINE_FORMAT, 101 log.localize(PrefixKind.JAVAC, "opt.Xlint.subopts"), 102 log.localize(PrefixKind.JAVAC, "opt.Xlint.suboptlist"))); 103 log.printRawLines(WriterKind.NOTICE, 104 String.format(LINT_KEY_FORMAT, 105 "all", 106 log.localize(PrefixKind.JAVAC, "opt.Xlint.all"))); 107 for (LintCategory lc : LintCategory.values()) { 108 if (lc.hidden) continue; 109 log.printRawLines(WriterKind.NOTICE, 110 String.format(LINT_KEY_FORMAT, 111 lc.option, 112 log.localize(PrefixKind.JAVAC, 113 "opt.Xlint.desc." + lc.option))); 114 } 115 log.printRawLines(WriterKind.NOTICE, 116 String.format(LINT_KEY_FORMAT, 117 "none", 118 log.localize(PrefixKind.JAVAC, "opt.Xlint.none"))); 119 } 120 }, 121 122 XDOCLINT("-Xdoclint", "opt.Xdoclint", EXTENDED, BASIC), 123 124 XDOCLINT_CUSTOM("-Xdoclint:", "opt.Xdoclint.subopts", "opt.Xdoclint.custom", EXTENDED, BASIC) { 125 @Override 126 public boolean matches(String option) { 127 return DocLint.isValidOption( 128 option.replace(XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX)); 129 } 130 131 @Override 132 public boolean process(OptionHelper helper, String option) { 133 String prev = helper.get(XDOCLINT_CUSTOM); 134 String next = (prev == null) ? option : (prev + " " + option); 135 helper.put(XDOCLINT_CUSTOM.text, next); 136 return false; 137 } 138 }, 139 140 XDOCLINT_PACKAGE("-Xdoclint/package:", "opt.Xdoclint.package.args", "opt.Xdoclint.package.desc", EXTENDED, BASIC) { 141 @Override 142 public boolean matches(String option) { 143 return DocLint.isValidOption( 144 option.replace(XDOCLINT_PACKAGE.text, DocLint.XCHECK_PACKAGE)); 145 } 146 147 @Override 148 public boolean process(OptionHelper helper, String option) { 149 String prev = helper.get(XDOCLINT_PACKAGE); 150 String next = (prev == null) ? option : (prev + " " + option); 151 helper.put(XDOCLINT_PACKAGE.text, next); 152 return false; 153 } 154 }, 155 156 // -nowarn is retained for command-line backward compatibility 157 NOWARN("-nowarn", "opt.nowarn", STANDARD, BASIC) { 158 @Override 159 public boolean process(OptionHelper helper, String option) { 160 helper.put("-Xlint:none", option); 161 return false; 162 } 163 }, 164 165 VERBOSE("-verbose", "opt.verbose", STANDARD, BASIC), 166 167 // -deprecation is retained for command-line backward compatibility 168 DEPRECATION("-deprecation", "opt.deprecation", STANDARD, BASIC) { 169 @Override 170 public boolean process(OptionHelper helper, String option) { 171 helper.put("-Xlint:deprecation", option); 172 return false; 173 } 174 }, 175 176 CLASSPATH("-classpath", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER), 177 178 CP("-cp", "opt.arg.path", "opt.classpath", STANDARD, FILEMANAGER) { 179 @Override 180 public boolean process(OptionHelper helper, String option, String arg) { 181 return super.process(helper, "-classpath", arg); 182 } 183 }, 184 185 SOURCEPATH("-sourcepath", "opt.arg.path", "opt.sourcepath", STANDARD, FILEMANAGER), 186 187 MODULESOURCEPATH("-modulesourcepath", "opt.arg.mspath", "opt.modulesourcepath", STANDARD, FILEMANAGER), 188 189 MODULEPATH("-modulepath", "opt.arg.path", "opt.modulepath", STANDARD, FILEMANAGER), 190 191 MP("-mp", "opt.arg.path", "opt.modulepath", STANDARD, FILEMANAGER) { 192 @Override 193 public boolean process(OptionHelper helper, String option, String arg) { 194 return super.process(helper, "-modulepath", arg); 195 } 196 }, 197 198 UPGRADEMODULEPATH("-upgrademodulepath", "opt.arg.path", "opt.upgrademodulepath", STANDARD, FILEMANAGER), 199 200 SYSTEM("-system", "opt.arg.jdk", "opt.system", STANDARD, FILEMANAGER), 201 202 XPATCH("-Xpatch:", "opt.arg.patch", "opt.patch", EXTENDED, FILEMANAGER), 203 204 BOOTCLASSPATH("-bootclasspath", "opt.arg.path", "opt.bootclasspath", STANDARD, FILEMANAGER) { 205 @Override 206 public boolean process(OptionHelper helper, String option, String arg) { 207 helper.remove("-Xbootclasspath/p:"); 208 helper.remove("-Xbootclasspath/a:"); 209 return super.process(helper, option, arg); 210 } 211 }, 212 213 XBOOTCLASSPATH_PREPEND("-Xbootclasspath/p:", "opt.arg.path", "opt.Xbootclasspath.p", EXTENDED, FILEMANAGER), 214 215 XBOOTCLASSPATH_APPEND("-Xbootclasspath/a:", "opt.arg.path", "opt.Xbootclasspath.a", EXTENDED, FILEMANAGER), 216 217 XBOOTCLASSPATH("-Xbootclasspath:", "opt.arg.path", "opt.bootclasspath", EXTENDED, FILEMANAGER) { 218 @Override 219 public boolean process(OptionHelper helper, String option, String arg) { 220 helper.remove("-Xbootclasspath/p:"); 221 helper.remove("-Xbootclasspath/a:"); 222 return super.process(helper, "-bootclasspath", arg); 223 } 224 }, 225 226 EXTDIRS("-extdirs", "opt.arg.dirs", "opt.extdirs", STANDARD, FILEMANAGER), 227 228 DJAVA_EXT_DIRS("-Djava.ext.dirs=", "opt.arg.dirs", "opt.extdirs", EXTENDED, FILEMANAGER) { 229 @Override 230 public boolean process(OptionHelper helper, String option, String arg) { 231 return super.process(helper, "-extdirs", arg); 232 } 233 }, 234 235 ENDORSEDDIRS("-endorseddirs", "opt.arg.dirs", "opt.endorseddirs", STANDARD, FILEMANAGER), 236 237 DJAVA_ENDORSED_DIRS("-Djava.endorsed.dirs=", "opt.arg.dirs", "opt.endorseddirs", EXTENDED, FILEMANAGER) { 238 @Override 239 public boolean process(OptionHelper helper, String option, String arg) { 240 return super.process(helper, "-endorseddirs", arg); 241 } 242 }, 243 244 PROC("-proc:", "opt.proc.none.only", STANDARD, BASIC, ONEOF, "none", "only"), 245 246 PROCESSOR("-processor", "opt.arg.class.list", "opt.processor", STANDARD, BASIC), 247 248 PROCESSORPATH("-processorpath", "opt.arg.path", "opt.processorpath", STANDARD, FILEMANAGER), 249 250 PROCESSORMODULEPATH("-processormodulepath", "opt.arg.path", "opt.processormodulepath", STANDARD, FILEMANAGER), 251 252 PARAMETERS("-parameters","opt.parameters", STANDARD, BASIC), 253 254 D("-d", "opt.arg.directory", "opt.d", STANDARD, FILEMANAGER), 255 256 S("-s", "opt.arg.directory", "opt.sourceDest", STANDARD, FILEMANAGER), 257 258 H("-h", "opt.arg.directory", "opt.headerDest", STANDARD, FILEMANAGER), 259 260 IMPLICIT("-implicit:", "opt.implicit", STANDARD, BASIC, ONEOF, "none", "class"), 261 262 ENCODING("-encoding", "opt.arg.encoding", "opt.encoding", STANDARD, FILEMANAGER), 263 264 SOURCE("-source", "opt.arg.release", "opt.source", STANDARD, BASIC) { 265 @Override 266 public boolean process(OptionHelper helper, String option, String operand) { 267 Source source = Source.lookup(operand); 268 if (source == null) { 269 helper.error("err.invalid.source", operand); 270 return true; 271 } 272 return super.process(helper, option, operand); 273 } 274 }, 275 276 TARGET("-target", "opt.arg.release", "opt.target", STANDARD, BASIC) { 277 @Override 278 public boolean process(OptionHelper helper, String option, String operand) { 279 Target target = Target.lookup(operand); 280 if (target == null) { 281 helper.error("err.invalid.target", operand); 282 return true; 283 } 284 return super.process(helper, option, operand); 285 } 286 }, 287 288 RELEASE("-release", "opt.arg.release", "opt.release", STANDARD, BASIC) { 289 @Override 290 void help(Log log, OptionKind kind) { 291 if (this.kind != kind) 292 return; 293 294 Iterable<PlatformProvider> providers = 295 ServiceLoader.load(PlatformProvider.class, Arguments.class.getClassLoader()); 296 Set<String> platforms = StreamSupport.stream(providers.spliterator(), false) 297 .flatMap(provider -> StreamSupport.stream(provider.getSupportedPlatformNames() 298 .spliterator(), 299 false)) 300 .collect(Collectors.toCollection(TreeSet :: new)); 301 302 StringBuilder targets = new StringBuilder(); 303 String delim = ""; 304 for (String platform : platforms) { 305 targets.append(delim); 306 targets.append(platform); 307 delim = ", "; 308 } 309 310 log.printRawLines(WriterKind.NOTICE, 311 String.format(HELP_LINE_FORMAT, 312 super.helpSynopsis(log), 313 log.localize(PrefixKind.JAVAC, descrKey, targets.toString()))); 314 } 315 }, 316 317 PROFILE("-profile", "opt.arg.profile", "opt.profile", STANDARD, BASIC) { 318 @Override 319 public boolean process(OptionHelper helper, String option, String operand) { 320 Profile profile = Profile.lookup(operand); 321 if (profile == null) { 322 helper.error("err.invalid.profile", operand); 323 return true; 324 } 325 return super.process(helper, option, operand); 326 } 327 }, 328 329 VERSION("-version", "opt.version", STANDARD, INFO) { 330 @Override 331 public boolean process(OptionHelper helper, String option) { 332 Log log = helper.getLog(); 333 String ownName = helper.getOwnName(); 334 log.printLines(PrefixKind.JAVAC, "version", ownName, JavaCompiler.version()); 335 return super.process(helper, option); 336 } 337 }, 338 339 FULLVERSION("-fullversion", null, HIDDEN, INFO) { 340 @Override 341 public boolean process(OptionHelper helper, String option) { 342 Log log = helper.getLog(); 343 String ownName = helper.getOwnName(); 344 log.printLines(PrefixKind.JAVAC, "fullVersion", ownName, JavaCompiler.fullVersion()); 345 return super.process(helper, option); 346 } 347 }, 348 349 DIAGS("-XDdiags=", null, HIDDEN, INFO) { 350 @Override 351 public boolean process(OptionHelper helper, String option) { 352 option = option.substring(option.indexOf('=') + 1); 353 String diagsOption = option.contains("%") ? 354 "-XDdiagsFormat=" : 355 "-XDdiags="; 356 diagsOption += option; 357 if (XD.matches(diagsOption)) 358 return XD.process(helper, diagsOption); 359 else 360 return false; 361 } 362 }, 363 364 HELP("-help", "opt.help", STANDARD, INFO) { 365 @Override 366 public boolean process(OptionHelper helper, String option) { 367 Log log = helper.getLog(); 368 String ownName = helper.getOwnName(); 369 log.printLines(PrefixKind.JAVAC, "msg.usage.header", ownName); 370 for (Option o: getJavaCompilerOptions()) { 371 o.help(log, OptionKind.STANDARD); 372 } 373 log.printNewline(); 374 return super.process(helper, option); 375 } 376 }, 377 378 A("-A", "opt.arg.key.equals.value", "opt.A", STANDARD, BASIC, true) { 379 @Override 380 public boolean matches(String arg) { 381 return arg.startsWith("-A"); 382 } 383 384 @Override 385 public boolean hasArg() { 386 return false; 387 } 388 // Mapping for processor options created in 389 // JavacProcessingEnvironment 390 @Override 391 public boolean process(OptionHelper helper, String option) { 392 int argLength = option.length(); 393 if (argLength == 2) { 394 helper.error("err.empty.A.argument"); 395 return true; 396 } 397 int sepIndex = option.indexOf('='); 398 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) ); 399 if (!JavacProcessingEnvironment.isValidOptionName(key)) { 400 helper.error("err.invalid.A.key", option); 401 return true; 402 } 403 return process(helper, option, option); 404 } 405 }, 406 407 X("-X", "opt.X", STANDARD, INFO) { 408 @Override 409 public boolean process(OptionHelper helper, String option) { 410 Log log = helper.getLog(); 411 for (Option o: getJavaCompilerOptions()) { 412 o.help(log, OptionKind.EXTENDED); 413 } 414 log.printNewline(); 415 log.printLines(PrefixKind.JAVAC, "msg.usage.nonstandard.footer"); 416 return super.process(helper, option); 417 } 418 }, 419 420 // This option exists only for the purpose of documenting itself. 421 // It's actually implemented by the launcher. 422 J("-J", "opt.arg.flag", "opt.J", STANDARD, INFO, true) { 423 @Override 424 public boolean process(OptionHelper helper, String option) { 425 throw new AssertionError 426 ("the -J flag should be caught by the launcher."); 427 } 428 }, 429 430 MOREINFO("-moreinfo", null, HIDDEN, BASIC) { 431 @Override 432 public boolean process(OptionHelper helper, String option) { 433 Type.moreInfo = true; 434 return super.process(helper, option); 435 } 436 }, 437 438 // treat warnings as errors 439 WERROR("-Werror", "opt.Werror", STANDARD, BASIC), 440 441 // prompt after each error 442 // new Option("-prompt", "opt.prompt"), 443 PROMPT("-prompt", null, HIDDEN, BASIC), 444 445 // dump stack on error 446 DOE("-doe", null, HIDDEN, BASIC), 447 448 // output source after type erasure 449 PRINTSOURCE("-printsource", null, HIDDEN, BASIC), 450 451 // display warnings for generic unchecked operations 452 WARNUNCHECKED("-warnunchecked", null, HIDDEN, BASIC) { 453 @Override 454 public boolean process(OptionHelper helper, String option) { 455 helper.put("-Xlint:unchecked", option); 456 return false; 457 } 458 }, 459 460 XMAXERRS("-Xmaxerrs", "opt.arg.number", "opt.maxerrs", EXTENDED, BASIC), 461 462 XMAXWARNS("-Xmaxwarns", "opt.arg.number", "opt.maxwarns", EXTENDED, BASIC), 463 464 XSTDOUT("-Xstdout", "opt.arg.file", "opt.Xstdout", EXTENDED, INFO) { 465 @Override 466 public boolean process(OptionHelper helper, String option, String arg) { 467 try { 468 Log log = helper.getLog(); 469 log.setWriters(new PrintWriter(new FileWriter(arg), true)); 470 } catch (java.io.IOException e) { 471 helper.error("err.error.writing.file", arg, e); 472 return true; 473 } 474 return super.process(helper, option, arg); 475 } 476 }, 477 478 XPRINT("-Xprint", "opt.print", EXTENDED, BASIC), 479 480 XPRINTROUNDS("-XprintRounds", "opt.printRounds", EXTENDED, BASIC), 481 482 XPRINTPROCESSORINFO("-XprintProcessorInfo", "opt.printProcessorInfo", EXTENDED, BASIC), 483 484 XPREFER("-Xprefer:", "opt.prefer", EXTENDED, BASIC, ONEOF, "source", "newer"), 485 486 XXUSERPATHSFIRST("-XXuserPathsFirst", "opt.userpathsfirst", HIDDEN, BASIC), 487 488 // see enum PkgInfo 489 XPKGINFO("-Xpkginfo:", "opt.pkginfo", EXTENDED, BASIC, ONEOF, "always", "legacy", "nonempty"), 490 491 /* -O is a no-op, accepted for backward compatibility. */ 492 O("-O", null, HIDDEN, BASIC), 493 494 /* -Xjcov produces tables to support the code coverage tool jcov. */ 495 XJCOV("-Xjcov", null, HIDDEN, BASIC), 496 497 PLUGIN("-Xplugin:", "opt.arg.plugin", "opt.plugin", EXTENDED, BASIC) { 498 @Override 499 public boolean process(OptionHelper helper, String option) { 500 String p = option.substring(option.indexOf(':') + 1).trim(); 501 String prev = helper.get(PLUGIN); 502 helper.put(PLUGIN.text, (prev == null) ? p : prev + '\0' + p); 503 return false; 504 } 505 }, 506 507 XDIAGS("-Xdiags:", "opt.diags", EXTENDED, BASIC, ONEOF, "compact", "verbose"), 508 509 /* This is a back door to the compiler's option table. 510 * -XDx=y sets the option x to the value y. 511 * -XDx sets the option x to the value x. 512 */ 513 XD("-XD", null, HIDDEN, BASIC) { 514 @Override 515 public boolean matches(String s) { 516 return s.startsWith(text); 517 } 518 @Override 519 public boolean process(OptionHelper helper, String option) { 520 return process(helper, option, option.substring(text.length())); 521 } 522 523 @Override 524 public boolean process(OptionHelper helper, String option, String arg) { 525 int eq = arg.indexOf('='); 526 String key = (eq < 0) ? arg : arg.substring(0, eq); 527 String value = (eq < 0) ? arg : arg.substring(eq+1); 528 helper.put(key, value); 529 return false; 530 } 531 }, 532 533 XDEBUG("-Xdebug:", null, HIDDEN, BASIC) { 534 @Override 535 public boolean process(OptionHelper helper, String option) { 536 String p = option.substring(option.indexOf(':') + 1).trim(); 537 String[] subOptions = p.split(";"); 538 for (String subOption : subOptions) { 539 subOption = "debug." + subOption.trim(); 540 XD.process(helper, subOption, subOption); 541 } 542 return false; 543 } 544 }, 545 546 XADDEXPORTS("-XaddExports:", "opt.arg.addExports", "opt.addExports", EXTENDED, BASIC) { 547 @Override 548 public boolean process(OptionHelper helper, String option) { 549 String p = option.substring(option.indexOf(':') + 1).trim(); 550 String prev = helper.get(XADDEXPORTS); 551 helper.put(XADDEXPORTS.text, (prev == null) ? p : prev + '\0' + p); 552 return false; 553 } 554 }, 555 556 XADDREADS("-XaddReads:", "opt.arg.addReads", "opt.addReads", EXTENDED, BASIC) { 557 @Override 558 public boolean process(OptionHelper helper, String option) { 559 String p = option.substring(option.indexOf(':') + 1).trim(); 560 String prev = helper.get(XADDREADS); 561 helper.put(XADDREADS.text, (prev == null) ? p : prev + '\0' + p); 562 return false; 563 } 564 }, 565 566 XMODULE("-Xmodule:", "opt.arg.module", "opt.module", EXTENDED, BASIC) { 567 @Override 568 public boolean process(OptionHelper helper, String option) { 569 String prev = helper.get(XMODULE); 570 if (prev != null) { 571 helper.error("err.option.too.many", XMODULE.text); 572 } 573 String p = option.substring(option.indexOf(':') + 1); 574 helper.put(XMODULE.text, p); 575 return false; 576 } 577 }, 578 579 M("-m", "opt.arg.m", "opt.m", STANDARD, BASIC), 580 581 ADDMODS("-addmods", "opt.arg.addmods", "opt.addmods", STANDARD, BASIC), 582 LIMITMODS("-limitmods", "opt.arg.limitmods", "opt.limitmods", STANDARD, BASIC), 583 584 // This option exists only for the purpose of documenting itself. 585 // It's actually implemented by the CommandLine class. 586 AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO, true) { 587 @Override 588 public boolean process(OptionHelper helper, String option) { 589 throw new AssertionError("the @ flag should be caught by CommandLine."); 590 } 591 }, 592 593 // Standalone positional argument: source file or type name. 594 SOURCEFILE("sourcefile", null, HIDDEN, INFO) { 595 @Override 596 public boolean matches(String s) { 597 if (s.endsWith(".java")) // Java source file 598 return true; 599 int sep = s.indexOf('/'); 600 if (sep != -1) { 601 return SourceVersion.isName(s.substring(0, sep)) 602 && SourceVersion.isName(s.substring(sep + 1)); 603 } else { 604 return SourceVersion.isName(s); // Legal type name 605 } 606 } 607 @Override 608 public boolean process(OptionHelper helper, String option) { 609 if (option.endsWith(".java") ) { 610 Path p = Paths.get(option); 611 if (!Files.exists(p)) { 612 helper.error("err.file.not.found", p); 613 return true; 614 } 615 if (!Files.isRegularFile(p)) { 616 helper.error("err.file.not.file", p); 617 return true; 618 } 619 helper.addFile(p); 620 } else { 621 helper.addClassName(option); 622 } 623 return false; 624 } 625 }, 626 627 MULTIRELEASE("-multi-release", "opt.arg.multi-release", "opt.multi-release", HIDDEN, FILEMANAGER); 628 629 /** The kind of an Option. This is used by the -help and -X options. */ 630 public enum OptionKind { 631 /** A standard option, documented by -help. */ 632 STANDARD, 633 /** An extended option, documented by -X. */ 634 EXTENDED, 635 /** A hidden option, not documented. */ 636 HIDDEN, 637 } 638 639 /** The group for an Option. This determines the situations in which the 640 * option is applicable. */ 641 enum OptionGroup { 642 /** A basic option, available for use on the command line or via the 643 * Compiler API. */ 644 BASIC, 645 /** An option for javac's standard JavaFileManager. Other file managers 646 * may or may not support these options. */ 647 FILEMANAGER, 648 /** A command-line option that requests information, such as -help. */ 649 INFO, 650 /** A command-line "option" representing a file or class name. */ 651 OPERAND 652 } 653 654 /** The kind of choice for "choice" options. */ 655 enum ChoiceKind { 656 /** The expected value is exactly one of the set of choices. */ 657 ONEOF, 658 /** The expected value is one of more of the set of choices. */ 659 ANYOF 660 } 661 662 public final String text; 663 664 final OptionKind kind; 665 666 final OptionGroup group; 667 668 /** Documentation key for arguments. 669 */ 670 final String argsNameKey; 671 672 /** Documentation key for description. 673 */ 674 final String descrKey; 675 676 /** Suffix option (-foo=bar or -foo:bar) 677 */ 678 final boolean hasSuffix; 679 680 /** The kind of choices for this option, if any. 681 */ 682 final ChoiceKind choiceKind; 683 684 /** The choices for this option, if any, and whether or not the choices 685 * are hidden 686 */ 687 final Map<String,Boolean> choices; 688 689 690 Option(String text, String descrKey, 691 OptionKind kind, OptionGroup group) { 692 this(text, null, descrKey, kind, group, null, null, false); 693 } 694 695 Option(String text, String argsNameKey, String descrKey, 696 OptionKind kind, OptionGroup group) { 697 this(text, argsNameKey, descrKey, kind, group, null, null, false); 698 } 699 700 Option(String text, String argsNameKey, String descrKey, 701 OptionKind kind, OptionGroup group, boolean doHasSuffix) { 702 this(text, argsNameKey, descrKey, kind, group, null, null, doHasSuffix); 703 } 704 705 Option(String text, OptionKind kind, OptionGroup group, 706 ChoiceKind choiceKind, Map<String,Boolean> choices) { 707 this(text, null, null, kind, group, choiceKind, choices, false); 708 } 709 710 Option(String text, String descrKey, 711 OptionKind kind, OptionGroup group, 712 ChoiceKind choiceKind, String... choices) { 713 this(text, null, descrKey, kind, group, choiceKind, 714 createChoices(choices), false); 715 } 716 // where 717 private static Map<String,Boolean> createChoices(String... choices) { 718 Map<String,Boolean> map = new LinkedHashMap<>(); 719 for (String c: choices) 720 map.put(c, false); 721 return map; 722 } 723 724 private Option(String text, String argsNameKey, String descrKey, 725 OptionKind kind, OptionGroup group, 726 ChoiceKind choiceKind, Map<String,Boolean> choices, 727 boolean doHasSuffix) { 728 this.text = text; 729 this.argsNameKey = argsNameKey; 730 this.descrKey = descrKey; 731 this.kind = kind; 732 this.group = group; 733 this.choiceKind = choiceKind; 734 this.choices = choices; 735 char lastChar = text.charAt(text.length()-1); 736 this.hasSuffix = doHasSuffix || lastChar == ':' || lastChar == '='; 737 } 738 739 public String getText() { 740 return text; 741 } 742 743 public OptionKind getKind() { 744 return kind; 745 } 746 747 public boolean hasArg() { 748 return argsNameKey != null && !hasSuffix; 749 } 750 751 public boolean matches(String option) { 752 if (!hasSuffix) 753 return option.equals(text); 754 755 if (!option.startsWith(text)) 756 return false; 757 758 if (choices != null) { 759 String arg = option.substring(text.length()); 760 if (choiceKind == ChoiceKind.ONEOF) 761 return choices.keySet().contains(arg); 762 else { 763 for (String a: arg.split(",+")) { 764 if (!choices.keySet().contains(a)) 765 return false; 766 } 767 } 768 } 769 770 return true; 771 } 772 773 public boolean process(OptionHelper helper, String option, String arg) { 774 if (choices != null) { 775 if (choiceKind == ChoiceKind.ONEOF) { 776 // some clients like to see just one of option+choice set 777 for (String s: choices.keySet()) 778 helper.remove(option + s); 779 String opt = option + arg; 780 helper.put(opt, opt); 781 // some clients like to see option (without trailing ":") 782 // set to arg 783 String nm = option.substring(0, option.length() - 1); 784 helper.put(nm, arg); 785 } else { 786 // set option+word for each word in arg 787 for (String a: arg.split(",+")) { 788 String opt = option + a; 789 helper.put(opt, opt); 790 } 791 } 792 } 793 helper.put(option, arg); 794 if (group == OptionGroup.FILEMANAGER) 795 helper.handleFileManagerOption(this, arg); 796 return false; 797 } 798 799 public boolean process(OptionHelper helper, String option) { 800 if (hasSuffix) 801 return process(helper, text, option.substring(text.length())); 802 else 803 return process(helper, option, option); 804 } 805 806 private static final String HELP_LINE_FORMAT = " %-26s %s"; 807 808 void help(Log log, OptionKind kind) { 809 if (this.kind != kind) 810 return; 811 812 log.printRawLines(WriterKind.NOTICE, 813 String.format(HELP_LINE_FORMAT, 814 helpSynopsis(log), 815 log.localize(PrefixKind.JAVAC, descrKey))); 816 817 } 818 819 private String helpSynopsis(Log log) { 820 StringBuilder sb = new StringBuilder(); 821 sb.append(text); 822 if (argsNameKey == null) { 823 if (choices != null) { 824 String sep = "{"; 825 for (Map.Entry<String,Boolean> e: choices.entrySet()) { 826 if (!e.getValue()) { 827 sb.append(sep); 828 sb.append(e.getKey()); 829 sep = ","; 830 } 831 } 832 sb.append("}"); 833 } 834 } else { 835 if (!hasSuffix) 836 sb.append(" "); 837 sb.append(log.localize(PrefixKind.JAVAC, argsNameKey)); 838 839 } 840 841 return sb.toString(); 842 } 843 844 // For -XpkgInfo:value 845 public enum PkgInfo { 846 /** 847 * Always generate package-info.class for every package-info.java file. 848 * The file may be empty if there annotations with a RetentionPolicy 849 * of CLASS or RUNTIME. This option may be useful in conjunction with 850 * build systems (such as Ant) that expect javac to generate at least 851 * one .class file for every .java file. 852 */ 853 ALWAYS, 854 /** 855 * Generate a package-info.class file if package-info.java contains 856 * annotations. The file may be empty if all the annotations have 857 * a RetentionPolicy of SOURCE. 858 * This value is just for backwards compatibility with earlier behavior. 859 * Either of the other two values are to be preferred to using this one. 860 */ 861 LEGACY, 862 /** 863 * Generate a package-info.class file if and only if there are annotations 864 * in package-info.java to be written into it. 865 */ 866 NONEMPTY; 867 868 public static PkgInfo get(Options options) { 869 String v = options.get(XPKGINFO); 870 return (v == null 871 ? PkgInfo.LEGACY 872 : PkgInfo.valueOf(StringUtils.toUpperCase(v))); 873 } 874 } 875 876 private static Map<String,Boolean> getXLintChoices() { 877 Map<String,Boolean> choices = new LinkedHashMap<>(); 878 choices.put("all", false); 879 for (Lint.LintCategory c : Lint.LintCategory.values()) 880 choices.put(c.option, c.hidden); 881 for (Lint.LintCategory c : Lint.LintCategory.values()) 882 choices.put("-" + c.option, c.hidden); 883 choices.put("none", false); 884 return choices; 885 } 886 887 static Set<Option> getJavaCompilerOptions() { 888 return EnumSet.allOf(Option.class); 889 } 890 891 public static Set<Option> getJavacFileManagerOptions() { 892 return getOptions(EnumSet.of(FILEMANAGER)); 893 } 894 895 public static Set<Option> getJavacToolOptions() { 896 return getOptions(EnumSet.of(BASIC)); 897 } 898 899 static Set<Option> getOptions(Set<OptionGroup> desired) { 900 Set<Option> options = EnumSet.noneOf(Option.class); 901 for (Option option : Option.values()) 902 if (desired.contains(option.group)) 903 options.add(option); 904 return Collections.unmodifiableSet(options); 905 } 906 907} 908