< prev index next >

src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/ToolOptions.java

Print this page

        

@@ -25,395 +25,602 @@
 
 package jdk.javadoc.internal.tool;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.EnumMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import javax.lang.model.element.ElementKind;
 
 import com.sun.tools.javac.main.Option;
 import com.sun.tools.javac.main.Option.InvalidValueException;
-import com.sun.tools.javac.main.Option.OptionKind;
 import com.sun.tools.javac.main.OptionHelper;
+import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.Options;
 
-import static com.sun.tools.javac.main.Option.OptionKind.*;
-import static jdk.javadoc.internal.tool.Main.Result.*;
+import static jdk.javadoc.internal.tool.Main.Result.OK;
+import static jdk.javadoc.internal.tool.ToolOptions.ToolOption.Kind.*;
 
 /**
- * javadoc tool options.
+ * Storage and support for javadoc tool options, as distinct from
+ * the options supported by any doclet that may be in use.
+ * The tool options includes those options which are delegated
+ * to javac and/or the file manager, such as options to set
+ * the source level, and path options to locate the files to be
+ * documented.
  *
  *  <p><b>This is NOT part of any supported API.
  *  If you write code that depends on this, you do so at your own risk.
  *  This code and its internal interfaces are subject to change or
  *  deletion without notice.</b>
  */
