6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
authorjjg
Fri, 11 Jun 2010 17:24:23 -0700
changeset 5850 6f095ff5b469
parent 5849 04edd7910585
child 5851 e9859eddcc92
child 5852 f81fe39e30dc
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns Reviewed-by: darcy
langtools/src/share/classes/com/sun/tools/javac/util/Log.java
langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java
langtools/src/share/classes/com/sun/tools/javadoc/Messager.java
langtools/src/share/classes/com/sun/tools/javadoc/Start.java
langtools/src/share/classes/com/sun/tools/javadoc/resources/javadoc.properties
langtools/test/tools/javadoc/6958836/Test.java
langtools/test/tools/javadoc/6958836/errs/Errors.java
langtools/test/tools/javadoc/6958836/warns/Warnings.java
--- a/langtools/src/share/classes/com/sun/tools/javac/util/Log.java	Fri Jun 11 07:12:07 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/Log.java	Fri Jun 11 17:24:23 2010 -0700
@@ -125,8 +125,8 @@
         this.promptOnError = options.get("-prompt") != null;
         this.emitWarnings = options.get("-Xlint:none") == null;
         this.suppressNotes = options.get("suppressNotes") != null;
-        this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100);
-        this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100);
+        this.MaxErrors = getIntOption(options, "-Xmaxerrs", getDefaultMaxErrors());
+        this.MaxWarnings = getIntOption(options, "-Xmaxwarns", getDefaultMaxWarnings());
 
         boolean rawDiagnostics = options.get("rawDiagnostics") != null;
         messages = JavacMessages.instance(context);
@@ -155,6 +155,18 @@
             return defaultValue;
         }
 
