Fri, 11 Jun 2010 17:24:23 -0700
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/util/Log.java Fri Jun 11 07:12:07 2010 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Fri Jun 11 17:24:23 2010 -0700 1.3 @@ -125,8 +125,8 @@ 1.4 this.promptOnError = options.get("-prompt") != null; 1.5 this.emitWarnings = options.get("-Xlint:none") == null; 1.6 this.suppressNotes = options.get("suppressNotes") != null; 1.7 - this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100); 1.8 - this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100); 1.9 + this.MaxErrors = getIntOption(options, "-Xmaxerrs", getDefaultMaxErrors()); 1.10 + this.MaxWarnings = getIntOption(options, "-Xmaxwarns", getDefaultMaxWarnings()); 1.11 1.12 boolean rawDiagnostics = options.get("rawDiagnostics") != null; 1.13 messages = JavacMessages.instance(context); 1.14 @@ -155,6 +155,18 @@ 1.15 return defaultValue; 1.16 } 1.17 1.18 + /** Default value for -Xmaxerrs. 1.19 + */ 1.20 + protected int getDefaultMaxErrors() { 1.21 + return 100; 1.22 + } 1.23 + 1.24 + /** Default value for -Xmaxwarns. 1.25 + */ 1.26 + protected int getDefaultMaxWarnings() { 1.27 + return 100; 1.28 + } 1.29 + 1.30 /** The default writer for diagnostics 1.31 */ 1.32 static final PrintWriter defaultWriter(Context context) {
2.1 --- a/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java Fri Jun 11 07:12:07 2010 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java Fri Jun 11 17:24:23 2010 -0700 2.3 @@ -155,10 +155,8 @@ 2.4 public boolean start(RootDoc root) { 2.5 Object retVal; 2.6 String methodName = "start"; 2.7 - Class<?>[] paramTypes = new Class<?>[1]; 2.8 - Object[] params = new Object[1]; 2.9 - paramTypes[0] = RootDoc.class; 2.10 - params[0] = root; 2.11 + Class<?>[] paramTypes = { RootDoc.class }; 2.12 + Object[] params = { root }; 2.13 try { 2.14 retVal = invoke(methodName, null, paramTypes, params); 2.15 } catch (DocletInvokeException exc) { 2.16 @@ -181,10 +179,8 @@ 2.17 public int optionLength(String option) { 2.18 Object retVal; 2.19 String methodName = "optionLength"; 2.20 - Class<?>[] paramTypes = new Class<?>[1]; 2.21 - Object[] params = new Object[1]; 2.22 - paramTypes[0] = option.getClass(); 2.23 - params[0] = option; 2.24 + Class<?>[] paramTypes = { String.class }; 2.25 + Object[] params = { option }; 2.26 try { 2.27 retVal = invoke(methodName, new Integer(0), paramTypes, params); 2.28 } catch (DocletInvokeException exc) { 2.29 @@ -208,12 +204,8 @@ 2.30 String options[][] = optlist.toArray(new String[optlist.length()][]); 2.31 String methodName = "validOptions"; 2.32 DocErrorReporter reporter = messager; 2.33 - Class<?>[] paramTypes = new Class<?>[2]; 2.34 - Object[] params = new Object[2]; 2.35 - paramTypes[0] = options.getClass(); 2.36 - paramTypes[1] = DocErrorReporter.class; 2.37 - params[0] = options; 2.38 - params[1] = reporter; 2.39 + Class<?>[] paramTypes = { String[][].class, DocErrorReporter.class }; 2.40 + Object[] params = { options, reporter }; 2.41 try { 2.42 retVal = invoke(methodName, Boolean.TRUE, paramTypes, params); 2.43 } catch (DocletInvokeException exc) {
3.1 --- a/src/share/classes/com/sun/tools/javadoc/Messager.java Fri Jun 11 07:12:07 2010 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javadoc/Messager.java Fri Jun 11 17:24:23 2010 -0700 3.3 @@ -86,7 +86,7 @@ 3.4 private static final long serialVersionUID = 0; 3.5 } 3.6 3.7 - private final String programName; 3.8 + final String programName; 3.9 3.10 private ResourceBundle messageRB = null; 3.11 3.12 @@ -121,6 +121,16 @@ 3.13 this.programName = programName; 3.14 } 3.15 3.16 + @Override 3.17 + protected int getDefaultMaxErrors() { 3.18 + return Integer.MAX_VALUE; 3.19 + } 3.20 + 3.21 + @Override 3.22 + protected int getDefaultMaxWarnings() { 3.23 + return Integer.MAX_VALUE; 3.24 + } 3.25 + 3.26 /** 3.27 * Reset resource bundle, eg. locale has changed. 3.28 */ 3.29 @@ -231,11 +241,13 @@ 3.30 * @param msg message to print 3.31 */ 3.32 public void printError(SourcePosition pos, String msg) { 3.33 - String prefix = (pos == null) ? programName : pos.toString(); 3.34 - errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); 3.35 - errWriter.flush(); 3.36 - prompt(); 3.37 - nerrors++; 3.38 + if (nerrors < MaxErrors) { 3.39 + String prefix = (pos == null) ? programName : pos.toString(); 3.40 + errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); 3.41 + errWriter.flush(); 3.42 + prompt(); 3.43 + nerrors++; 3.44 + } 3.45 } 3.46 3.47 /** 3.48 @@ -256,10 +268,12 @@ 3.49 * @param msg message to print 3.50 */ 3.51 public void printWarning(SourcePosition pos, String msg) { 3.52 - String prefix = (pos == null) ? programName : pos.toString(); 3.53 - warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); 3.54 - warnWriter.flush(); 3.55 - nwarnings++; 3.56 + if (nwarnings < MaxWarnings) { 3.57 + String prefix = (pos == null) ? programName : pos.toString(); 3.58 + warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); 3.59 + warnWriter.flush(); 3.60 + nwarnings++; 3.61 + } 3.62 } 3.63 3.64 /**
4.1 --- a/src/share/classes/com/sun/tools/javadoc/Start.java Fri Jun 11 07:12:07 2010 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javadoc/Start.java Fri Jun 11 17:24:23 2010 -0700 4.3 @@ -51,8 +51,6 @@ 4.4 * @author Neal Gafter (rewrite) 4.5 */ 4.6 class Start { 4.7 - /** Context for this invocation. */ 4.8 - private final Context context; 4.9 4.10 private final String defaultDocletClassName; 4.11 private final ClassLoader docletParentClassLoader; 4.12 @@ -98,8 +96,8 @@ 4.13 PrintWriter noticeWriter, 4.14 String defaultDocletClassName, 4.15 ClassLoader docletParentClassLoader) { 4.16 - context = new Context(); 4.17 - messager = new Messager(context, programName, errWriter, warnWriter, noticeWriter); 4.18 + Context tempContext = new Context(); // interim context until option decoding completed 4.19 + messager = new Messager(tempContext, programName, errWriter, warnWriter, noticeWriter); 4.20 this.defaultDocletClassName = defaultDocletClassName; 4.21 this.docletParentClassLoader = docletParentClassLoader; 4.22 } 4.23 @@ -110,8 +108,8 @@ 4.24 4.25 Start(String programName, String defaultDocletClassName, 4.26 ClassLoader docletParentClassLoader) { 4.27 - context = new Context(); 4.28 - messager = new Messager(context, programName); 4.29 + Context tempContext = new Context(); // interim context until option decoding completed 4.30 + messager = new Messager(tempContext, programName); 4.31 this.defaultDocletClassName = defaultDocletClassName; 4.32 this.docletParentClassLoader = docletParentClassLoader; 4.33 } 4.34 @@ -145,6 +143,13 @@ 4.35 } 4.36 4.37 /** 4.38 + * Usage 4.39 + */ 4.40 + private void Xusage() { 4.41 + messager.notice("main.Xusage"); 4.42 + } 4.43 + 4.44 + /** 4.45 * Exit 4.46 */ 4.47 private void exit() { 4.48 @@ -213,6 +218,15 @@ 4.49 setDocletInvoker(argv); 4.50 ListBuffer<String> subPackages = new ListBuffer<String>(); 4.51 ListBuffer<String> excludedPackages = new ListBuffer<String>(); 4.52 + 4.53 + Context context = new Context(); 4.54 + // Setup a new Messager, using the same initial parameters as the 4.55 + // existing Messager, except that this one will be able to use any 4.56 + // options that may be set up below. 4.57 + Messager.preRegister(context, 4.58 + messager.programName, 4.59 + messager.errWriter, messager.warnWriter, messager.noticeWriter); 4.60 + 4.61 Options compOpts = Options.instance(context); 4.62 boolean docClasses = false; 4.63 4.64 @@ -310,6 +324,15 @@ 4.65 usageError("main.locale_first"); 4.66 oneArg(argv, i++); 4.67 docLocale = argv[i]; 4.68 + } else if (arg.equals("-Xmaxerrs") || arg.equals("-Xmaxwarns")) { 4.69 + oneArg(argv, i++); 4.70 + if (compOpts.get(arg) != null) { 4.71 + usageError("main.option.already.seen", arg); 4.72 + } 4.73 + compOpts.put(arg, argv[i]); 4.74 + } else if (arg.equals("-X")) { 4.75 + Xusage(); 4.76 + exit(); 4.77 } else if (arg.startsWith("-XD")) { 4.78 String s = arg.substring("-XD".length()); 4.79 int eq = s.indexOf('=');
5.1 --- a/src/share/classes/com/sun/tools/javadoc/resources/javadoc.properties Fri Jun 11 07:12:07 2010 -0700 5.2 +++ b/src/share/classes/com/sun/tools/javadoc/resources/javadoc.properties Fri Jun 11 17:24:23 2010 -0700 5.3 @@ -49,7 +49,13 @@ 5.4 -locale <name> Locale to be used, e.g. en_US or en_US_WIN\n\ 5.5 -encoding <name> Source file encoding name\n\ 5.6 -quiet Do not display status messages\n\ 5.7 - -J<flag> Pass <flag> directly to the runtime system\n 5.8 + -J<flag> Pass <flag> directly to the runtime system\n\ 5.9 + -X Print a synopsis of nonstandard options\n 5.10 +main.Xusage=\ 5.11 + -Xmaxerrs <number> Set the maximum number of errors to print\n\ 5.12 + -Xmaxwarns <number> Set the maximum number of warnings to print\n\ 5.13 +\n\ 5.14 +These options are non-standard and subject to change without notice. 5.15 main.option.already.seen=The {0} option may be specified no more than once. 5.16 main.requires_argument=option {0} requires an argument. 5.17 main.locale_first=option -locale must be first on the command line.
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javadoc/6958836/Test.java Fri Jun 11 17:24:23 2010 -0700 6.3 @@ -0,0 +1,120 @@ 6.4 +/* 6.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/* 6.28 + * @test 6.29 + * @bug 6958836 6.30 + * @summary javadoc should support -Xmaxerrs and -Xmaxwarns 6.31 + */ 6.32 + 6.33 +import java.io.*; 6.34 +import java.util.*; 6.35 + 6.36 +public class Test { 6.37 + public static void main(String... args) throws Exception { 6.38 + new Test().run(); 6.39 + } 6.40 + 6.41 + void run() throws Exception { 6.42 + javadoc("errs", list(), 10, 0); 6.43 + javadoc("errs", list("-Xmaxerrs", "0"), 10, 0); 6.44 + javadoc("errs", list("-Xmaxerrs", "2"), 2, 0); 6.45 + javadoc("errs", list("-Xmaxerrs", "4"), 4, 0); 6.46 + javadoc("errs", list("-Xmaxerrs", "20"), 10, 0); 6.47 + 6.48 + javadoc("warns", list(), 0, 10); 6.49 + javadoc("warns", list("-Xmaxwarns", "0"), 0, 10); 6.50 + javadoc("warns", list("-Xmaxwarns", "2"), 0, 2); 6.51 + javadoc("warns", list("-Xmaxwarns", "4"), 0, 4); 6.52 + javadoc("warns", list("-Xmaxwarns", "20"), 0, 10); 6.53 + 6.54 + if (errors > 0) 6.55 + throw new Exception(errors + " errors occurred."); 6.56 + } 6.57 + 6.58 + void javadoc(String pkg, List<String> testOpts, 6.59 + int expectErrs, int expectWarns) { 6.60 + System.err.println("Test " + (++count) + ": " + pkg + " " + testOpts); 6.61 + File testOutDir = new File("test" + count); 6.62 + 6.63 + List<String> opts = new ArrayList<String>(); 6.64 + // Force en_US locale in lieu of something like -XDrawDiagnostics. 6.65 + // For some reason, this must be the first option when used. 6.66 + opts.addAll(list("-locale", "en_US")); 6.67 + opts.addAll(list("-classpath", System.getProperty("test.src"))); 6.68 + opts.addAll(list("-d", testOutDir.getPath())); 6.69 + opts.addAll(testOpts); 6.70 + opts.add(pkg); 6.71 + 6.72 + StringWriter errSW = new StringWriter(); 6.73 + PrintWriter errPW = new PrintWriter(errSW); 6.74 + StringWriter warnSW = new StringWriter(); 6.75 + PrintWriter warnPW = new PrintWriter(warnSW); 6.76 + StringWriter noteSW = new StringWriter(); 6.77 + PrintWriter notePW = new PrintWriter(noteSW); 6.78 + 6.79 + int rc = com.sun.tools.javadoc.Main.execute("javadoc", 6.80 + errPW, warnPW, notePW, 6.81 + "com.sun.tools.doclets.standard.Standard", 6.82 + getClass().getClassLoader(), 6.83 + opts.toArray(new String[opts.size()])); 6.84 + System.err.println("rc: " + rc); 6.85 + 6.86 + errPW.close(); 6.87 + String errOut = errSW.toString(); 6.88 + System.err.println("Errors:\n" + errOut); 6.89 + warnPW.close(); 6.90 + String warnOut = warnSW.toString(); 6.91 + System.err.println("Warnings:\n" + warnOut); 6.92 + notePW.close(); 6.93 + String noteOut = noteSW.toString(); 6.94 + System.err.println("Notes:\n" + noteOut); 6.95 + 6.96 + check(errOut, "Errors.java", expectErrs); 6.97 + check(warnOut, " warning ", expectWarns); // requires -locale en_US 6.98 + } 6.99 + 6.100 + void check(String text, String expectText, int expectCount) { 6.101 + int foundCount = 0; 6.102 + for (String line: text.split("[\r\n]+")) { 6.103 + if (line.contains(expectText)) 6.104 + foundCount++; 6.105 + } 6.106 + if (foundCount != expectCount) { 6.107 + error("incorrect number of matches found: " + foundCount 6.108 + + ", expected: " + expectCount); 6.109 + } 6.110 + } 6.111 + 6.112 + private List<String> list(String... args) { 6.113 + return Arrays.asList(args); 6.114 + } 6.115 + 6.116 + void error(String msg) { 6.117 + System.err.println(msg); 6.118 + errors++; 6.119 + } 6.120 + 6.121 + int count; 6.122 + int errors; 6.123 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javadoc/6958836/errs/Errors.java Fri Jun 11 17:24:23 2010 -0700 7.3 @@ -0,0 +1,38 @@ 7.4 +/* 7.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +package errs; 7.28 + 7.29 +// class with 10 errors 7.30 +class Errors { 7.31 + X m0() { } 7.32 + X m1() { } 7.33 + X m2() { } 7.34 + X m3() { } 7.35 + X m4() { } 7.36 + X m5() { } 7.37 + X m6() { } 7.38 + X m7() { } 7.39 + X m8() { } 7.40 + X m9() { } 7.41 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javadoc/6958836/warns/Warnings.java Fri Jun 11 17:24:23 2010 -0700 8.3 @@ -0,0 +1,57 @@ 8.4 +/* 8.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +package warns; 8.28 + 8.29 +// class with 10 warnings 8.30 +public class Warnings { 8.31 + /** @param x */ 8.32 + public void m0() { } 8.33 + 8.34 + /** @param x */ 8.35 + public void m1() { } 8.36 + 8.37 + /** @param x */ 8.38 + public void m2() { } 8.39 + 8.40 + /** @param x */ 8.41 + public void m3() { } 8.42 + 8.43 + /** @param x */ 8.44 + public void m4() { } 8.45 + 8.46 + /** @param x */ 8.47 + public void m5() { } 8.48 + 8.49 + /** @param x */ 8.50 + public void m6() { } 8.51 + 8.52 + /** @param x */ 8.53 + public void m7() { } 8.54 + 8.55 + /** @param x */ 8.56 + public void m8() { } 8.57 + 8.58 + /** @param x */ 8.59 + public void m9() { } 8.60 +}