-public enum ToolOption {
+public class ToolOptions {
+    // The following are the names of options handled in the first pass of option decoding,
+    // in Start.preprocess.
+    static final String DOCLET = "-doclet";
+    static final String DOCLET_PATH = "-docletpath";
+    static final String DUMP_ON_ERROR = "--dump-on-error";
+    static final String J = "-J";
+    static final String LOCALE = "-locale";
+
+    /**
+     * Argument for command-line option {@code -breakiterator}.
+     */
+    private boolean breakIterator = false;
+
+    /**
+     * Argument for command-line option {@code -locale}.
+     */
+    private String docLocale = "";
+
+    /**
+     * Argument for command-line option {@code --dump-on-error}.
+     * Dump stack traces for debugging etc.
+     * Similar to javac {@code -doe}.
+     */
+    private boolean dumpOnError = false;
+
+    /**
+     * Argument for command-line option {@code -exclude}.
+     */
+    private List<String> excludes = new ArrayList<>();
+
+    /**
+     * Argument for command-line option {@code --expand-requires}.
+     */
+    private AccessKind expandRequires;
+
+    /**
+     * Argument for command-line option {@code --ignore-source-errors}.
+     */
+    private boolean ignoreSourceErrors;
+
+    /**
+     * Argument for command-line option {@code --module}.
+     */
+    private List<String> modules = new ArrayList<>();
+
+    /**
+     * Argument for command-line option {@code -Xwerror}.
+     * Set by -Xwerror.
+     */
+    private boolean rejectWarnings = false;
+
+    /**
+     * Argument for command-line option {@code --show-members}.
+     */
+    private AccessKind showMembersAccess;
+
+    /**
+     * Argument for command-line option {@code --show-types}.
+     */
+    private AccessKind showTypesAccess;
+
+    /**
+     * Argument for command-line option {@code --show-packages}.
+     */
+    private AccessKind showPackagesAccess;
+
+    /**
+     * Argument for command-line option {@code --show-module-contents}.
+     */
+    private AccessKind showModuleContents;
+
+    /**
+     * Argument for command-line option {@code -quiet}.
+     */
+    private boolean quiet;
+
+    /**
+     * Argument for command-line option {@code -subpackages}.
+     */
+    private List<String> subpackages = new ArrayList<>();
+
+    /**
+     * Argument for command-line option {@code -verbose}.
+     */
+    private boolean verbose;
+
+    /**
+     * Argument for command-line option {@code -xclasses}.
+     * If true, names on the command line that would normally be
+     * treated as package names are treated as class names instead.
+     */
+    private boolean xclasses = false;
+
+    /**
+     * Options to be given to the file manager, such as path options
+     * indicating where to find files to be documented.
+     */
+    private final Map<Option, String> fileManagerOpts;
+
+    /**
+     * Options to be given to the underlying compiler front-end,
+     * such as options to indicate the source level to be used.
+     */
+    private final Options compOpts;
+
+    /**
+     * The "helper" to be used when processing compiler options.
+     */
+    private final OptionHelper compilerOptionHelper;
+
+    /**
+     * The messager to be used to report diagnostics..
+     */
+    private final Messager messager;
+
+    /**
+     * The helper for help and version options
+     */
+    private final ShowHelper showHelper;
+
+    /**
+     * Creates an object to handle tool options.
+     *
+     * @param context the context used to find other tool-related components
+     * @param messager the messager to be used to report diagnostics
+     */
+    ToolOptions(Context context, Messager messager, ShowHelper showHelper) {
+        this.messager = messager;
+        this.showHelper = showHelper;
+        compOpts = Options.instance(context);
+        fileManagerOpts = new LinkedHashMap<>();
+        compilerOptionHelper = getOptionHelper();
+        setAccessDefault();
+    }
+
+    /**
+     * Creates a minimal object, just sufficient to check the names of the
+     * supported options.
+     */
+    private ToolOptions() {
+        compOpts = null;
+        compilerOptionHelper = null;
+        fileManagerOpts = null;
+        messager = null;
+        showHelper = null;
+    }
+
+    /**
+     * Returns the set of options supported by the tool, excluding any options
+     * that are managed by the doclet that may be in use.
+     *
+     * @return the set of options
+     */
+    public List<ToolOption> getSupportedOptions() {
+        return supportedOptions;
+    }
+
+    /**
+     * Determines if the given option is supported and if so, the
+     * number of arguments the option takes.
+     *
+     * @param option an option
+     * @return the number of arguments the given option takes or -1 if
+     * the option is not supported
+     * @see javax.tools.DocumentationTool#isSupportedOption(String)
+     */
+    public static int isSupportedOption(String option) {
+        ToolOptions t = new ToolOptions();
+        for (ToolOption o : t.supportedOptions) {
+            for (String name : o.names) {
+                if (name.equals(option))
+                    return o.hasArg ? 1 : 0;
+            }
+        }
+        return -1;
+    }
 
+    /**
+     * Returns the option to be used to process an argument such as may be found on
+     * the command line.
+     *
+     * @param arg the argument
+     * @return the option
+     */
+    ToolOption getOption(String arg) {
+        String name = arg;
+        if (arg.startsWith("--") && arg.contains("=")) {
+            name = arg.substring(0, arg.indexOf('='));
+        }
+        for (ToolOption o : supportedOptions) {
+            for (String n : o.names) {
+                if (name.equals(n)) {
+                    return o;
+                }
+            }
+        }
+        return null;
+    }
+
+    private List<ToolOption> supportedOptions = List.of(
     // ----- options for underlying compiler -----
 
-    BOOTCLASSPATH("-bootclasspath", STANDARD, true) {
+            new ToolOption("-bootclasspath", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.BOOT_CLASS_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.BOOT_CLASS_PATH, primaryName, arg);
         }
     },
 
-    CLASS_PATH("--class-path -classpath -cp", STANDARD, true) {
+            new ToolOption("--class-path -classpath -cp", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.CLASS_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.CLASS_PATH, primaryName, arg);
         }
     },
 
-    EXTDIRS("-extdirs", STANDARD, true) {
+            new ToolOption("-extdirs", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.EXTDIRS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.EXTDIRS, primaryName, arg);
         }
     },
 
-    SOURCE_PATH("--source-path -sourcepath", STANDARD, true) {
+            new ToolOption("--source-path -sourcepath", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.SOURCE_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.SOURCE_PATH, primaryName, arg);
         }
     },
 
-    MODULE_SOURCE_PATH("--module-source-path", STANDARD, true) {
+            new ToolOption("--module-source-path", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.MODULE_SOURCE_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.MODULE_SOURCE_PATH, primaryName, arg);
         }
     },
 
-    UPGRADE_MODULE_PATH("--upgrade-module-path", STANDARD, true) {
+            new ToolOption("--upgrade-module-path", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.UPGRADE_MODULE_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.UPGRADE_MODULE_PATH, primaryName, arg);
         }
     },
 
-    SYSTEM("--system", STANDARD, true) {
+            new ToolOption("--system", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.SYSTEM.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.SYSTEM, primaryName, arg);
         }
     },
 
