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