Arguments.java revision 3170:dc017a37aac5
1/* 2 * Copyright (c) 1999, 2015, 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.IOException; 30import java.nio.file.Path; 31import java.util.Collection; 32import java.util.Collections; 33import java.util.Iterator; 34import java.util.LinkedHashMap; 35import java.util.LinkedHashSet; 36import java.util.Map; 37import java.util.Set; 38import java.util.stream.Stream; 39 40import javax.tools.JavaFileManager; 41import javax.tools.JavaFileObject; 42import javax.tools.StandardJavaFileManager; 43import javax.tools.StandardLocation; 44 45import com.sun.tools.doclint.DocLint; 46import com.sun.tools.javac.code.Lint.LintCategory; 47import com.sun.tools.javac.code.Source; 48import com.sun.tools.javac.file.BaseFileManager; 49import com.sun.tools.javac.file.JavacFileManager; 50import com.sun.tools.javac.jvm.Profile; 51import com.sun.tools.javac.jvm.Target; 52import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; 53import com.sun.tools.javac.platform.PlatformDescription; 54import com.sun.tools.javac.platform.PlatformUtils; 55import com.sun.tools.javac.util.Context; 56import com.sun.tools.javac.util.List; 57import com.sun.tools.javac.util.ListBuffer; 58import com.sun.tools.javac.util.Log; 59import com.sun.tools.javac.util.Log.PrefixKind; 60import com.sun.tools.javac.util.Log.WriterKind; 61import com.sun.tools.javac.util.Options; 62import com.sun.tools.javac.util.PropagatedException; 63 64/** 65 * Shared option and argument handling for command line and API usage of javac. 66 */ 67public class Arguments { 68 69 /** 70 * The context key for the arguments. 71 */ 72 public static final Context.Key<Arguments> argsKey = new Context.Key<>(); 73 74 private String ownName; 75 private Set<String> classNames; 76 private Set<File> files; 77 private Map<Option, String> deferredFileManagerOptions; 78 private Set<JavaFileObject> fileObjects; 79 private final Options options; 80 81 private JavaFileManager fileManager; 82 private final Log log; 83 private final Context context; 84 85 private enum ErrorMode { ILLEGAL_ARGUMENT, ILLEGAL_STATE, LOG }; 86 private ErrorMode errorMode; 87 private boolean errors; 88 89 /** 90 * Gets the Arguments instance for this context. 91 * 92 * @param context the content 93 * @return the Arguments instance for this context. 94 */ 95 public static Arguments instance(Context context) { 96 Arguments instance = context.get(argsKey); 97 if (instance == null) { 98 instance = new Arguments(context); 99 } 100 return instance; 101 } 102 103 protected Arguments(Context context) { 104 context.put(argsKey, this); 105 options = Options.instance(context); 106 log = Log.instance(context); 107 this.context = context; 108 109 // Ideally, we could init this here and update/configure it as 110 // needed, but right now, initializing a file manager triggers 111 // initialization of other items in the context, such as Lint 112 // and FSInfo, which should not be initialized until after 113 // processArgs 114 // fileManager = context.get(JavaFileManager.class); 115 } 116 117 private final OptionHelper cmdLineHelper = new OptionHelper() { 118 @Override 119 public String get(Option option) { 120 return options.get(option); 121 } 122 123 @Override 124 public void put(String name, String value) { 125 options.put(name, value); 126 } 127 128 @Override 129 public void remove(String name) { 130 options.remove(name); 131 } 132 133 @Override 134 public boolean handleFileManagerOption(Option option, String value) { 135 options.put(option.getText(), value); 136 deferredFileManagerOptions.put(option, value); 137 return true; 138 } 139 140 @Override 141 public Log getLog() { 142 return log; 143 } 144 145 @Override 146 public String getOwnName() { 147 return ownName; 148 } 149 150 @Override 151 public void error(String key, Object... args) { 152 Arguments.this.error(key, args); 153 } 154 155 @Override 156 public void addFile(File f) { 157 files.add(f); 158 } 159 160 @Override 161 public void addClassName(String s) { 162 classNames.add(s); 163 } 164 165 }; 166 167 /** 168 * Initializes this Args instance with a set of command line args. 169 * The args will be processed in conjunction with the full set of 170 * command line options, including -help, -version etc. 171 * The args may also contain class names and filenames. 172 * Any errors during this call, and later during validate, will be reported 173 * to the log. 174 * @param ownName the name of this tool; used to prefix messages 175 * @param args the args to be processed 176 */ 177 public void init(String ownName, String... args) { 178 this.ownName = ownName; 179 errorMode = ErrorMode.LOG; 180 files = new LinkedHashSet<>(); 181 deferredFileManagerOptions = new LinkedHashMap<>(); 182 fileObjects = null; 183 classNames = new LinkedHashSet<>(); 184 processArgs(List.from(args), Option.getJavaCompilerOptions(), cmdLineHelper, true, false); 185 } 186 187 private final OptionHelper apiHelper = new GrumpyHelper(null) { 188 @Override 189 public String get(Option option) { 190 return options.get(option.getText()); 191 } 192 193 @Override 194 public void put(String name, String value) { 195 options.put(name, value); 196 } 197 198 @Override 199 public void remove(String name) { 200 options.remove(name); 201 } 202 203 @Override 204 public void error(String key, Object... args) { 205 Arguments.this.error(key, args); 206 } 207 208 @Override 209 public Log getLog() { 210 return Arguments.this.log; 211 } 212 }; 213 214 /** 215 * Initializes this Args instance with the parameters for a JavacTask. 216 * The options will be processed in conjunction with the restricted set 217 * of tool options, which does not include -help, -version, etc, 218 * nor does it include classes and filenames, which should be specified 219 * separately. 220 * File manager options are handled directly by the file manager. 221 * Any errors found while processing individual args will be reported 222 * via IllegalArgumentException. 223 * Any subsequent errors during validate will be reported via IllegalStateException. 224 * @param ownName the name of this tool; used to prefix messages 225 * @param options the options to be processed 226 * @param classNames the classes to be subject to annotation processing 227 * @param files the files to be compiled 228 */ 229 public void init(String ownName, 230 Iterable<String> options, 231 Iterable<String> classNames, 232 Iterable<? extends JavaFileObject> files) { 233 this.ownName = ownName; 234 this.classNames = toSet(classNames); 235 this.fileObjects = toSet(files); 236 this.files = null; 237 errorMode = ErrorMode.ILLEGAL_ARGUMENT; 238 if (options != null) { 239 processArgs(toList(options), Option.getJavacToolOptions(), apiHelper, false, true); 240 } 241 errorMode = ErrorMode.ILLEGAL_STATE; 242 } 243 244 /** 245 * Gets the files to be compiled. 246 * @return the files to be compiled 247 */ 248 public Set<JavaFileObject> getFileObjects() { 249 if (fileObjects == null) { 250 if (files == null) { 251 fileObjects = Collections.emptySet(); 252 } else { 253 fileObjects = new LinkedHashSet<>(); 254 JavacFileManager jfm = (JavacFileManager) getFileManager(); 255 for (JavaFileObject fo: jfm.getJavaFileObjectsFromFiles(files)) 256 fileObjects.add(fo); 257 } 258 } 259 return fileObjects; 260 } 261 262 /** 263 * Gets the classes to be subject to annotation processing. 264 * @return the classes to be subject to annotation processing 265 */ 266 public Set<String> getClassNames() { 267 return classNames; 268 } 269 270 /** 271 * Processes strings containing options and operands. 272 * @param args the strings to be processed 273 * @param allowableOpts the set of option declarations that are applicable 274 * @param helper a help for use by Option.process 275 * @param allowOperands whether or not to check for files and classes 276 * @param checkFileManager whether or not to check if the file manager can handle 277 * options which are not recognized by any of allowableOpts 278 * @return true if all the strings were successfully processed; false otherwise 279 * @throws IllegalArgumentException if a problem occurs and errorMode is set to 280 * ILLEGAL_ARGUMENT 281 */ 282 private boolean processArgs(Iterable<String> args, 283 Set<Option> allowableOpts, OptionHelper helper, 284 boolean allowOperands, boolean checkFileManager) { 285 if (!doProcessArgs(args, allowableOpts, helper, allowOperands, checkFileManager)) 286 return false; 287 288 String platformString = options.get(Option.RELEASE); 289 290 checkOptionAllowed(platformString == null, 291 option -> error("err.release.bootclasspath.conflict", option.getText()), 292 Option.BOOTCLASSPATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND, 293 Option.XBOOTCLASSPATH_PREPEND, Option.ENDORSEDDIRS, Option.EXTDIRS, Option.SOURCE, 294 Option.TARGET); 295 296 if (platformString != null) { 297 PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString); 298 299 if (platformDescription == null) { 300 error("err.unsupported.release.version", platformString); 301 return false; 302 } 303 304 options.put(Option.SOURCE, platformDescription.getSourceVersion()); 305 options.put(Option.TARGET, platformDescription.getTargetVersion()); 306 307 context.put(PlatformDescription.class, platformDescription); 308 309 if (!doProcessArgs(platformDescription.getAdditionalOptions(), allowableOpts, helper, allowOperands, checkFileManager)) 310 return false; 311 312 Collection<Path> platformCP = platformDescription.getPlatformPath(); 313 314 if (platformCP != null) { 315 JavaFileManager fm = getFileManager(); 316 317 if (!(fm instanceof StandardJavaFileManager)) { 318 error("err.release.not.standard.file.manager"); 319 return false; 320 } 321 322 try { 323 StandardJavaFileManager sfm = (StandardJavaFileManager) fm; 324 325 sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP); 326 } catch (IOException ex) { 327 log.printLines(PrefixKind.JAVAC, "msg.io"); 328 ex.printStackTrace(log.getWriter(WriterKind.NOTICE)); 329 return false; 330 } 331 } 332 } 333 334 options.notifyListeners(); 335 336 return true; 337 } 338 339 private boolean doProcessArgs(Iterable<String> args, 340 Set<Option> allowableOpts, OptionHelper helper, 341 boolean allowOperands, boolean checkFileManager) { 342 JavaFileManager fm = checkFileManager ? getFileManager() : null; 343 Iterator<String> argIter = args.iterator(); 344 while (argIter.hasNext()) { 345 String arg = argIter.next(); 346 if (arg.isEmpty()) { 347 error("err.invalid.flag", arg); 348 return false; 349 } 350 351 Option option = null; 352 if (arg.startsWith("-")) { 353 for (Option o : allowableOpts) { 354 if (o.matches(arg)) { 355 option = o; 356 break; 357 } 358 } 359 } else if (allowOperands && Option.SOURCEFILE.matches(arg)) { 360 option = Option.SOURCEFILE; 361 } 362 363 if (option == null) { 364 if (fm != null && fm.handleOption(arg, argIter)) { 365 continue; 366 } 367 error("err.invalid.flag", arg); 368 return false; 369 } 370 371 if (option.hasArg()) { 372 if (!argIter.hasNext()) { 373 error("err.req.arg", arg); 374 return false; 375 } 376 String operand = argIter.next(); 377 if (option.process(helper, arg, operand)) { 378 return false; 379 } 380 } else { 381 if (option.process(helper, arg)) { 382 return false; 383 } 384 } 385 } 386 387 return true; 388 } 389 390 /** 391 * Validates the overall consistency of the options and operands 392 * processed by processOptions. 393 * @return true if all args are successfully validating; false otherwise. 394 * @throws IllegalStateException if a problem is found and errorMode is set to 395 * ILLEGAL_STATE 396 */ 397 public boolean validate() { 398 if (isEmpty()) { 399 // It is allowed to compile nothing if just asking for help or version info. 400 // But also note that none of these options are supported in API mode. 401 if (options.isSet(Option.HELP) 402 || options.isSet(Option.X) 403 || options.isSet(Option.VERSION) 404 || options.isSet(Option.FULLVERSION)) 405 return true; 406 407 if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { 408 error("err.no.source.files.classes"); 409 } else { 410 error("err.no.source.files"); 411 } 412 return false; 413 } 414 415 if (!checkDirectory(Option.D)) { 416 return false; 417 } 418 if (!checkDirectory(Option.S)) { 419 return false; 420 } 421 if (!checkDirectory(Option.H)) { 422 return false; 423 } 424 425 String sourceString = options.get(Option.SOURCE); 426 Source source = (sourceString != null) 427 ? Source.lookup(sourceString) 428 : Source.DEFAULT; 429 String targetString = options.get(Option.TARGET); 430 Target target = (targetString != null) 431 ? Target.lookup(targetString) 432 : Target.DEFAULT; 433 434 // We don't check source/target consistency for CLDC, as J2ME 435 // profiles are not aligned with J2SE targets; moreover, a 436 // single CLDC target may have many profiles. In addition, 437 // this is needed for the continued functioning of the JSR14 438 // prototype. 439 if (Character.isDigit(target.name.charAt(0))) { 440 if (target.compareTo(source.requiredTarget()) < 0) { 441 if (targetString != null) { 442 if (sourceString == null) { 443 error("warn.target.default.source.conflict", 444 targetString, 445 source.requiredTarget().name); 446 } else { 447 error("warn.source.target.conflict", 448 sourceString, 449 source.requiredTarget().name); 450 } 451 return false; 452 } else { 453 target = source.requiredTarget(); 454 options.put("-target", target.name); 455 } 456 } 457 } 458 459 String profileString = options.get(Option.PROFILE); 460 if (profileString != null) { 461 Profile profile = Profile.lookup(profileString); 462 if (!profile.isValid(target)) { 463 error("warn.profile.target.conflict", profileString, target.name); 464 } 465 466 // This check is only effective in command line mode, 467 // where the file manager options are added to options 468 if (options.get(Option.BOOTCLASSPATH) != null) { 469 error("err.profile.bootclasspath.conflict"); 470 } 471 } 472 473 boolean lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); 474 475 if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { 476 JavaFileManager fm = getFileManager(); 477 if (fm instanceof BaseFileManager) { 478 if (((BaseFileManager) fm).isDefaultBootClassPath()) 479 log.warning(LintCategory.OPTIONS, "source.no.bootclasspath", source.name); 480 } 481 } 482 483 boolean obsoleteOptionFound = false; 484 485 if (source.compareTo(Source.MIN) < 0) { 486 log.error("option.removed.source", source.name, Source.MIN.name); 487 } else if (source == Source.MIN && lintOptions) { 488 log.warning(LintCategory.OPTIONS, "option.obsolete.source", source.name); 489 obsoleteOptionFound = true; 490 } 491 492 if (target.compareTo(Target.MIN) < 0) { 493 log.error("option.removed.target", target.name, Target.MIN.name); 494 } else if (target == Target.MIN && lintOptions) { 495 log.warning(LintCategory.OPTIONS, "option.obsolete.target", target.name); 496 obsoleteOptionFound = true; 497 } 498 499 if (obsoleteOptionFound) 500 log.warning(LintCategory.OPTIONS, "option.obsolete.suppression"); 501 502 return !errors; 503 } 504 505 /** 506 * Returns true if there are no files or classes specified for use. 507 * @return true if there are no files or classes specified for use 508 */ 509 public boolean isEmpty() { 510 return ((files == null) || files.isEmpty()) 511 && ((fileObjects == null) || fileObjects.isEmpty()) 512 && classNames.isEmpty(); 513 } 514 515 /** 516 * Gets the file manager options which may have been deferred 517 * during processArgs. 518 * @return the deferred file manager options 519 */ 520 public Map<Option, String> getDeferredFileManagerOptions() { 521 return deferredFileManagerOptions; 522 } 523 524 /** 525 * Gets any options specifying plugins to be run. 526 * @return options for plugins 527 */ 528 public Set<List<String>> getPluginOpts() { 529 String plugins = options.get(Option.PLUGIN); 530 if (plugins == null) 531 return Collections.emptySet(); 532 533 Set<List<String>> pluginOpts = new LinkedHashSet<>(); 534 for (String plugin: plugins.split("\\x00")) { 535 pluginOpts.add(List.from(plugin.split("\\s+"))); 536 } 537 return Collections.unmodifiableSet(pluginOpts); 538 } 539 540 /** 541 * Gets any options specifying how doclint should be run. 542 * An empty list is returned if no doclint options are specified 543 * or if the only doclint option is -Xdoclint:none. 544 * @return options for doclint 545 */ 546 public List<String> getDocLintOpts() { 547 String xdoclint = options.get(Option.XDOCLINT); 548 String xdoclintCustom = options.get(Option.XDOCLINT_CUSTOM); 549 if (xdoclint == null && xdoclintCustom == null) 550 return List.nil(); 551 552 Set<String> doclintOpts = new LinkedHashSet<>(); 553 if (xdoclint != null) 554 doclintOpts.add(DocLint.XMSGS_OPTION); 555 if (xdoclintCustom != null) { 556 for (String s: xdoclintCustom.split("\\s+")) { 557 if (s.isEmpty()) 558 continue; 559 doclintOpts.add(s.replace(Option.XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX)); 560 } 561 } 562 563 if (doclintOpts.equals(Collections.singleton(DocLint.XMSGS_CUSTOM_PREFIX + "none"))) 564 return List.nil(); 565 566 String checkPackages = options.get(Option.XDOCLINT_PACKAGE); 567 568 if (checkPackages != null) { 569 for (String s : checkPackages.split("\\s+")) { 570 doclintOpts.add(s.replace(Option.XDOCLINT_PACKAGE.text, DocLint.XCHECK_PACKAGE)); 571 } 572 } 573 574 // standard doclet normally generates H1, H2, 575 // so for now, allow user comments to assume that 576 doclintOpts.add(DocLint.XIMPLICIT_HEADERS + "2"); 577 578 return List.from(doclintOpts.toArray(new String[doclintOpts.size()])); 579 } 580 581 private boolean checkDirectory(Option option) { 582 String value = options.get(option); 583 if (value == null) { 584 return true; 585 } 586 File file = new File(value); 587 if (file.exists() && !file.isDirectory()) { 588 error("err.file.not.directory", value); 589 return false; 590 } 591 return true; 592 } 593 594 private interface ErrorReporter { 595 void report(Option o); 596 } 597 598 void checkOptionAllowed(boolean allowed, ErrorReporter r, Option... opts) { 599 if (!allowed) { 600 Stream.of(opts) 601 .filter(options :: isSet) 602 .forEach(r :: report); 603 } 604 } 605 606 void error(String key, Object... args) { 607 errors = true; 608 switch (errorMode) { 609 case ILLEGAL_ARGUMENT: { 610 String msg = log.localize(PrefixKind.JAVAC, key, args); 611 throw new PropagatedException(new IllegalArgumentException(msg)); 612 } 613 case ILLEGAL_STATE: { 614 String msg = log.localize(PrefixKind.JAVAC, key, args); 615 throw new PropagatedException(new IllegalStateException(msg)); 616 } 617 case LOG: 618 report(key, args); 619 log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); 620 } 621 } 622 623 void warning(String key, Object... args) { 624 report(key, args); 625 } 626 627 private void report(String key, Object... args) { 628 log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); 629 } 630 631 private JavaFileManager getFileManager() { 632 if (fileManager == null) 633 fileManager = context.get(JavaFileManager.class); 634 return fileManager; 635 } 636 637 <T> ListBuffer<T> toList(Iterable<? extends T> items) { 638 ListBuffer<T> list = new ListBuffer<>(); 639 if (items != null) { 640 for (T item : items) { 641 list.add(item); 642 } 643 } 644 return list; 645 } 646 647 <T> Set<T> toSet(Iterable<? extends T> items) { 648 Set<T> set = new LinkedHashSet<>(); 649 if (items != null) { 650 for (T item : items) { 651 set.add(item); 652 } 653 } 654 return set; 655 } 656} 657