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