1 /* 2 * Copyright (c) 1999, 2014, 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 package com.sun.tools.javac.main; 26 27 import java.io.File; 28 import java.io.IOException; 29 import java.nio.file.Path; 30 import java.util.Collection; 31 import java.util.Collections; 32 import java.util.Iterator; 33 import java.util.LinkedHashMap; 34 import java.util.LinkedHashSet; 35 import java.util.Map; 36 import java.util.ServiceLoader; 37 import java.util.Set; 38 import java.util.stream.Stream; 39 import java.util.stream.StreamSupport; 40 41 import javax.tools.JavaFileManager; 42 import javax.tools.JavaFileObject; 43 import javax.tools.StandardJavaFileManager; 44 import javax.tools.StandardLocation; 45 46 import com.sun.tools.doclint.DocLint; 47 import com.sun.tools.javac.code.Lint.LintCategory; 48 import com.sun.tools.javac.code.Source; 49 import com.sun.tools.javac.file.BaseFileManager; 50 import com.sun.tools.javac.file.JavacFileManager; 51 import com.sun.tools.javac.jvm.Profile; 52 import com.sun.tools.javac.jvm.Target; 53 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; 54 import com.sun.tools.javac.platform.PlatformProvider; 55 import com.sun.tools.javac.platform.PlatformProviderFactory; 56 import com.sun.tools.javac.util.Context; 57 import com.sun.tools.javac.util.List; 58 import com.sun.tools.javac.util.ListBuffer; 59 import com.sun.tools.javac.util.Log; 60 import com.sun.tools.javac.util.Log.PrefixKind; 61 import com.sun.tools.javac.util.Log.WriterKind; 62 import com.sun.tools.javac.util.Options; 63 import com.sun.tools.javac.util.PropagatedException; 64 65 /** 66 * Shared option and argument handling for command line and API usage of javac. 67 */ 68 public class Arguments { 69 70 /** 71 * The context key for the arguments. 72 */ 73 protected static final Context.Key<Arguments> argsKey = new Context.Key<>(); 74 75 private String ownName; 76 private Set<String> classNames; 77 private Set<File> 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(File f) { 158 files.add(f); 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.getJavaFileObjectsFromFiles(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 PlatformProvider platformProvider = lookupPlatformProvider(platformString); 299 300 if (platformProvider == null) { 301 error("err.unsupported.release.version", platformString); 302 return false; 303 } 304 305 options.put(Option.SOURCE, platformProvider.getSourceVersion()); 306 options.put(Option.TARGET, platformProvider.getTargetVersion()); 307 308 context.put(PlatformProvider.class, platformProvider); 309 310 if (!doProcessArgs(platformProvider.getAdditionalOptions(), allowableOpts, helper, allowOperands, checkFileManager)) 311 return false; 312 313 Collection<Path> platformCP = platformProvider.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 423 String sourceString = options.get(Option.SOURCE); 424 Source source = (sourceString != null) 425 ? Source.lookup(sourceString) 426 : Source.DEFAULT; 427 String targetString = options.get(Option.TARGET); 428 Target target = (targetString != null) 429 ? Target.lookup(targetString) 430 : Target.DEFAULT; 431 432 // We don't check source/target consistency for CLDC, as J2ME 433 // profiles are not aligned with J2SE targets; moreover, a 434 // single CLDC target may have many profiles. In addition, 435 // this is needed for the continued functioning of the JSR14 436 // prototype. 437 if (Character.isDigit(target.name.charAt(0))) { 438 if (target.compareTo(source.requiredTarget()) < 0) { 439 if (targetString != null) { 440 if (sourceString == null) { 441 error("warn.target.default.source.conflict", 442 targetString, 443 source.requiredTarget().name); 444 } else { 445 error("warn.source.target.conflict", 446 sourceString, 447 source.requiredTarget().name); 448 } 449 return false; 450 } else { 451 target = source.requiredTarget(); 452 options.put("-target", target.name); 453 } 454 } 455 } 456 457 String profileString = options.get(Option.PROFILE); 458 if (profileString != null) { 459 Profile profile = Profile.lookup(profileString); 460 if (!profile.isValid(target)) { 461 error("warn.profile.target.conflict", profileString, target.name); 462 } 463 464 // This check is only effective in command line mode, 465 // where the file manager options are added to options 466 if (options.get(Option.BOOTCLASSPATH) != null) { 467 error("err.profile.bootclasspath.conflict"); 468 } 469 } 470 471 boolean lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); 472 473 if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { 474 JavaFileManager fm = getFileManager(); 475 if (fm instanceof BaseFileManager) { 476 if (((BaseFileManager) fm).isDefaultBootClassPath()) 477 log.warning(LintCategory.OPTIONS, "source.no.bootclasspath", source.name); 478 } 479 } 480 481 boolean obsoleteOptionFound = false; 482 483 if (source.compareTo(Source.MIN) < 0) { 484 log.error("option.removed.source", source.name, Source.MIN.name); 485 } else if (source == Source.MIN && lintOptions) { 486 log.warning(LintCategory.OPTIONS, "option.obsolete.source", source.name); 487 obsoleteOptionFound = true; 488 } 489 490 if (target.compareTo(Target.MIN) < 0) { 491 log.error("option.removed.target", target.name, Target.MIN.name); 492 } else if (target == Target.MIN && lintOptions) { 493 log.warning(LintCategory.OPTIONS, "option.obsolete.target", target.name); 494 obsoleteOptionFound = true; 495 } 496 497 if (obsoleteOptionFound) 498 log.warning(LintCategory.OPTIONS, "option.obsolete.suppression"); 499 500 return !errors; 501 } 502 503 private PlatformProvider lookupPlatformProvider(String platform) { 504 Iterable<PlatformProviderFactory> factories = 505 ServiceLoader.load(PlatformProviderFactory.class, Arguments.class.getClassLoader()); 506 return StreamSupport.stream(factories.spliterator(), false) 507 .flatMap(factory -> StreamSupport.stream(factory.createPlatformProviders() 508 .spliterator(), 509 false)) 510 .filter(provider -> platform.equals(provider.getName())) 511 .findFirst() 512 .orElse(null); 513 } 514 515 /** 516 * Returns true if there are no files or classes specified for use. 517 * @return true if there are no files or classes specified for use 518 */ 519 public boolean isEmpty() { 520 return ((files == null) || files.isEmpty()) 521 && ((fileObjects == null) || fileObjects.isEmpty()) 522 && classNames.isEmpty(); 523 } 524 525 /** 526 * Gets the file manager options which may have been deferred 527 * during processArgs. 528 * @return the deferred file manager options 529 */ 530 public Map<Option, String> getDeferredFileManagerOptions() { 531 return deferredFileManagerOptions; 532 } 533 534 /** 535 * Gets any options specifying plugins to be run. 536 * @return options for plugins 537 */ 538 public Set<List<String>> getPluginOpts() { 539 String plugins = options.get(Option.PLUGIN); 540 if (plugins == null) 541 return Collections.emptySet(); 542 543 Set<List<String>> pluginOpts = new LinkedHashSet<>(); 544 for (String plugin: plugins.split("\\x00")) { 545 pluginOpts.add(List.from(plugin.split("\\s+"))); 546 } 547 return Collections.unmodifiableSet(pluginOpts); 548 } 549 550 /** 551 * Gets any options specifying how doclint should be run. 552 * An empty list is returned if no doclint options are specified 553 * or if the only doclint option is -Xdoclint:none. 554 * @return options for doclint 555 */ 556 public List<String> getDocLintOpts() { 557 String xdoclint = options.get(Option.XDOCLINT); 558 String xdoclintCustom = options.get(Option.XDOCLINT_CUSTOM); 559 if (xdoclint == null && xdoclintCustom == null) 560 return List.nil(); 561 562 Set<String> doclintOpts = new LinkedHashSet<>(); 563 if (xdoclint != null) 564 doclintOpts.add(DocLint.XMSGS_OPTION); 565 if (xdoclintCustom != null) { 566 for (String s: xdoclintCustom.split("\\s+")) { 567 if (s.isEmpty()) 568 continue; 569 doclintOpts.add(s.replace(Option.XDOCLINT_CUSTOM.text, DocLint.XMSGS_CUSTOM_PREFIX)); 570 } 571 } 572 573 if (doclintOpts.equals(Collections.singleton(DocLint.XMSGS_CUSTOM_PREFIX + "none"))) 574 return List.nil(); 575 576 String checkPackages = options.get(Option.XDOCLINT_PACKAGE); 577 578 if (checkPackages != null) { 579 for (String s : checkPackages.split("\\s+")) { 580 doclintOpts.add(s.replace(Option.XDOCLINT_PACKAGE.text, DocLint.XCHECK_PACKAGE)); 581 } 582 } 583 584 // standard doclet normally generates H1, H2, 585 // so for now, allow user comments to assume that 586 doclintOpts.add(DocLint.XIMPLICIT_HEADERS + "2"); 587 588 return List.from(doclintOpts.toArray(new String[doclintOpts.size()])); 589 } 590 591 private boolean checkDirectory(Option option) { 592 String value = options.get(option); 593 if (value == null) { 594 return true; 595 } 596 File file = new File(value); 597 if (!file.exists()) { 598 error("err.dir.not.found", value); 599 return false; 600 } 601 if (!file.isDirectory()) { 602 error("err.file.not.directory", value); 603 return false; 604 } 605 return true; 606 } 607 608 private interface ErrorReporter { 609 void report(Option o); 610 } 611 612 void checkOptionAllowed(boolean allowed, ErrorReporter r, Option... opts) { 613 if (!allowed) { 614 Stream.of(opts) 615 .filter(options :: isSet) 616 .forEach(r :: report); 617 } 618 } 619 620 void error(String key, Object... args) { 621 errors = true; 622 switch (errorMode) { 623 case ILLEGAL_ARGUMENT: { 624 String msg = log.localize(PrefixKind.JAVAC, key, args); 625 throw new PropagatedException(new IllegalArgumentException(msg)); 626 } 627 case ILLEGAL_STATE: { 628 String msg = log.localize(PrefixKind.JAVAC, key, args); 629 throw new PropagatedException(new IllegalStateException(msg)); 630 } 631 case LOG: 632 report(key, args); 633 log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); 634 } 635 } 636 637 void warning(String key, Object... args) { 638 report(key, args); 639 } 640 641 private void report(String key, Object... args) { 642 log.printRawLines(ownName + ": " + log.localize(PrefixKind.JAVAC, key, args)); 643 } 644 645 private JavaFileManager getFileManager() { 646 if (fileManager == null) 647 fileManager = context.get(JavaFileManager.class); 648 return fileManager; 649 } 650 651 <T> ListBuffer<T> toList(Iterable<? extends T> items) { 652 ListBuffer<T> list = new ListBuffer<>(); 653 if (items != null) { 654 for (T item : items) { 655 list.add(item); 656 } 657 } 658 return list; 659 } 660 661 <T> Set<T> toSet(Iterable<? extends T> items) { 662 Set<T> set = new LinkedHashSet<>(); 663 if (items != null) { 664 for (T item : items) { 665 set.add(item); 666 } 667 } 668 return set; 669 } 670 }