src/share/classes/com/sun/tools/javadoc/Start.java

changeset 1411
467f4f754368
parent 1392
352d130c47c5
child 1413
bdcef2ef52d2
     1.1 --- a/src/share/classes/com/sun/tools/javadoc/Start.java	Thu Nov 15 09:18:36 2012 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/Start.java	Thu Nov 15 14:41:31 2012 -0800
     1.3 @@ -29,7 +29,6 @@
     1.4  import java.io.FileNotFoundException;
     1.5  import java.io.IOException;
     1.6  import java.io.PrintWriter;
     1.7 -import java.util.StringTokenizer;
     1.8  
     1.9  import com.sun.javadoc.*;
    1.10  import com.sun.tools.javac.main.CommandLine;
    1.11 @@ -53,7 +52,7 @@
    1.12   * @author Robert Field
    1.13   * @author Neal Gafter (rewrite)
    1.14   */
    1.15 -class Start {
    1.16 +public class Start extends ToolOption.Helper {
    1.17      /** Context for this invocation. */
    1.18      private final Context context;
    1.19  
    1.20 @@ -65,25 +64,12 @@
    1.21      private static final String standardDocletClassName =
    1.22          "com.sun.tools.doclets.standard.Standard";
    1.23  
    1.24 -    private ListBuffer<String[]> options = new ListBuffer<String[]>();
    1.25 -
    1.26 -    private ModifierFilter showAccess = null;
    1.27 -
    1.28      private long defaultFilter = PUBLIC | PROTECTED;
    1.29  
    1.30      private final Messager messager;
    1.31  
    1.32 -    String docLocale = "";
    1.33 -
    1.34 -    boolean breakiterator = false;
    1.35 -    boolean quiet = false;
    1.36 -    String encoding = null;
    1.37 -
    1.38      private DocletInvoker docletInvoker;
    1.39  
    1.40 -    /* Treat warnings as errors. */
    1.41 -    private boolean rejectWarnings = false;
    1.42 -
    1.43      Start(String programName,
    1.44            PrintWriter errWriter,
    1.45            PrintWriter warnWriter,
    1.46 @@ -132,23 +118,58 @@
    1.47          this(javadocName);
    1.48      }
    1.49  
    1.50 +    public Start(Context context) {
    1.51 +        context.getClass(); // null check
    1.52 +        this.context = context;
    1.53 +        defaultDocletClassName = standardDocletClassName;
    1.54 +        docletParentClassLoader = null;
    1.55 +
    1.56 +        Log log = context.get(Log.logKey);
    1.57 +        if (log instanceof Messager)
    1.58 +            messager = (Messager) log;
    1.59 +        else {
    1.60 +            PrintWriter out = context.get(Log.outKey);
    1.61 +            messager = (out == null) ? new Messager(context, javadocName)
    1.62 +                    : new Messager(context, javadocName, out, out, out);
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66      /**
    1.67       * Usage
    1.68       */
    1.69 -    private void usage() {
    1.70 +    @Override
    1.71 +    void usage() {
    1.72 +        usage(true);
    1.73 +    }
    1.74 +
    1.75 +
    1.76 +    /**
    1.77 +     * Usage
    1.78 +     */
    1.79 +    private void usage(boolean exit) {
    1.80 +        // RFE: it would be better to replace the following with code to
    1.81 +        // write a header, then help for each option, then a footer.
    1.82          messager.notice("main.usage");
    1.83  
    1.84          // let doclet print usage information (does nothing on error)
    1.85          if (docletInvoker != null) {
    1.86              docletInvoker.optionLength("-help");
    1.87          }
    1.88 +
    1.89 +        if (exit) exit();
    1.90 +    }
    1.91 +
    1.92 +    @Override
    1.93 +    void Xusage() {
    1.94 +        Xusage(true);
    1.95      }
    1.96  
    1.97      /**
    1.98       * Usage
    1.99       */
   1.100 -    private void Xusage() {
   1.101 +    private void Xusage(boolean exit) {
   1.102          messager.notice("main.Xusage");
   1.103 +        if (exit) exit();
   1.104      }
   1.105  
   1.106      /**
   1.107 @@ -167,18 +188,18 @@
   1.108  
   1.109          try {
   1.110              failed = !parseAndExecute(argv);
   1.111 -        } catch(Messager.ExitJavadoc exc) {
   1.112 +        } catch (Messager.ExitJavadoc exc) {
   1.113              // ignore, we just exit this way
   1.114          } catch (OutOfMemoryError ee) {
   1.115 -            messager.error(null, "main.out.of.memory");
   1.116 +            messager.error(Messager.NOPOS, "main.out.of.memory");
   1.117              failed = true;
   1.118          } catch (Error ee) {
   1.119              ee.printStackTrace(System.err);
   1.120 -            messager.error(null, "main.fatal.error");
   1.121 +            messager.error(Messager.NOPOS, "main.fatal.error");
   1.122              failed = true;
   1.123          } catch (Exception ee) {
   1.124              ee.printStackTrace(System.err);
   1.125 -            messager.error(null, "main.fatal.exception");
   1.126 +            messager.error(Messager.NOPOS, "main.fatal.exception");
   1.127              failed = true;
   1.128          } finally {
   1.129              messager.exitNotice();
   1.130 @@ -189,15 +210,6 @@
   1.131          return failed ? 1 : 0;
   1.132      }
   1.133  
   1.134 -    private void addToList(ListBuffer<String> list, String str){
   1.135 -        StringTokenizer st = new StringTokenizer(str, ":");
   1.136 -        String current;
   1.137 -        while(st.hasMoreTokens()){
   1.138 -            current = st.nextToken();
   1.139 -            list.append(current);
   1.140 -        }
   1.141 -    }
   1.142 -
   1.143      /**
   1.144       * Main program - internal
   1.145       */
   1.146 @@ -210,7 +222,7 @@
   1.147          try {
   1.148              argv = CommandLine.parse(argv);
   1.149          } catch (FileNotFoundException e) {
   1.150 -            messager.error(null, "main.cant.read", e.getMessage());
   1.151 +            messager.error(Messager.NOPOS, "main.cant.read", e.getMessage());
   1.152              exit();
   1.153          } catch (IOException e) {
   1.154              e.printStackTrace(System.err);
   1.155 @@ -218,116 +230,29 @@
   1.156          }
   1.157  
   1.158          setDocletInvoker(argv);
   1.159 -        ListBuffer<String> subPackages = new ListBuffer<String>();
   1.160 -        ListBuffer<String> excludedPackages = new ListBuffer<String>();
   1.161  
   1.162 -        Options compOpts = Options.instance(context);
   1.163 -        boolean docClasses = false;
   1.164 +        compOpts = Options.instance(context);
   1.165  
   1.166          // Parse arguments
   1.167          for (int i = 0 ; i < argv.length ; i++) {
   1.168              String arg = argv[i];
   1.169 -            if (arg.equals("-subpackages")) {
   1.170 -                oneArg(argv, i++);
   1.171 -                addToList(subPackages, argv[i]);
   1.172 -            } else if (arg.equals("-exclude")){
   1.173 -                oneArg(argv, i++);
   1.174 -                addToList(excludedPackages, argv[i]);
   1.175 -            } else if (arg.equals("-verbose")) {
   1.176 -                setOption(arg);
   1.177 -                compOpts.put("-verbose", "");
   1.178 -            } else if (arg.equals("-encoding")) {
   1.179 -                oneArg(argv, i++);
   1.180 -                encoding = argv[i];
   1.181 -                compOpts.put("-encoding", argv[i]);
   1.182 -            } else if (arg.equals("-breakiterator")) {
   1.183 -                breakiterator = true;
   1.184 -                setOption("-breakiterator");
   1.185 -            } else if (arg.equals("-quiet")) {
   1.186 -                quiet = true;
   1.187 -                setOption("-quiet");
   1.188 -            } else if (arg.equals("-help")) {
   1.189 -                usage();
   1.190 -                exit();
   1.191 -            } else if (arg.equals("-Xclasses")) {
   1.192 -                setOption(arg);
   1.193 -                docClasses = true;
   1.194 -            } else if (arg.equals("-Xwerror")) {
   1.195 -                setOption(arg);
   1.196 -                rejectWarnings = true;
   1.197 -            } else if (arg.equals("-private")) {
   1.198 -                setOption(arg);
   1.199 -                setFilter(ModifierFilter.ALL_ACCESS);
   1.200 -            } else if (arg.equals("-package")) {
   1.201 -                setOption(arg);
   1.202 -                setFilter(PUBLIC | PROTECTED |
   1.203 -                          ModifierFilter.PACKAGE );
   1.204 -            } else if (arg.equals("-protected")) {
   1.205 -                setOption(arg);
   1.206 -                setFilter(PUBLIC | PROTECTED );
   1.207 -            } else if (arg.equals("-public")) {
   1.208 -                setOption(arg);
   1.209 -                setFilter(PUBLIC);
   1.210 -            } else if (arg.equals("-source")) {
   1.211 -                oneArg(argv, i++);
   1.212 -                if (compOpts.get("-source") != null) {
   1.213 -                    usageError("main.option.already.seen", arg);
   1.214 +
   1.215 +            ToolOption o = ToolOption.get(arg);
   1.216 +            if (o != null) {
   1.217 +                // hack: this restriction should be removed
   1.218 +                if (o == ToolOption.LOCALE && i > 0)
   1.219 +                    usageError("main.locale_first");
   1.220 +
   1.221 +                if (o.hasArg) {
   1.222 +                    oneArg(argv, i++);
   1.223 +                    o.process(this, argv[i]);
   1.224 +                } else {
   1.225 +                    setOption(arg);
   1.226 +                    o.process(this);
   1.227                  }
   1.228 -                compOpts.put("-source", argv[i]);
   1.229 -            } else if (arg.equals("-prompt")) {
   1.230 -                compOpts.put("-prompt", "-prompt");
   1.231 -                messager.promptOnError = true;
   1.232 -            } else if (arg.equals("-sourcepath")) {
   1.233 -                oneArg(argv, i++);
   1.234 -                if (compOpts.get("-sourcepath") != null) {
   1.235 -                    usageError("main.option.already.seen", arg);
   1.236 -                }
   1.237 -                compOpts.put("-sourcepath", argv[i]);
   1.238 -            } else if (arg.equals("-classpath")) {
   1.239 -                oneArg(argv, i++);
   1.240 -                if (compOpts.get("-classpath") != null) {
   1.241 -                    usageError("main.option.already.seen", arg);
   1.242 -                }
   1.243 -                compOpts.put("-classpath", argv[i]);
   1.244 -            } else if (arg.equals("-sysclasspath")) {
   1.245 -                oneArg(argv, i++);
   1.246 -                if (compOpts.get("-bootclasspath") != null) {
   1.247 -                    usageError("main.option.already.seen", arg);
   1.248 -                }
   1.249 -                compOpts.put("-bootclasspath", argv[i]);
   1.250 -            } else if (arg.equals("-bootclasspath")) {
   1.251 -                oneArg(argv, i++);
   1.252 -                if (compOpts.get("-bootclasspath") != null) {
   1.253 -                    usageError("main.option.already.seen", arg);
   1.254 -                }
   1.255 -                compOpts.put("-bootclasspath", argv[i]);
   1.256 -            } else if (arg.equals("-extdirs")) {
   1.257 -                oneArg(argv, i++);
   1.258 -                if (compOpts.get("-extdirs") != null) {
   1.259 -                    usageError("main.option.already.seen", arg);
   1.260 -                }
   1.261 -                compOpts.put("-extdirs", argv[i]);
   1.262 -            } else if (arg.equals("-overview")) {
   1.263 -                oneArg(argv, i++);
   1.264 -            } else if (arg.equals("-doclet")) {
   1.265 -                i++;  // handled in setDocletInvoker
   1.266 -            } else if (arg.equals("-docletpath")) {
   1.267 -                i++;  // handled in setDocletInvoker
   1.268 -            } else if (arg.equals("-locale")) {
   1.269 -                if (i != 0)
   1.270 -                    usageError("main.locale_first");
   1.271 -                oneArg(argv, i++);
   1.272 -                docLocale = argv[i];
   1.273 -            } else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) {
   1.274 -                oneArg(argv, i++);
   1.275 -                if (compOpts.get(arg) != null) {
   1.276 -                    usageError("main.option.already.seen", arg);
   1.277 -                }
   1.278 -                compOpts.put(arg, argv[i]);
   1.279 -            } else if (arg.equals("-X")) {
   1.280 -                Xusage();
   1.281 -                exit();
   1.282 +
   1.283              } else if (arg.startsWith("-XD")) {
   1.284 +                // hidden javac options
   1.285                  String s = arg.substring("-XD".length());
   1.286                  int eq = s.indexOf('=');
   1.287                  String key = (eq < 0) ? s : s.substring(0, eq);
   1.288 @@ -336,7 +261,7 @@
   1.289              }
   1.290              // call doclet for its options
   1.291              // other arg starts with - is invalid
   1.292 -            else if ( arg.startsWith("-") ) {
   1.293 +            else if (arg.startsWith("-")) {
   1.294                  int optionLength;
   1.295                  optionLength = docletInvoker.optionLength(arg);
   1.296                  if (optionLength < 0) {
   1.297 @@ -380,12 +305,18 @@
   1.298  
   1.299          LanguageVersion languageVersion = docletInvoker.languageVersion();
   1.300          RootDocImpl root = comp.getRootDocImpl(
   1.301 -                docLocale, encoding, showAccess,
   1.302 -                javaNames.toList(), options.toList(), breakiterator,
   1.303 -                subPackages.toList(), excludedPackages.toList(),
   1.304 +                docLocale,
   1.305 +                encoding,
   1.306 +                showAccess,
   1.307 +                javaNames.toList(),
   1.308 +                options.toList(),
   1.309 +                breakiterator,
   1.310 +                subPackages.toList(),
   1.311 +                excludedPackages.toList(),
   1.312                  docClasses,
   1.313                  // legacy?
   1.314 -                languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1, quiet);
   1.315 +                languageVersion == null || languageVersion == LanguageVersion.JAVA_1_1,
   1.316 +                quiet);
   1.317  
   1.318          // release resources
   1.319          comp = null;
   1.320 @@ -437,15 +368,6 @@
   1.321                                            docletParentClassLoader);
   1.322      }
   1.323  
   1.324 -    private void setFilter(long filterBits) {
   1.325 -        if (showAccess != null) {
   1.326 -            messager.error(null, "main.incompatible.access.flags");
   1.327 -            usage();
   1.328 -            exit();
   1.329 -        }
   1.330 -        showAccess = new ModifierFilter(filterBits);
   1.331 -    }
   1.332 -
   1.333      /**
   1.334       * Set one arg option.
   1.335       * Error and exit if one argument is not provided.
   1.336 @@ -458,22 +380,10 @@
   1.337          }
   1.338      }
   1.339  
   1.340 -    private void usageError(String key) {
   1.341 -        messager.error(null, key);
   1.342 -        usage();
   1.343 -        exit();
   1.344 -    }
   1.345 -
   1.346 -    private void usageError(String key, String a1) {
   1.347 -        messager.error(null, key, a1);
   1.348 -        usage();
   1.349 -        exit();
   1.350 -    }
   1.351 -
   1.352 -    private void usageError(String key, String a1, String a2) {
   1.353 -        messager.error(null, key, a1, a2);
   1.354 -        usage();
   1.355 -        exit();
   1.356 +    @Override
   1.357 +    void usageError(String key, Object... args) {
   1.358 +        messager.error(Messager.NOPOS, key, args);
   1.359 +        usage(true);
   1.360      }
   1.361  
   1.362      /**
   1.363 @@ -502,7 +412,6 @@
   1.364          for (List<String> i = arguments; i.nonEmpty(); i=i.tail) {
   1.365              args[k++] = i.head;
   1.366          }
   1.367 -        options = options.append(args);
   1.368 +        options.append(args);
   1.369      }
   1.370 -
   1.371  }

mercurial