-    MODULE_PATH("--module-path -p", STANDARD, true) {
+            new ToolOption("--module-path -p", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.MODULE_PATH.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.MODULE_PATH, primaryName, arg);
         }
     },
 
-    ADD_MODULES("--add-modules", STANDARD, true) {
+            new ToolOption("--add-modules", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.ADD_MODULES.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.ADD_MODULES, primaryName, arg);
         }
     },
 
-    LIMIT_MODULES("--limit-modules", STANDARD, true) {
+            new ToolOption("--limit-modules", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.LIMIT_MODULES.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.LIMIT_MODULES, primaryName, arg);
         }
     },
 
-    MODULE("--module", STANDARD, true) {
+            new ToolOption("--module", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) {
-            helper.addToList(this, ",", arg);
+                public void process(String arg) {
+                    modules.addAll(List.of(arg.split(",")));
         }
     },
 
-    ENCODING("-encoding", STANDARD, true) {
+            new ToolOption("-encoding", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.ENCODING.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.ENCODING, primaryName, arg);
         }
     },
 
-    RELEASE("--release", STANDARD, true) {
+            new ToolOption("--release", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.RELEASE.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.RELEASE, primaryName, arg);
         }
     },
 
-    SOURCE("--source -source", STANDARD, true) {
+            new ToolOption("--source -source", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.SOURCE.process(helper.getOptionHelper(), primaryName, arg);
-            Option.TARGET.process(helper.getOptionHelper(), Option.TARGET.primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.SOURCE, primaryName, arg);
+                    processCompilerOption(Option.TARGET, Option.TARGET.primaryName, arg);
         }
     },
 
-    XMAXERRS("-Xmaxerrs", EXTENDED, true) {
+            new ToolOption("-Xmaxerrs", EXTENDED, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.XMAXERRS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.XMAXERRS, primaryName, arg);
         }
     },
 
-    XMAXWARNS("-Xmaxwarns", EXTENDED, true) {
+            new ToolOption("-Xmaxwarns", EXTENDED, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.XMAXWARNS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.XMAXWARNS, primaryName, arg);
         }
     },
 
-    ADD_READS("--add-reads", EXTENDED, true) {
+            new ToolOption("--add-reads", EXTENDED, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.ADD_READS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.ADD_READS, primaryName, arg);
         }
     },
 
-    ADD_EXPORTS("--add-exports", EXTENDED, true) {
+            new ToolOption("--add-exports", EXTENDED, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.ADD_EXPORTS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.ADD_EXPORTS, primaryName, arg);
         }
     },
 
-    PATCH_MODULE("--patch-module", EXTENDED, true) {
+            new ToolOption("--patch-module", EXTENDED, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.PATCH_MODULE.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.PATCH_MODULE, primaryName, arg);
         }
     },
 