+        /** Default value for -Xmaxerrs.
+         */
+        protected int getDefaultMaxErrors() {
+            return 100;
+        }
+
+        /** Default value for -Xmaxwarns.
+         */
+        protected int getDefaultMaxWarnings() {
+            return 100;
+        }
+
     /** The default writer for diagnostics
      */
     static final PrintWriter defaultWriter(Context context) {
--- a/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Fri Jun 11 07:12:07 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Fri Jun 11 17:24:23 2010 -0700
@@ -155,10 +155,8 @@
     public boolean start(RootDoc root) {
         Object retVal;
         String methodName = "start";
-        Class<?>[] paramTypes = new Class<?>[1];
-        Object[] params = new Object[1];
-        paramTypes[0] = RootDoc.class;
-        params[0] = root;
+        Class<?>[] paramTypes = { RootDoc.class };
+        Object[] params = { root };
         try {
             retVal = invoke(methodName, null, paramTypes, params);
         } catch (DocletInvokeException exc) {
@@ -181,10 +179,8 @@
     public int optionLength(String option) {
         Object retVal;
         String methodName = "optionLength";
-        Class<?>[] paramTypes = new Class<?>[1];
-        Object[] params = new Object[1];
-        paramTypes[0] = option.getClass();
-        params[0] = option;
+        Class<?>[] paramTypes = { String.class };
+        Object[] params = { option };
         try {
             retVal = invoke(methodName, new Integer(0), paramTypes, params);
         } catch (DocletInvokeException exc) {
@@ -208,12 +204,8 @@
         String options[][] = optlist.toArray(new String[optlist.length()][]);
         String methodName = "validOptions";
         DocErrorReporter reporter = messager;
-        Class<?>[] paramTypes = new Class<?>[2];
-        Object[] params = new Object[2];
-        paramTypes[0] = options.getClass();
-        paramTypes[1] = DocErrorReporter.class;
-        params[0] = options;
-        params[1] = reporter;
+        Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class };
+        Object[] params = { options, reporter };
         try {
             retVal = invoke(methodName, Boolean.TRUE, paramTypes, params);
         } catch (DocletInvokeException exc) {
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Messager.java	Fri Jun 11 07:12:07 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Messager.java	Fri Jun 11 17:24:23 2010 -0700
@@ -86,7 +86,7 @@
         private static final long serialVersionUID = 0;
     }
 
-    private final String programName;
+    final String programName;
 
     private ResourceBundle messageRB = null;
 
@@ -121,6 +121,16 @@
         this.programName = programName;
     }
 
+    @Override
+    protected int getDefaultMaxErrors() {
+        return Integer.MAX_VALUE;
+    }
+
+    @Override
+    protected int getDefaultMaxWarnings() {
+        return Integer.MAX_VALUE;
+    }
+
     /**
      * Reset resource bundle, eg. locale has changed.
      */
@@ -231,11 +241,13 @@
      * @param msg message to print
      */
     public void printError(SourcePosition pos, String msg) {
-        String prefix = (pos == null) ? programName : pos.toString();
-        errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
-        errWriter.flush();
-        prompt();
-        nerrors++;
+        if (nerrors < MaxErrors) {
+            String prefix = (pos == null) ? programName : pos.toString();
+            errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
+            errWriter.flush();
+            prompt();
+            nerrors++;
+        }
     }
 
     /**
@@ -256,10 +268,12 @@
      * @param msg message to print
      */
     public void printWarning(SourcePosition pos, String msg) {
-        String prefix = (pos == null) ? programName : pos.toString();
-        warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
-        warnWriter.flush();
-        nwarnings++;
+        if (nwarnings < MaxWarnings) {
+            String prefix = (pos == null) ? programName : pos.toString();
+            warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
+            warnWriter.flush();
+            nwarnings++;
+        }
     }
 
     /**
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Fri Jun 11 07:12:07 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Fri Jun 11 17:24:23 2010 -0700
@@ -51,8 +51,6 @@
  * @author Neal Gafter (rewrite)
  */
 class Start {
-    /** Context for this invocation. */
-    private final Context context;
 
     private final String defaultDocletClassName;
     private final ClassLoader docletParentClassLoader;
@@ -98,8 +96,8 @@
           PrintWriter noticeWriter,
           String defaultDocletClassName,
           ClassLoader docletParentClassLoader) {
-        context = new Context();
-        messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter);
+        Context tempContext = new Context(); // interim context until option decoding completed
+        messager = new Messager(tempContext, programName, errWriter, warnWriter, noticeWriter);
         this.defaultDocletClassName = defaultDocletClassName;
         this.docletParentClassLoader = docletParentClassLoader;
     }
@@ -110,8 +108,8 @@
 
     Start(String programName, String defaultDocletClassName,
           ClassLoader docletParentClassLoader) {
-        context = new Context();
-        messager = new Messager(context, programName);
+        Context tempContext = new Context(); // interim context until option decoding completed
+        messager = new Messager(tempContext, programName);
         this.defaultDocletClassName = defaultDocletClassName;
         this.docletParentClassLoader = docletParentClassLoader;
     }
@@ -145,6 +143,13 @@
     }
 
     /**
+     * Usage
+     */
+    private void Xusage() {
+        messager.notice("main.Xusage");
+    }
+
+    /**
      * Exit
      */
     private void exit() {
@@ -213,6 +218,15 @@
         setDocletInvoker(argv);
         ListBuffer<String> subPackages = new ListBuffer<String>();
         ListBuffer<String> excludedPackages = new ListBuffer<String>();
+
+        Context context = new Context();
+        // Setup a new Messager, using the same initial parameters as the
+        // existing Messager, except that this one will be able to use any
+        // options that may be set up below.
+        Messager.preRegister(context,
+                messager.programName,
+                messager.errWriter, messager.warnWriter, messager.noticeWriter);
+
         Options compOpts = Options.instance(context);
         boolean docClasses = false;
 
@@ -310,6 +324,15 @@
                     usageError("main.locale_first");
                 oneArg(argv, i++);
                 docLocale = argv[i];
+            } else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) {
+                oneArg(argv, i++);
+                if (compOpts.get(arg) != null) {
+                    usageError("main.option.already.seen", arg);
+                }
+                compOpts.put(arg, argv[i]);
+            } else if (arg.equals("-X")) {
+                Xusage();
+                exit();
             } else if (arg.startsWith("-XD")) {
                 String s = arg.substring("-XD".length());
                 int eq = s.indexOf('=');
--- a/langtools/src/share/classes/com/sun/tools/javadoc/resources/javadoc.properties	Fri Jun 11 07:12:07 2010 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/resources/javadoc.properties	Fri Jun 11 17:24:23 2010 -0700
@@ -49,7 +49,13 @@
   -locale <name>            Locale to be used, e.g. en_US or en_US_WIN\n\
   -encoding <name>          Source file encoding name\n\
   -quiet                    Do not display status messages\n\
-  -J<flag>                  Pass <flag> directly to the runtime system\n
+  -J<flag>                  Pass <flag> directly to the runtime system\n\
+  -X                        Print a synopsis of nonstandard options\n
+main.Xusage=\
+  -Xmaxerrs <number>        Set the maximum number of errors to print\n\
+  -Xmaxwarns <number>       Set the maximum number of warnings to print\n\
+\n\
+These options are non-standard and subject to change without notice.
 main.option.already.seen=The {0} option may be specified no more than once.
 main.requires_argument=option {0} requires an argument.
 main.locale_first=option -locale must be first on the command line.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6958836/Test.java	Fri Jun 11 17:24:23 2010 -0700
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6958836
+ * @summary javadoc should support -Xmaxerrs and -Xmaxwarns
+ */
+
+import java.io.*;
+import java.util.*;
+
+public class Test {
+    public static void main(String... args) throws Exception {
+        new Test().run();
+    }
+
+    void run() throws Exception {
+        javadoc("errs",  list(),                   10,  0);
+        javadoc("errs",  list("-Xmaxerrs",   "0"), 10,  0);
+        javadoc("errs",  list("-Xmaxerrs",   "2"),  2,  0);
+        javadoc("errs",  list("-Xmaxerrs",   "4"),  4,  0);
+        javadoc("errs",  list("-Xmaxerrs",  "20"), 10,  0);
+
+        javadoc("warns", list(),                    0, 10);
+        javadoc("warns", list("-Xmaxwarns",  "0"),  0, 10);
+        javadoc("warns", list("-Xmaxwarns",  "2"),  0,  2);
+        javadoc("warns", list("-Xmaxwarns",  "4"),  0,  4);
+        javadoc("warns", list("-Xmaxwarns", "20"),  0, 10);
+
+        if (errors > 0)
+            throw new Exception(errors + " errors occurred.");
+    }
+
+    void javadoc(String pkg, List<String> testOpts,
+                int expectErrs, int expectWarns) {
+        System.err.println("Test " + (++count) + ": " + pkg + " " + testOpts);
+        File testOutDir = new File("test" + count);
+
+        List<String> opts = new ArrayList<String>();
+        // Force en_US locale in lieu of something like -XDrawDiagnostics.
+        // For some reason, this must be the first option when used.
+        opts.addAll(list("-locale", "en_US"));
+        opts.addAll(list("-classpath", System.getProperty("test.src")));
+        opts.addAll(list("-d", testOutDir.getPath()));
+        opts.addAll(testOpts);
+        opts.add(pkg);
+
+        StringWriter errSW = new StringWriter();
+        PrintWriter errPW = new PrintWriter(errSW);
+        StringWriter warnSW = new StringWriter();
+        PrintWriter warnPW = new PrintWriter(warnSW);
+        StringWriter noteSW = new StringWriter();
+        PrintWriter notePW = new PrintWriter(noteSW);
+
+        int rc = com.sun.tools.javadoc.Main.execute("javadoc",
+                              errPW, warnPW, notePW,
+                              "com.sun.tools.doclets.standard.Standard",
+                              getClass().getClassLoader(),
+                              opts.toArray(new String[opts.size()]));
+        System.err.println("rc: " + rc);
+
+        errPW.close();
+        String errOut = errSW.toString();
+        System.err.println("Errors:\n" + errOut);
+        warnPW.close();
+        String warnOut = warnSW.toString();
+        System.err.println("Warnings:\n" + warnOut);
+        notePW.close();
+        String noteOut = noteSW.toString();
+        System.err.println("Notes:\n" + noteOut);
+
+        check(errOut, "Errors.java", expectErrs);
+        check(warnOut, " warning ", expectWarns); // requires -locale en_US
+    }
+
+    void check(String text, String expectText, int expectCount) {
+        int foundCount = 0;
+        for (String line: text.split("[\r\n]+")) {
+            if (line.contains(expectText))
+                foundCount++;
+        }
+        if (foundCount != expectCount) {
+            error("incorrect number of matches found: " + foundCount
+                  + ", expected: " + expectCount);
+        }
+    }
+
+    private List<String> list(String... args) {
+        return Arrays.asList(args);
+    }
+
+    void error(String msg) {
+        System.err.println(msg);
+        errors++;
+    }
+
+    int count;
+    int errors;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6958836/errs/Errors.java	Fri Jun 11 17:24:23 2010 -0700
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package errs;
+
+// class with 10 errors
+class Errors {
+    X m0() { }
+    X m1() { }
+    X m2() { }
+    X m3() { }
+    X m4() { }
+    X m5() { }
+    X m6() { }
+    X m7() { }
+    X m8() { }
+    X m9() { }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6958836/warns/Warnings.java	Fri Jun 11 17:24:23 2010 -0700
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package warns;
+
+// class with 10 warnings
+public class Warnings {
+    /** @param x */
+    public void m0() { }
+
+    /** @param x */
+    public void m1() { }
+
+    /** @param x */
+    public void m2() { }
+
+    /** @param x */
+    public void m3() { }
+
+    /** @param x */
+    public void m4() { }
+
+    /** @param x */
+    public void m5() { }
+
+    /** @param x */
+    public void m6() { }
+
+    /** @param x */
+    public void m7() { }
+
+    /** @param x */
+    public void m8() { }
+
+    /** @param x */
+    public void m9() { }
+}