Arguments.java revision 4079:bef1cba2d0d9
1/* 2 * Copyright (c) 1999, 2017, 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.IOException; 29import java.nio.file.Files; 30import java.nio.file.Path; 31import java.nio.file.Paths; 32import java.util.Arrays; 33import java.util.Collection; 34import java.util.Collections; 35import java.util.EnumSet; 36import java.util.HashSet; 37import java.util.Iterator; 38import java.util.LinkedHashMap; 39import java.util.LinkedHashSet; 40import java.util.Map; 41import java.util.Set; 42import java.util.function.Predicate; 43import java.util.regex.Matcher; 44import java.util.regex.Pattern; 45import java.util.stream.Stream; 46 47import javax.lang.model.SourceVersion; 48import javax.tools.JavaFileManager; 49import javax.tools.JavaFileManager.Location; 50import javax.tools.JavaFileObject; 51import javax.tools.JavaFileObject.Kind; 52import javax.tools.StandardJavaFileManager; 53import javax.tools.StandardLocation; 54 55import com.sun.tools.doclint.DocLint; 56import com.sun.tools.javac.code.Lint.LintCategory; 57import com.sun.tools.javac.code.Source; 58import com.sun.tools.javac.file.BaseFileManager; 59import com.sun.tools.javac.file.JavacFileManager; 60import com.sun.tools.javac.jvm.Profile; 61import com.sun.tools.javac.jvm.Target; 62import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; 63import com.sun.tools.javac.platform.PlatformDescription; 64import com.sun.tools.javac.platform.PlatformUtils; 65import com.sun.tools.javac.resources.CompilerProperties.Errors; 66import com.sun.tools.javac.resources.CompilerProperties.Warnings; 67import com.sun.tools.javac.util.Context; 68import com.sun.tools.javac.util.JCDiagnostic; 69import com.sun.tools.javac.util.List; 70import com.sun.tools.javac.util.ListBuffer; 71import com.sun.tools.javac.util.Log; 72import com.sun.tools.javac.util.Log.PrefixKind; 73import com.sun.tools.javac.util.Log.WriterKind; 74import com.sun.tools.javac.util.Options; 75import com.sun.tools.javac.util.PropagatedException; 76 77/** 78 * Shared option and argument handling for command line and API usage of javac. 79 */ 80public class Arguments { 81 82 /** 83 * The context key for the arguments. 84 */ 85 public static final Context.Key<Arguments> argsKey = new Context.Key<>(); 86 87 private String ownName; 88 private Set<String> classNames; 89 private Set<Path> files; 90 private Map<Option, String> deferredFileManagerOptions; 91 private Set<JavaFileObject> fileObjects; 92 private boolean emptyAllowed; 93 private final Options options; 94 95 private JavaFileManager fileManager; 96 private final Log log; 97 private final Context context; 98 99 private enum ErrorMode { ILLEGAL_ARGUMENT, ILLEGAL_STATE, LOG }; 100 private ErrorMode errorMode; 101 private boolean errors; 102 103 /** 104 * Gets the Arguments instance for this context. 105 * 106 * @param context the content 107 * @return the Arguments instance for this context. 108 */ 109 public static Arguments instance(Context context) { 110 Arguments instance = context.get(argsKey); 111 if (instance == null) { 112 instance = new Arguments(context); 113 } 114 return instance; 115 } 116 117 protected Arguments(Context context) { 118 context.put(argsKey, this); 119 options = Options.instance(context); 120 log = Log.instance(context); 121 this.context = context; 122 123 // Ideally, we could init this here and update/configure it as 124 // needed, but right now, initializing a file manager triggers 125 // initialization of other items in the context, such as Lint 126 // and FSInfo, which should not be initialized until after 127 // processArgs 128 // fileManager = context.get(JavaFileManager.class); 129 } 130 131 private final OptionHelper cmdLineHelper = new OptionHelper() { 132 @Override 133 public String get(Option option) { 134 return options.get(option); 135 } 136 137 @Override 138 public void put(String name, String value) { 139 options.put(name, value); 140 } 141 142 @Override 143 public void remove(String name) { 144 options.remove(name); 145 } 146 147 @Override 148 public boolean handleFileManagerOption(Option option, String value) { 149 options.put(option, value); 150 deferredFileManagerOptions.put(option, value); 151 return true; 152 } 153 154 @Override 155 public Log getLog() { 156 return log; 157 } 158 159 @Override 160 public String getOwnName() { 161 return ownName; 162 } 163 164 @Override 165 public void addFile(Path p) { 166 files.add(p); 167 } 168 169 @Override 170 public void addClassName(String s) { 171 classNames.add(s); 172 } 173 174 }; 175 176 /** 177 * Initializes this Args instance with a set of command line args. 178 * The args will be processed in conjunction with the full set of 179 * command line options, including -help, -version etc. 180 * The args may also contain class names and filenames. 181 * Any errors during this call, and later during validate, will be reported 182 * to the log. 183 * @param ownName the name of this tool; used to prefix messages 184 * @param args the args to be processed 185 */ 186 public void init(String ownName, String... args) { 187 this.ownName = ownName; 188 errorMode = ErrorMode.LOG; 189 files = new LinkedHashSet<>(); 190 deferredFileManagerOptions = new LinkedHashMap<>(); 191 fileObjects = null; 192 classNames = new LinkedHashSet<>(); 193 processArgs(List.from(args), Option.getJavaCompilerOptions(), cmdLineHelper, true, false); 194 if (errors) { 195 log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); 196 } 197 } 198 199 private final OptionHelper apiHelper = new GrumpyHelper(null) { 200 @Override 201 public String get(Option option) { 202 return options.get(option); 203 } 204 205 @Override 206 public void put(String name, String value) { 207 options.put(name, value); 208 } 209 210 @Override 211 public void remove(String name) { 212 options.remove(name); 213 } 214 215 @Override 216 public Log getLog() { 217 return Arguments.this.log; 218 } 219 }; 220 221 /** 222 * Initializes this Args instance with the parameters for a JavacTask. 223 * The options will be processed in conjunction with the restricted set 224 * of tool options, which does not include -help, -version, etc, 225 * nor does it include classes and filenames, which should be specified 226 * separately. 227 * File manager options are handled directly by the file manager. 228 * Any errors found while processing individual args will be reported 229 * via IllegalArgumentException. 230 * Any subsequent errors during validate will be reported via IllegalStateException. 231 * @param ownName the name of this tool; used to prefix messages 232 * @param options the options to be processed 233 * @param classNames the classes to be subject to annotation processing 234 * @param files the files to be compiled 235 */ 236 public void init(String ownName, 237 Iterable<String> options, 238 Iterable<String> classNames, 239 Iterable<? extends JavaFileObject> files) { 240 this.ownName = ownName; 241 this.classNames = toSet(classNames); 242 this.fileObjects = toSet(files); 243 this.files = null; 244 errorMode = ErrorMode.ILLEGAL_ARGUMENT; 245 if (options != null) { 246 processArgs(toList(options), Option.getJavacToolOptions(), apiHelper, false, true); 247 } 248 errorMode = ErrorMode.ILLEGAL_STATE; 249 } 250 251 /** 252 * Minimal initialization for tools, like javadoc, 253 * to be able to process javac options for themselves, 254 * and then call validate. 255 * @param ownName the name of this tool; used to prefix messages 256 */ 257 public void init(String ownName) { 258 this.ownName = ownName; 259 errorMode = ErrorMode.LOG; 260 } 261 262 /** 263 * Gets the files to be compiled. 264 * @return the files to be compiled 265 */ 266 public Set<JavaFileObject> getFileObjects() { 267 if (fileObjects == null) { 268 fileObjects = new LinkedHashSet<>(); 269 } 270 if (files != null) { 271 JavacFileManager jfm = (JavacFileManager) getFileManager(); 272 for (JavaFileObject fo: jfm.getJavaFileObjectsFromPaths(files)) 273 fileObjects.add(fo); 274 } 275 return fileObjects; 276 } 277 278 /** 279 * Gets the classes to be subject to annotation processing. 280 * @return the classes to be subject to annotation processing 281 */ 282 public Set<String> getClassNames() { 283 return classNames; 284 } 285 286 /** 287 * Handles the {@code --release} option. 288 * 289 * @param additionalOptions a predicate to handle additional options implied by the 290 * {@code --release} option. The predicate should return true if all the additional 291 * options were processed successfully. 292 * @return true if successful, false otherwise 293 */ 294 public boolean handleReleaseOptions(Predicate<Iterable<String>> additionalOptions) { 295 String platformString = options.get(Option.RELEASE); 296 297 checkOptionAllowed(platformString == null, 298 option -> error("err.release.bootclasspath.conflict", option.getPrimaryName()), 299 Option.BOOT_CLASS_PATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND, 300 Option.XBOOTCLASSPATH_PREPEND, 301 Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS, 302 Option.EXTDIRS, Option.DJAVA_EXT_DIRS, 303 Option.SOURCE, Option.TARGET); 304 305 if (platformString != null) { 306 PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString); 307 308 if (platformDescription == null) { 309 error("err.unsupported.release.version", platformString); 310 return false; 311 } 312 313 options.put(Option.SOURCE, platformDescription.getSourceVersion()); 314 options.put(Option.TARGET, platformDescription.getTargetVersion()); 315 316 context.put(PlatformDescription.class, platformDescription); 317 318 if (!additionalOptions.test(platformDescription.getAdditionalOptions())) 319 return false; 320 321 Collection<Path> platformCP = platformDescription.getPlatformPath(); 322 323 if (platformCP != null) { 324 JavaFileManager fm = getFileManager(); 325 326 if (!(fm instanceof StandardJavaFileManager)) { 327 error("err.release.not.standard.file.manager"); 328 return false; 329 } 330 331 try { 332 StandardJavaFileManager sfm = (StandardJavaFileManager) fm; 333 334 sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP); 335 } catch (IOException ex) { 336 log.printLines(PrefixKind.JAVAC, "msg.io"); 337 ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); 338 return false; 339 } 340 } 341 } 342 343 return true; 344 } 345 346 /** 347 * Processes strings containing options and operands. 348 * @param args the strings to be processed 349 * @param allowableOpts the set of option declarations that are applicable 350 * @param helper a help for use by Option.process 351 * @param allowOperands whether or not to check for files and classes 352 * @param checkFileManager whether or not to check if the file manager can handle 353 * options which are not recognized by any of allowableOpts 354 * @return true if all the strings were successfully processed; false otherwise 355 * @throws IllegalArgumentException if a problem occurs and errorMode is set to 356 * ILLEGAL_ARGUMENT 357 */ 358 private boolean processArgs(Iterable<String> args, 359 Set<Option> allowableOpts, OptionHelper helper, 360 boolean allowOperands, boolean checkFileManager) { 361 if (!doProcessArgs(args, allowableOpts, helper, allowOperands, checkFileManager)) 362 return false; 363 364 if (!handleReleaseOptions(extra -> doProcessArgs(extra, allowableOpts, helper, allowOperands, checkFileManager))) 365 return false; 366 367 options.notifyListeners(); 368 369 return true; 370 } 371 372 private boolean doProcessArgs(Iterable<String> args, 373 Set<Option> allowableOpts, OptionHelper helper, 374 boolean allowOperands, boolean checkFileManager) { 375 JavaFileManager fm = checkFileManager ? getFileManager() : null; 376 Iterator<String> argIter = args.iterator(); 377 while (argIter.hasNext()) { 378 String arg = argIter.next(); 379 if (arg.isEmpty()) { 380 error("err.invalid.flag", arg); 381 return false; 382 } 383 384 Option option = null; 385 386 // first, check the provided set of javac options 387 if (arg.startsWith("-")) { 388 option = Option.lookup(arg, allowableOpts); 389 } else if (allowOperands && Option.SOURCEFILE.matches(arg)) { 390 option = Option.SOURCEFILE; 391 } 392 393 if (option != null) { 394 try { 395 option.handleOption(helper, arg, argIter); 396 } catch (Option.InvalidValueException e) { 397 error(e); 398 return false; 399 } 400 continue; 401 } 402 403 // check file manager option 404 if (fm != null && fm.handleOption(arg, argIter)) { 405 continue; 406 } 407 408 // none of the above 409 error("err.invalid.flag", arg); 410 return false; 411 } 412 413 return true; 414 } 415 416 /** 417 * Validates the overall consistency of the options and operands 418 * processed by processOptions. 419 * @return true if all args are successfully validated; false otherwise. 420 * @throws IllegalStateException if a problem is found and errorMode is set to 421 * ILLEGAL_STATE 422 */ 423 public boolean validate() { 424 JavaFileManager fm = getFileManager(); 425 if (options.isSet(Option.MODULE)) { 426 if (!fm.hasLocation(StandardLocation.CLASS_OUTPUT)) { 427 log.error(Errors.OutputDirMustBeSpecifiedWithDashMOption); 428 } else if (!fm.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) { 429 log.error(Errors.ModulesourcepathMustBeSpecifiedWithDashMOption); 430 } else { 431 java.util.List<String> modules = Arrays.asList(options.get(Option.MODULE).split(",")); 432 try { 433 for (String module : modules) { 434 Location sourceLoc = fm.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, module); 435 if (sourceLoc == null) { 436 log.error(Errors.ModuleNotFoundInModuleSourcePath(module)); 437 } else { 438 Location classLoc = fm.getLocationForModule(StandardLocation.CLASS_OUTPUT, module); 439 440 for (JavaFileObject file : fm.list(sourceLoc, "", EnumSet.of(JavaFileObject.Kind.SOURCE), true)) { 441 String className = fm.inferBinaryName(sourceLoc, file); 442 JavaFileObject classFile = fm.getJavaFileForInput(classLoc, className, Kind.CLASS); 443 444 if (classFile == null || classFile.getLastModified() < file.getLastModified()) { 445 if (fileObjects == null) 446 fileObjects = new HashSet<>(); 447 fileObjects.add(file); 448 } 449 } 450 } 451 } 452 } catch (IOException ex) { 453 log.printLines(PrefixKind.JAVAC, "msg.io"); 454 ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); 455 return false; 456 } 457 } 458 } 459 460 if (isEmpty()) { 461 // It is allowed to compile nothing if just asking for help or version info. 462 // But also note that none of these options are supported in API mode. 463 if (options.isSet(Option.HELP) 464 || options.isSet(Option.X) 465 || options.isSet(Option.VERSION) 466 || options.isSet(Option.FULLVERSION) 467 || options.isSet(Option.MODULE)) { 468 return true; 469 } 470 471 if (!emptyAllowed) { 472 if (!errors) { 473 if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { 474 error("err.no.source.files.classes"); 475 } else { 476 error("err.no.source.files"); 477 } 478 } 479 return false; 480 } 481 } 482 483 if (!checkDirectory(Option.D)) { 484 return false; 485 } 486 if (!checkDirectory(Option.S)) { 487 return false; 488 } 489 if (!checkDirectory(Option.H)) { 490 return false; 491 } 492 493 // The following checks are to help avoid accidental confusion between 494 // directories of modules and exploded module directories. 495 if (fm instanceof StandardJavaFileManager) { 496 StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager; 497 if (sfm.hasLocation(StandardLocation.CLASS_OUTPUT)) { 498 Path outDir = sfm.getLocationAsPaths(StandardLocation.CLASS_OUTPUT).iterator().next(); 499 if (sfm.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) { 500 // multi-module mode 501 if (Files.exists(outDir.resolve("module-info.class"))) { 502 log.error(Errors.MultiModuleOutdirCannotBeExplodedModule(outDir)); 503 } 504 } else { 505 // single-module or legacy mode 506 boolean lintPaths = options.isUnset(Option.XLINT_CUSTOM, 507 "-" + LintCategory.PATH.option); 508 if (lintPaths) { 509 Path outDirParent = outDir.getParent(); 510 if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) { 511 log.warning(LintCategory.PATH, Warnings.OutdirIsInExplodedModule(outDir)); 512 } 513 } 514 } 515 } 516 } 517 518 519 String sourceString = options.get(Option.SOURCE); 520 Source source = (sourceString != null) 521 ? Source.lookup(sourceString) 522 : Source.DEFAULT; 523 String targetString = options.get(Option.TARGET); 524 Target target = (targetString != null) 525 ? Target.lookup(targetString) 526 : Target.DEFAULT; 527 528 // We don't check source/target consistency for CLDC, as J2ME 529 // profiles are not aligned with J2SE targets; moreover, a 530 // single CLDC target may have many profiles. In addition, 531 // this is needed for the continued functioning of the JSR14 532 // prototype. 533 if (Character.isDigit(target.name.charAt(0))) { 534 if (target.compareTo(source.requiredTarget()) < 0) { 535 if (targetString != null) { 536 if (sourceString == null) { 537 error("warn.target.default.source.conflict", 538 targetString, 539 source.requiredTarget().name); 540 } else { 541 error("warn.source.target.conflict", 542 sourceString, 543 source.requiredTarget().name); 544 } 545 return false; 546 } else { 547 target = source.requiredTarget(); 548 options.put("-target", target.name); 549 } 550 } 551 } 552 553 String profileString = options.get(Option.PROFILE); 554 if (profileString != null) { 555 Profile profile = Profile.lookup(profileString); 556 if (!profile.isValid(target)) { 557 error("warn.profile.target.conflict", profileString, target.name); 558 } 559 560 // This check is only effective in command line mode, 561 // where the file manager options are added to options 562 if (options.get(Option.BOOT_CLASS_PATH) != null) { 563 error("err.profile.bootclasspath.conflict"); 564 } 565 } 566 567 if (options.isSet(Option.SOURCE_PATH) && options.isSet(Option.MODULE_SOURCE_PATH)) { 568 error("err.sourcepath.modulesourcepath.conflict"); 569 } 570 571 boolean lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); 572 if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { 573 if (fm instanceof BaseFileManager) { 574 if (((BaseFileManager) fm).isDefaultBootClassPath()) 575 log.warning(LintCategory.OPTIONS, "source.no.bootclasspath", source.name); 576 } 577 } 578 579 boolean obsoleteOptionFound = false; 580 581 if (source.compareTo(Source.MIN) < 0) { 582 log.error(Errors.OptionRemovedSource(source.name, Source.MIN.name)); 583 } else if (source == Source.MIN && lintOptions) { 584 log.warning(LintCategory.OPTIONS, Warnings.OptionObsoleteSource(source.name)); 585 obsoleteOptionFound = true; 586 } 587 588 if (target.compareTo(Target.MIN) < 0) { 589 log.error(Errors.OptionRemovedTarget(target.name, Target.MIN.name)); 590 } else if (target == Target.MIN && lintOptions) { 591 log.warning(LintCategory.OPTIONS, Warnings.OptionObsoleteTarget(target.name)); 592 obsoleteOptionFound = true; 593 } 594 595 final Target t = target; 596 checkOptionAllowed(t.compareTo(Target.JDK1_8) <= 0, 597 option -> error("err.option.not.allowed.with.target", option.getPrimaryName(), t.name), 598 Option.BOOT_CLASS_PATH, 599 Option.XBOOTCLASSPATH_PREPEND, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND, 600 Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS, 601 Option.EXTDIRS, Option.DJAVA_EXT_DIRS, 602 Option.PROFILE); 603 604 checkOptionAllowed(t.compareTo(Target.JDK1_9) >= 0, 605 option -> error("err.option.not.allowed.with.target", option.getPrimaryName(), t.name), 606 Option.MODULE_SOURCE_PATH, Option.UPGRADE_MODULE_PATH, 607 Option.SYSTEM, Option.MODULE_PATH, Option.ADD_MODULES, 608 Option.ADD_EXPORTS, Option.ADD_OPENS, Option.ADD_READS, 609 Option.LIMIT_MODULES, 610 Option.PATCH_MODULE); 611 612 if (fm.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) { 613 if (!options.isSet(Option.PROC, "only") 614 && !fm.hasLocation(StandardLocation.CLASS_OUTPUT)) { 615 log.error(Errors.NoOutputDir); 616 } 617 } 618 619 if (fm.hasLocation(StandardLocation.ANNOTATION_PROCESSOR_MODULE_PATH) && 620 fm.hasLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH)) { 621 log.error(Errors.ProcessorpathNoProcessormodulepath); 622 } 623 624 if (obsoleteOptionFound && lintOptions) { 625 log.warning(LintCategory.OPTIONS, "option.obsolete.suppression"); 626 } 627 628 SourceVersion sv = Source.toSourceVersion(source); 629 validateAddExports(sv); 630 validateAddModules(sv); 631 validateAddReads(sv); 632 validateLimitModules(sv); 633 validateDefaultModuleForCreatedFiles(sv); 634 635 if (lintOptions && options.isSet(Option.ADD_OPENS)) { 636 log.warning(LintCategory.OPTIONS, Warnings.AddopensIgnored); 637 } 638 639 return !errors && (log.nerrors == 0); 640 } 641 642 private void validateAddExports(SourceVersion sv) { 643 String addExports = options.get(Option.ADD_EXPORTS); 644 if (addExports != null) { 645 // Each entry must be of the form sourceModule/sourcePackage=target-list where 646 // target-list is a comma separated list of module or ALL-UNNAMED. 647 // Empty items in the target-list are ignored. 648 // There must be at least one item in the list; this is handled in Option.ADD_EXPORTS. 649 Pattern p = Option.ADD_EXPORTS.getPattern(); 650 for (String e : addExports.split("\0")) { 651 Matcher m = p.matcher(e); 652 if (m.matches()) { 653 String sourceModuleName = m.group(1); 654 if (!SourceVersion.isName(sourceModuleName, sv)) { 655 // syntactically invalid source name: e.g. --add-exports m!/p1=m2 656 log.warning(Warnings.BadNameForOption(Option.ADD_EXPORTS, sourceModuleName)); 657 } 658 String sourcePackageName = m.group(2); 659 if (!SourceVersion.isName(sourcePackageName, sv)) { 660 // syntactically invalid source name: e.g. --add-exports m1/p!=m2 661 log.warning(Warnings.BadNameForOption(Option.ADD_EXPORTS, sourcePackageName)); 662 } 663 664 String targetNames = m.group(3); 665 for (String targetName : targetNames.split(",")) { 666 switch (targetName) { 667 case "": 668 case "ALL-UNNAMED": 669 break; 670 671 default: 672 if (!SourceVersion.isName(targetName, sv)) { 673 // syntactically invalid target name: e.g. --add-exports m1/p1=m! 674 log.warning(Warnings.BadNameForOption(Option.ADD_EXPORTS, targetName)); 675 } 676 break; 677 } 678 } 679 } 680 } 681 } 682 } 683 684 private void validateAddReads(SourceVersion sv) { 685 String addReads = options.get(Option.ADD_READS); 686 if (addReads != null) { 687 // Each entry must be of the form source=target-list where target-list is a 688 // comma-separated list of module or ALL-UNNAMED. 689 // Empty items in the target list are ignored. 690 // There must be at least one item in the list; this is handled in Option.ADD_READS. 691 Pattern p = Option.ADD_READS.getPattern(); 692 for (String e : addReads.split("\0")) { 693 Matcher m = p.matcher(e); 694 if (m.matches()) { 695 String sourceName = m.group(1); 696 if (!SourceVersion.isName(sourceName, sv)) { 697 // syntactically invalid source name: e.g. --add-reads m!=m2 698 log.warning(Warnings.BadNameForOption(Option.ADD_READS, sourceName)); 699 } 700 701 String targetNames = m.group(2); 702 for (String targetName : targetNames.split(",", -1)) { 703 switch (targetName) { 704 case "": 705 case "ALL-UNNAMED": 706 break; 707 708 default: 709 if (!SourceVersion.isName(targetName, sv)) { 710 // syntactically invalid target name: e.g. --add-reads m1=m! 711 log.warning(Warnings.BadNameForOption(Option.ADD_READS, targetName)); 712 } 713 break; 714 } 715 } 716 } 717 } 718 } 719 } 720 721 private void validateAddModules(SourceVersion sv) { 722 String addModules = options.get(Option.ADD_MODULES); 723 if (addModules != null) { 724 // Each entry must be of the form target-list where target-list is a 725 // comma separated list of module names, or ALL-DEFAULT, ALL-SYSTEM, 726 // or ALL-MODULE_PATH. 727 // Empty items in the target list are ignored. 728 // There must be at least one item in the list; this is handled in Option.ADD_MODULES. 729 for (String moduleName : addModules.split(",")) { 730 switch (moduleName) { 731 case "": 732 case "ALL-SYSTEM": 733 case "ALL-MODULE-PATH": 734 break; 735 736 default: 737 if (!SourceVersion.isName(moduleName, sv)) { 738 // syntactically invalid module name: e.g. --add-modules m1,m! 739 log.error(Errors.BadNameForOption(Option.ADD_MODULES, moduleName)); 740 } 741 break; 742 } 743 } 744 } 745 } 746 747 private void validateLimitModules(SourceVersion sv) { 748 String limitModules = options.get(Option.LIMIT_MODULES); 749 if (limitModules != null) { 750 // Each entry must be of the form target-list where target-list is a 751 // comma separated list of module names, or ALL-DEFAULT, ALL-SYSTEM, 752 // or ALL-MODULE_PATH. 753 // Empty items in the target list are ignored. 754 // There must be at least one item in the list; this is handled in Option.LIMIT_EXPORTS. 755 for (String moduleName : limitModules.split(",")) { 756 switch (moduleName) { 757 case "": 758 break; 759 760 default: 761 if (!SourceVersion.isName(moduleName, sv)) { 762 // syntactically invalid module name: e.g. --limit-modules m1,m! 763 log.error(Errors.BadNameForOption(Option.LIMIT_MODULES, moduleName)); 764 } 765 break; 766 } 767 } 768 } 769 } 770 771 private void validateDefaultModuleForCreatedFiles(SourceVersion sv) { 772 String moduleName = options.get(Option.DEFAULT_MODULE_FOR_CREATED_FILES); 773 if (moduleName != null) { 774 if (!SourceVersion.isName(moduleName, sv)) { 775 // syntactically invalid module name: e.g. --default-module-for-created-files m! 776 log.error(Errors.BadNameForOption(Option.DEFAULT_MODULE_FOR_CREATED_FILES, 777 moduleName)); 778 } 779 } 780 } 781 782 /** 783 * Returns true if there are no files or classes specified for use. 784 * @return true if there are no files or classes specified for use 785 */ 786 public boolean isEmpty() { 787 return ((files == null) || files.isEmpty()) 788 && ((fileObjects == null) || fileObjects.isEmpty()) 789 && (classNames == null || classNames.isEmpty()); 790 } 791 792 public void allowEmpty() { 793 this.emptyAllowed = true; 794 } 795 796 /** 797 * Gets the file manager options which may have been deferred 798 * during processArgs. 799 * @return the deferred file manager options 800 */ 801 public Map<Option, String> getDeferredFileManagerOptions() { 802 return deferredFileManagerOptions; 803 } 804 805 /** 806 * Gets any options specifying plugins to be run. 807 * @return options for plugins 808 */ 809 public Set<List<String>> getPluginOpts() { 810 String plugins = options.get(Option.PLUGIN); 811 if (plugins == null) 812 return Collections.emptySet(); 813 814 Set<List<String>> pluginOpts = new LinkedHashSet<>(); 815 for (String plugin: plugins.split("\\x00")) { 816 pluginOpts.add(List.from(plugin.split("\\s+"))); 817 } 818 return Collections.unmodifiableSet(pluginOpts); 819 } 820 821 /** 822 * Gets any options specifying how doclint should be run. 823 * An empty list is returned if no doclint options are specified 824 * or if the only doclint option is -Xdoclint:none. 825 * @return options for doclint 826 */ 827 public List<String> getDocLintOpts() { 828 String xdoclint = options.get(Option.XDOCLINT); 829 String xdoclintCustom = options.get(Option.XDOCLINT_CUSTOM); 830 if (xdoclint == null && xdoclintCustom == null) 831 return List.nil(); 832 833 Set<String> doclintOpts = new LinkedHashSet<>(); 834 if (xdoclint != null) 835 doclintOpts.add(DocLint.XMSGS_OPTION); 836 if (xdoclintCustom != null) { 837 for (String s: xdoclintCustom.split("\\s+")) { 838 if (s.isEmpty()) 839 continue; 840 doclintOpts.add(DocLint.XMSGS_CUSTOM_PREFIX + s); 841 } 842 } 843 844 if (doclintOpts.equals(Collections.singleton(DocLint.XMSGS_CUSTOM_PREFIX + "none"))) 845 return List.nil(); 846 847 String checkPackages = options.get(Option.XDOCLINT_PACKAGE); 848 if (checkPackages != null) { 849 for (String s : checkPackages.split("\\s+")) { 850 doclintOpts.add(DocLint.XCHECK_PACKAGE + s); 851 } 852 } 853 854 String format = options.get(Option.DOCLINT_FORMAT); 855 if (format != null) { 856 doclintOpts.add(DocLint.XHTML_VERSION_PREFIX + format); 857 } 858 859 // standard doclet normally generates H1, H2, 860 // so for now, allow user comments to assume that 861 doclintOpts.add(DocLint.XIMPLICIT_HEADERS + "2"); 862 return List.from(doclintOpts.toArray(new String[doclintOpts.size()])); 863 } 864 865 private boolean checkDirectory(Option option) { 866 String value = options.get(option); 867 if (value == null) { 868 return true; 869 } 870 Path file = Paths.get(value); 871 if (Files.exists(file) && !Files.isDirectory(file)) { 872 error("err.file.not.directory", value); 873 return false; 874 } 875 return true; 876 } 877 878 private interface ErrorReporter { 879 void report(Option o); 880 } 881 882 void checkOptionAllowed(boolean allowed, ErrorReporter r, Option... opts) { 883 if (!allowed) { 884 Stream.of(opts) 885 .filter(options :: isSet) 886 .forEach(r :: report); 887 } 888 } 889 890 void error(JCDiagnostic.Error error) { 891 errors = true; 892 switch (errorMode) { 893 case ILLEGAL_ARGUMENT: { 894 String msg = log.localize(error); 895 throw new PropagatedException(new IllegalArgumentException(msg)); 896 } 897 case ILLEGAL_STATE: { 898 String msg = log.localize(error); 899 throw new PropagatedException(new IllegalStateException(msg)); 900 } 901 case LOG: 902 report(error); 903 } 904 } 905 906 void error(String key, Object... args) { 907 errors = true; 908 switch (errorMode) { 909 case ILLEGAL_ARGUMENT: { 910 String msg = log.localize(PrefixKind.JAVAC, key, args); 911 throw new PropagatedException(new IllegalArgumentException(msg)); 912 } 913 case ILLEGAL_STATE: { 914 String msg = log.localize(PrefixKind.JAVAC, key, args); 915 throw new PropagatedException(new IllegalStateException(msg)); 916 } 917 case LOG: 918 report(key, args); 919 } 920 } 921 922 void error(Option.InvalidValueException f) { 923 String msg = f.getMessage(); 924 errors = true; 925 switch (errorMode) { 926 case ILLEGAL_ARGUMENT: { 927 throw new PropagatedException(new IllegalArgumentException(msg, f.getCause())); 928 } 929 case ILLEGAL_STATE: { 930 throw new PropagatedException(new IllegalStateException(msg, f.getCause())); 931 } 932 case LOG: 933 log.printRawLines(ownName + ": " + msg); 934 } 935 } 936 937 void warning(String key, Object... args) { 938 report(key, args); 939 } 940 941 private void report(String key, Object... args) { 942 // Would be good to have support for -XDrawDiagnostics here 943 log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); 944 } 945 946 private void report(JCDiagnostic.Error error) { 947 // Would be good to have support for -XDrawDiagnostics here 948 log.printRawLines(ownName + ": " + log.localize(error)); 949 } 950 951 private JavaFileManager getFileManager() { 952 if (fileManager == null) 953 fileManager = context.get(JavaFileManager.class); 954 return fileManager; 955 } 956 957 <T> ListBuffer<T> toList(Iterable<? extends T> items) { 958 ListBuffer<T> list = new ListBuffer<>(); 959 if (items != null) { 960 for (T item : items) { 961 list.add(item); 962 } 963 } 964 return list; 965 } 966 967 <T> Set<T> toSet(Iterable<? extends T> items) { 968 Set<T> set = new LinkedHashSet<>(); 969 if (items != null) { 970 for (T item : items) { 971 set.add(item); 972 } 973 } 974 return set; 975 } 976} 977