-    ADD_OPENS("--add-opens", HIDDEN, true) {
+            new ToolOption("--add-opens", HIDDEN, true) {
         @Override
-        public void process(Helper helper, String arg) throws InvalidValueException {
-            Option.ADD_OPENS.process(helper.getOptionHelper(), primaryName, arg);
+                public void process(String arg) throws InvalidValueException {
+                    processCompilerOption(Option.ADD_OPENS, primaryName, arg);
         }
     },
 
-    ENABLE_PREVIEW("--enable-preview", STANDARD) {
+            new ToolOption("--enable-preview", STANDARD) {
         @Override
-        public void process(Helper helper) throws InvalidValueException {
-            Option.PREVIEW.process(helper.getOptionHelper(), primaryName);
+                public void process() throws InvalidValueException {
+                    processCompilerOption(Option.PREVIEW, primaryName);
         }
     },
 
     // ----- doclet options -----
 
-    DOCLET("-doclet", STANDARD, true), // handled in setDocletInvoker
+            new ToolOption(DOCLET, STANDARD, true), // handled in setDocletInvoker
 
-    DOCLETPATH("-docletpath", STANDARD, true), // handled in setDocletInvoker
+            new ToolOption(DOCLET_PATH, STANDARD, true), // handled in setDocletInvoker
 
     // ----- selection options -----
 
-    SUBPACKAGES("-subpackages", STANDARD, true) {
+            new ToolOption("-subpackages", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) {
-            helper.addToList(this, ":", arg);
+                public void process(String arg) {
+                    subpackages.addAll(List.of(arg.split(":")));
         }
     },
 
-    EXCLUDE("-exclude", STANDARD, true) {
+            new ToolOption("-exclude", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) {
-            helper.addToList(this, ":", arg);
+                public void process(String arg) {
+                    excludes.addAll(List.of(arg.split(":")));
         }
     },
 
     // ----- filtering options -----
 
-    PACKAGE("-package", STANDARD) {
+            new ToolOption("-package", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            helper.setSimpleFilter("package");
+                public void process() throws OptionException {
+                    setSimpleFilter("package");
         }
     },
 
-    PRIVATE("-private", STANDARD) {
+            new ToolOption("-private", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            helper.setSimpleFilter("private");
+                public void process() throws OptionException {
+                    setSimpleFilter("private");
         }
     },
 
-    PROTECTED("-protected", STANDARD) {
+            new ToolOption("-protected", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            helper.setSimpleFilter("protected");
+                public void process() throws OptionException {
+                    setSimpleFilter("protected");
         }
     },
 
-    PUBLIC("-public", STANDARD) {
+            new ToolOption("-public", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            helper.setSimpleFilter("public");
+                public void process() throws OptionException {
+                    setSimpleFilter("public");
         }
     },
 
-    SHOW_MEMBERS("--show-members", STANDARD, true) {
+            new ToolOption("--show-members", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws OptionException {
-            helper.setFilter(this, arg);
+                public void process(String arg) throws OptionException {
+                    setShowMembersAccess(arg);
         }
     },
 
-    SHOW_TYPES("--show-types", STANDARD, true) {
+            new ToolOption("--show-types", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws OptionException {
-            helper.setFilter(this, arg);
+                public void process(String arg) throws OptionException {
+                    setShowTypesAccess(arg);
         }
     },
 
-    SHOW_PACKAGES("--show-packages", STANDARD, true) {
+            new ToolOption("--show-packages", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws OptionException {
-            helper.setShowPackageAccess(SHOW_PACKAGES, arg);
+                public void process(String arg) throws OptionException {
+                    setShowPackageAccess(arg);
         }
     },
 
-    SHOW_MODULE_CONTENTS("--show-module-contents", STANDARD, true) {
+            new ToolOption("--show-module-contents", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws OptionException {
-            helper.setShowModuleContents(SHOW_MODULE_CONTENTS, arg);
+                public void process(String arg) throws OptionException {
+                    setShowModuleContents(arg);
         }
     },
 
-    EXPAND_REQUIRES("--expand-requires", STANDARD, true) {
+            new ToolOption("--expand-requires", STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) throws OptionException {
-            helper.setExpandRequires(EXPAND_REQUIRES, arg);
+                public void process(String arg) throws OptionException {
+                    setExpandRequires(arg);
         }
     },
 
     // ----- output control options -----
 
-    QUIET("-quiet", STANDARD) {
+            new ToolOption("-quiet", STANDARD) {
         @Override
-        public void process(Helper helper) {
-            helper.jdtoolOpts.put(QUIET, true);
+                public void process() {
+                    quiet = true;
         }
     },
 
-    VERBOSE("-verbose", STANDARD) {
+            new ToolOption("-verbose", STANDARD) {
         @Override
-        public void process(Helper helper) {
-            helper.compOpts.put("-verbose", "");
+                public void process() {
+                    setVerbose();
         }
     },
 
-    XWERROR("-Xwerror", HIDDEN) {
+            new ToolOption("-Xwerror", HIDDEN) {
         @Override
-        public void process(Helper helper) {
-            helper.rejectWarnings = true;
-
+                public void process() {
+                    rejectWarnings = true;
         }
     },
 
     // ----- other options -----
 
-    BREAKITERATOR("-breakiterator", STANDARD) {
+            new ToolOption("-breakiterator", STANDARD) {
         @Override
-        public void process(Helper helper) {
-            helper.breakiterator = true;
+                public void process() {
+                    breakIterator = true;
         }
     },
 
-    LOCALE("-locale", STANDARD, true) {
+            new ToolOption(LOCALE, STANDARD, true) {
         @Override
-        public void process(Helper helper, String arg) {
-            helper.docLocale = arg;
+                public void process(String arg) {
+                    docLocale = arg;
         }
     },
 
-    XCLASSES("-Xclasses", HIDDEN) {
+            new ToolOption("-Xclasses", HIDDEN) {
         @Override
-        public void process(Helper helper) {
-            helper.jdtoolOpts.put(XCLASSES, true);
+                public void process() {
+                    xclasses = true;
         }
     },
 
-    DUMPONERROR("--dump-on-error", HIDDEN) {
+            new ToolOption(DUMP_ON_ERROR, HIDDEN) {
         @Override
-        public void process(Helper helper) {
-            helper.dumpOnError = true;
+                public void process() {
+                    dumpOnError = true;
         }
     },
 
-    IGNORE_SOURCE_ERRORS("--ignore-source-errors", HIDDEN) {
+            new ToolOption("--ignore-source-errors", HIDDEN) {
         @Override
-        public void process(Helper helper) {
-            helper.jdtoolOpts.put(IGNORE_SOURCE_ERRORS, true);
+                public void process() {
+                    ignoreSourceErrors = true;
         }
     },
 
     // ----- help options -----
 
-    HELP("--help -help -? -h", STANDARD) {
+            new ToolOption("--help -help -? -h", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            throw new OptionException(OK, helper::usage);
+                public void process() throws OptionException {
+                    throw new OptionException(OK, showHelper::usage);
         }
     },
 
-    HELP_EXTRA("--help-extra -X", STANDARD) {
+            new ToolOption("--help-extra -X", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-           throw new OptionException(OK, helper::Xusage);
+                public void process() throws OptionException {
+                    throw new OptionException(OK, showHelper::Xusage);
         }
     },
 
     // This option exists only for the purpose of documenting itself.
     // It's actually implemented by the launcher.
-    J("-J", STANDARD, true) {
+            new ToolOption(J, STANDARD, true) {
         @Override
-        public void process(Helper helper) {
+                public void process() {
             throw new AssertionError("the -J flag should be caught by the launcher.");
         }
     },
 
-    VERSION("--version", STANDARD) {
+            new ToolOption("--version", STANDARD) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            throw new OptionException(OK, helper::version);
+                public void process() throws OptionException {
+                    throw new OptionException(OK, showHelper::version);
         }
     },
 
-    FULLVERSION("--full-version", HIDDEN) {
+            new ToolOption("--full-version", HIDDEN) {
         @Override
-        public void process(Helper helper) throws OptionException {
-            throw new OptionException(OK, helper::fullVersion);
+                public void process() throws OptionException {
+                    throw new OptionException(OK, showHelper::fullVersion);
         }
-    };
+            });
+
+    /**
+     * Base class for all supported tool options.
+     */
+    static class ToolOption {
+        enum Kind { STANDARD, EXTENDED, HIDDEN }
 
-    public final String primaryName;
-    public final List<String> names;
-    public final OptionKind kind;
-    public final boolean hasArg;
-    public final boolean hasSuffix; // ex: foo:bar or -foo=bar
+        final String primaryName;
+        final List<String> names;
+        final Kind kind;
+        final boolean hasArg;
+        final boolean hasSuffix; // ex: foo:bar or -foo=bar
 
-    ToolOption(String opt, OptionKind kind) {
+        ToolOption(String opt, Kind kind) {
         this(opt, kind, false);
     }
 
-    ToolOption(String names, OptionKind kind, boolean hasArg) {
+        ToolOption(String names, Kind kind, boolean hasArg) {
         this.names = Arrays.asList(names.split("\\s+"));
         this.primaryName = this.names.get(0);
         this.kind = kind;
         this.hasArg = hasArg;
         char lastChar = names.charAt(names.length() - 1);
         this.hasSuffix = lastChar == ':' || lastChar == '=';
     }
 
-    void process(Helper helper, String arg) throws OptionException, Option.InvalidValueException { }
+        void process(String arg) throws OptionException, Option.InvalidValueException { }
 
-    void process(Helper helper) throws OptionException, Option.InvalidValueException { }
+        void process() throws OptionException, Option.InvalidValueException { }
 
     List<String> getNames() {
         return names;
     }
 

@@ -433,131 +640,298 @@
                 .replaceAll("^-*", "")              // remove leading '-'
                 .replaceAll("[^A-Za-z0-9]+$", "")   // remove trailing non-alphanumeric
                 .replaceAll("[^A-Za-z0-9]", ".")    // replace internal non-alphanumeric
                 + suffix;
     }
+    }
 
+    interface ShowHelper {
+        /**
+         * Show command-line help for the standard options, as requested by
+         * the {@code --help} option and its aliases.
+         */
+        void usage();
+
+        /**
+         * Show command-line help for the extended options, as requested by
+         * the {@code --help-extended} option and its aliases.
+         */
+        void Xusage();
+
+        /**
+         * Show the basic version information, as requested by the {@code --version} option.
+         */
+        void version();
 
-    static ToolOption get(String name) {
-        String oname = name;
-        if (name.startsWith("--") && name.contains("=")) {
-            oname = name.substring(0, name.indexOf('='));
+        /**
+         * Show the full version information, as requested by the {@code --full-version} option.
+         */
+        void fullVersion();
         }
-        for (ToolOption o : values()) {
-            for (String n : o.names) {
-                if (oname.equals(n)) {
-                    return o;
+
+    //<editor-fold desc="accessor methods">
+    /**
+     * Argument for command-line option {@code -breakiterator}.
+     */
+    boolean breakIterator() {
+        return breakIterator;
                 }
+
+    /**
+     * Argument for command-line option {@code -locale}.
+     */
+    String locale() {
+        return docLocale;
             }
+
+    /**
+     * Argument for command-line option {@code --dump-on-error}.
+     * Dump stack traces for debugging etc.
+     * Similar to javac {@code -doe}.
+     */
+    boolean dumpOnError() {
+        return dumpOnError;
         }
-        return null;
+
+    void setDumpOnError(boolean v) {
+        dumpOnError = true;
     }
 
-    abstract static class Helper {
+    /**
+     * Argument for command-line option {@code -exclude}.
+     */
+    List<String> excludes() {
+        return excludes;
+    }
 
-        // File manager options
-        final Map<Option, String> fileManagerOpts = new LinkedHashMap<>();
+    /**
+     * Argument for command-line option {@code --expand-requires}.
+     */
+    AccessKind expandRequires() {
+        return expandRequires;
+    }
 
-        /** javac options, set by various options. */
-        Options compOpts; // = Options.instance(context)
+    /**
+     * Argument for command-line option {@code --ignore-source-errors}.
+     */
+    boolean ignoreSourceErrors() {
+        return ignoreSourceErrors;
+    }
 
-        /** Javadoc tool options */
-        final Map<ToolOption, Object> jdtoolOpts = new EnumMap<>(ToolOption.class);
+    /**
+     * Argument for command-line option {@code --module}.
+     */
+    List<String> modules() {
+        return modules;
+    }
 
-        /** dump stack traces for debugging etc.*/
-        boolean dumpOnError = false;
+    /**
+     * Argument for command-line option {@code -Xwerror}.
+     * Set by -Xwerror.
+     */
+    boolean rejectWarnings() {
+        return rejectWarnings;
+    }
 
-        /** Set by -breakiterator. */
-        boolean breakiterator = false;
+    /**
+     * Argument for command-line option {@code --show-members}.
+     */
+    AccessKind showMembersAccess() {
+        return showMembersAccess;
+    }
 
-        /** Set by -Xwerror. */
-        boolean rejectWarnings = false;
+    /**
+     * Argument for command-line option {@code --show-types}.
+     */
+    AccessKind showTypesAccess() {
+        return showTypesAccess;
+    }
 
-        /** Set by -prompt. */
-        boolean promptOnError;
+    /**
+     * Argument for command-line option {@code --show-packages}.
+     */
+    AccessKind showPackagesAccess() {
+        return showPackagesAccess;
+    }
 
-        /** Set by -locale. */
-        String docLocale = "";
+    /**
+     * Argument for command-line option {@code --show-module-contents}.
+     */
+    AccessKind showModuleContents() {
+        return showModuleContents;
+    }
 
-        Helper() {
-            populateDefaultAccessMap();
+    /**
+     * Argument for command-line option {@code -quiet}.
+     */
+    boolean quiet() {
+        return quiet;
         }
 
-        abstract void usage();
-        abstract void Xusage();
+    /**
+     * Argument for command-line option {@code -subpackages}.
+     */
+    List<String> subpackages() {
+        return subpackages;
+    }
 
-        abstract void version();
-        abstract void fullVersion();
+    /**
+     * Argument for command-line option {@code -verbose}.
+     */
+    boolean verbose() {
+        return verbose;
+    }
 
-        abstract String getLocalizedMessage(String msg, Object... args);
+    /**
+     * Argument for command-line option {@code -xclasses}.
+     * If true, names on the command line that would normally be
+     * treated as package names are treated as class names instead.
+     */
+    boolean xclasses() {
+        return xclasses;
+    }
 
-        abstract OptionHelper getOptionHelper();
+    /**
+     * Returns the set of options to be used for the instance of the
+     * underlying compiler front-end.
+     *
+     * @return the options
+     */
+    Options compilerOptions() {
+        return compOpts;
+    }
 
-        @SuppressWarnings("unchecked")
-        void addToList(ToolOption opt, String delimiter, String str) {
-            List<String> list = (List<String>) jdtoolOpts.computeIfAbsent(opt, v -> new ArrayList<>());
-            list.addAll(Arrays.asList(str.split(delimiter)));
-            jdtoolOpts.put(opt, list);
+    /**
+     * Returns the set of options to be used for the file manager.
+     *
+     * @return the options
+     */
+    Map<Option, String> fileManagerOptions() {
+        return fileManagerOpts;
         }
+    //</editor-fold>
 
-        void setExpandRequires(ToolOption opt, String arg) throws OptionException {
+    /**
+     * Returns an {@code IllegalOptionValue} exception.
+     *
+     * @param arg the arghument to include in the detail message
+     * @return the exception
+     */
+    private IllegalOptionValue illegalOptionValue(String arg) {
+        return new IllegalOptionValue(showHelper::usage, messager.getText("main.illegal_option_value", arg));
+    }
+
+    /**
+     * Process a compiler option.
+     *
+     * @param option the option object to process the command-line option
+     * @param opt    the command-line option
+     * @throws Option.InvalidValueException if the command-line option is invalid
+     */
+    void processCompilerOption(Option option, String opt) throws Option.InvalidValueException {
+        option.process(compilerOptionHelper, opt);
+    }
+
+    /**
+     * Process a compiler option.
+     *
+     * @param option the option object to process the command-line option
+     * @param opt    the command-line option
+     * @param arg    the argument for the command-line option
+     * @throws Option.InvalidValueException if the command-line option is invalid
+     */
+    private void processCompilerOption(Option option, String opt, String arg) throws Option.InvalidValueException {
+        option.process(compilerOptionHelper, opt, arg);
+    }
+
+    /**
+     * Returns a "helper" to be used when processing compiler options.
+     * @return the helper
+     */
+    private OptionHelper getOptionHelper() {
+        return new OptionHelper.GrumpyHelper(messager) {
+            @Override
+            public String get(com.sun.tools.javac.main.Option option) {
+                return compOpts.get(option);
+            }
+
+            @Override
+            public void put(String name, String value) {
+                compOpts.put(name, value);
+            }
+
+            @Override
+            public void remove(String name) {
+                compOpts.remove(name);
+            }
+
+            @Override
+            public boolean handleFileManagerOption(com.sun.tools.javac.main.Option option, String value) {
+                fileManagerOpts.put(option, value);
+                return true;
+            }
+        };
+    }
+
+    private void setExpandRequires(String arg) throws OptionException {
             switch (arg) {
                 case "transitive":
-                    jdtoolOpts.put(opt, AccessKind.PUBLIC);
+                expandRequires = AccessKind.PUBLIC;
                     break;
                 case "all":
-                    jdtoolOpts.put(opt, AccessKind.PRIVATE);
+                expandRequires = AccessKind.PRIVATE;
                     break;
                 default:
-                    String text = getLocalizedMessage("main.illegal_option_value", arg);
-                    throw new IllegalOptionValue(this::usage, text);
+                throw illegalOptionValue(arg);
             }
         }
 
-        void setShowModuleContents(ToolOption opt, String arg) throws OptionException {
+    private void setShowModuleContents(String arg) throws OptionException {
             switch (arg) {
                 case "api":
-                    jdtoolOpts.put(opt, AccessKind.PUBLIC);
+                showModuleContents = AccessKind.PUBLIC;
                     break;
                 case "all":
-                    jdtoolOpts.put(opt, AccessKind.PRIVATE);
+                showModuleContents = AccessKind.PRIVATE;
                     break;
                 default:
-                    String text = getLocalizedMessage("main.illegal_option_value", arg);
-                    throw new IllegalOptionValue(this::usage, text);
+                throw illegalOptionValue(arg);
             }
         }
 
-        void setShowPackageAccess(ToolOption opt, String arg) throws OptionException {
+    private void setShowPackageAccess(String arg) throws OptionException {
             switch (arg) {
                 case "exported":
-                    jdtoolOpts.put(opt, AccessKind.PUBLIC);
+                showPackagesAccess = AccessKind.PUBLIC;
                     break;
                 case "all":
-                    jdtoolOpts.put(opt, AccessKind.PRIVATE);
+                showPackagesAccess = AccessKind.PRIVATE;
                     break;
                 default:
-                    String text = getLocalizedMessage("main.illegal_option_value", arg);
-                    throw new IllegalOptionValue(this::usage, text);
+                throw illegalOptionValue(arg);
             }
         }
 
+    private void setShowTypesAccess(String arg) throws OptionException {
+        showTypesAccess = getAccessValue(arg);
+    }
 
-        void setFilter(ToolOption opt, String arg) throws OptionException {
-            jdtoolOpts.put(opt, getAccessValue(arg));
+    private void setShowMembersAccess(String arg) throws OptionException {
+        showMembersAccess = getAccessValue(arg);
         }
 
-        void setSimpleFilter(String arg) throws OptionException {
-            handleSimpleOption(arg);
+    private void setSimpleFilter(String arg) throws OptionException {
+        setSimpleAccessOption(arg);
         }
 
-        void setFileManagerOpt(Option opt, String arg) {
-            fileManagerOpts.put(opt, arg);
+    private void setVerbose() {
+        compOpts.put("-verbose", "");
+        verbose = true;
         }
 
-        void handleSimpleOption(String arg) throws OptionException {
-            populateSimpleAccessMap(getAccessValue(arg));
+    private void setSimpleAccessOption(String arg) throws OptionException {
+        setAccess(getAccessValue(arg));
         }
 
         /*
          * This method handles both the simple options -package,
          * -private, so on, in addition to the new ones such as

@@ -576,43 +950,41 @@
                 case "package":
                     return AccessKind.PACKAGE;
                 case "private":
                     return AccessKind.PRIVATE;
                 default:
-                    String text = getLocalizedMessage("main.illegal_option_value", value);
-                    throw new IllegalOptionValue(this::usage, text);
+                throw illegalOptionValue(value);
             }
         }
 
         /*
-         * Sets the entire kind map to PROTECTED this is the default.
+     * Sets all access members to PROTECTED; this is the default.
          */
-        private void populateDefaultAccessMap() {
-            populateSimpleAccessMap(AccessKind.PROTECTED);
+    private void setAccessDefault() {
+        setAccess(AccessKind.PROTECTED);
         }
 
         /*
          * This sets access to all the allowed kinds in the
-         * access map.
+     * access members.
          */
-        void populateSimpleAccessMap(AccessKind accessValue) {
+    private void setAccess(AccessKind accessValue) {
             for (ElementKind kind : ElementsTable.ModifierFilter.ALLOWED_KINDS) {
                 switch (kind) {
                     case METHOD:
-                        jdtoolOpts.put(SHOW_MEMBERS, accessValue);
+                    showMembersAccess = accessValue;
                         break;
                     case CLASS:
-                        jdtoolOpts.put(SHOW_TYPES, accessValue);
+                    showTypesAccess = accessValue;
                         break;
                     case PACKAGE:
-                        jdtoolOpts.put(SHOW_PACKAGES, accessValue);
+                    showPackagesAccess = accessValue;
                         break;
                     case MODULE:
-                        jdtoolOpts.put(SHOW_MODULE_CONTENTS, accessValue);
+                    showModuleContents = accessValue;
                         break;
                     default:
                         throw new AssertionError("unknown element kind:" + kind);
                 }
             }
         }
-    }
 }
< prev index next >