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