6964914: javadoc does not output number of warnings using user written doclet
authorjjg
Fri, 04 Mar 2011 19:59:04 -0800
changeset 8633 41311e88ffb7
parent 8632 af2a1f02c8ac
child 8634 222829aedfe4
6964914: javadoc does not output number of warnings using user written doclet Reviewed-by: bpatel
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/RootDocImpl.java
langtools/src/share/classes/com/sun/tools/javadoc/Start.java
langtools/test/tools/javadoc/6964914/Error.java
langtools/test/tools/javadoc/6964914/JavacWarning.java
langtools/test/tools/javadoc/6964914/JavadocWarning.java
langtools/test/tools/javadoc/6964914/Test.java
langtools/test/tools/javadoc/6964914/TestStdDoclet.java
langtools/test/tools/javadoc/6964914/TestUserDoclet.java
langtools/test/tools/javadoc/T6968833.java
--- a/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Fri Mar 04 19:56:02 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/DocletInvoker.java	Fri Mar 04 19:59:04 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2011, 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
@@ -31,14 +31,13 @@
 
 import com.sun.tools.javac.util.List;
 
-import java.net.*;
+import java.io.File;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.net.URLClassLoader;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.StringTokenizer;
 
 /**
  * Class creates, controls and invokes doclets.
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Messager.java	Fri Mar 04 19:56:02 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Messager.java	Fri Mar 04 19:59:04 2011 -0800
@@ -143,11 +143,9 @@
      * if needed.
      */
     private String getString(String key) {
-        ResourceBundle messageRB = this.messageRB;
         if (messageRB == null) {
             try {
-                this.messageRB = messageRB =
-                    ResourceBundle.getBundle(
+                messageRB = ResourceBundle.getBundle(
                           "com.sun.tools.javadoc.resources.javadoc");
             } catch (MissingResourceException e) {
                 throw new Error("Fatal: Resource for javadoc is missing");
@@ -456,8 +454,6 @@
      * Print exit message.
      */
     public void exitNotice() {
-        int nerrors = nerrors();
-        int nwarnings = nwarnings();
         if (nerrors > 0) {
             notice((nerrors > 1) ? "main.errors" : "main.error",
                    "" + nerrors);
--- a/langtools/src/share/classes/com/sun/tools/javadoc/RootDocImpl.java	Fri Mar 04 19:56:02 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/RootDocImpl.java	Fri Mar 04 19:59:04 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, 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
@@ -322,6 +322,7 @@
     /**
      * Do lazy initialization of "documentation" string.
      */
+    @Override
     protected String documentation() {
         if (documentation == null) {
             int cnt = options.length();
@@ -348,6 +349,7 @@
      * Return the source position of the entity, or null if
      * no position is available.
      */
+    @Override
     public SourcePosition position() {
         JavaFileObject path;
         return ((path = getOverviewPath()) == null) ?
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Fri Mar 04 19:56:02 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Fri Mar 04 19:59:04 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, 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
@@ -396,6 +396,10 @@
         boolean ok = root != null;
         if (ok) ok = docletInvoker.start(root);
 
+        Messager docletMessager = Messager.instance0(context);
+        messager.nwarnings += docletMessager.nwarnings;
+        messager.nerrors += docletMessager.nerrors;
+
         // We're done.
         if (compOpts.get("-verbose") != null) {
             tm = System.currentTimeMillis() - tm;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/Error.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2010, 2011, 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.
+ */
+
+public class Error {
+    Object x // no semicolon
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/JavacWarning.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2010, 2011, 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.
+ */
+
+public class JavacWarning {
+    int enum;  // warning in source 1.4
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/JavadocWarning.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010, 2011, 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.
+ */
+
+public class JavadocWarning {
+    /** @see DoesNotExist */
+    int x;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/Test.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2010, 2011, 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 6964914
+ * @summary javadoc does not output number of warnings using user written doclet
+ */
+
+import java.io.*;
+
+public class Test {
+    public static void main(String... args) throws Exception {
+        new Test().run();
+    }
+
+    public void run() throws Exception {
+        javadoc("Error.java", "1 error");
+        javadoc("JavacWarning.java", "1 warning");
+        javadoc("JavadocWarning.java", "1 warning");
+        if (errors > 0)
+            throw new Exception(errors + " errors found");
+    }
+
+    void javadoc(String path, String expect) {
+        File testSrc = new File(System.getProperty("test.src"));
+        String[] args = {
+            "-source", "1.4", // enables certain Parser warnings
+            "-bootclasspath", System.getProperty("sun.boot.class.path"),
+            "-classpath", ".",
+            "-package",
+            new File(testSrc, path).getPath()
+        };
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = com.sun.tools.javadoc.Main.execute(
+                "javadoc",
+                pw, pw, pw,
+                com.sun.tools.doclets.standard.Standard.class.getName(),
+                args);
+        pw.close();
+        String out = sw.toString();
+        if (!out.isEmpty())
+            System.err.println(out);
+        System.err.println("javadoc exit: rc=" + rc);
+
+        if (!out.contains(expect))
+            error("expected text not found: " + expect);
+    }
+
+    void error(String msg) {
+        System.err.println("Error: " + msg);
+        errors++;
+    }
+
+    int errors;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/TestStdDoclet.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2011, 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 6964914
+ * @summary javadoc does not output number of warnings using user written doclet
+ */
+
+import java.io.*;
+
+/**
+ * Dummy javadoc comment.
+ * @author jjg
+ * @see DoesNotExist
+ */
+public class TestStdDoclet {
+    public static void main(String... args) throws Exception {
+        new TestStdDoclet().run();
+    }
+
+    /**
+     * More dummy comments.
+     * @throws DoesNotExist   oops, javadoc does not see this
+     * @see DoesNotExist
+     */
+    void run() throws Exception {
+        File javaHome = new File(System.getProperty("java.home"));
+        if (javaHome.getName().equals("jre"))
+            javaHome = javaHome.getParentFile();
+        File javadoc = new File(new File(javaHome, "bin"), "javadoc");
+        File testSrc = new File(System.getProperty("test.src"));
+
+        // run javadoc in separate process to ensure doclet executed under
+        // normal user conditions w.r.t. classloader
+        String thisClassName = TestStdDoclet.class.getName();
+        Process p = new ProcessBuilder()
+            .command(javadoc.getPath(),
+                "-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"),
+                "-package",
+                new File(testSrc, thisClassName + ".java").getPath())
+            .redirectErrorStream(true)
+            .start();
+
+        int actualDocletWarnCount = 0;
+        int reportedDocletWarnCount = 0;
+        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        try {
+            String line;
+            while ((line = in.readLine()) != null) {
+                System.err.println(line);
+                if (line.contains("DoesNotExist"))
+                    actualDocletWarnCount++;
+                if (line.matches("[0-9]+ warning(s)?"))
+                    reportedDocletWarnCount =
+                            Integer.valueOf(line.substring(0, line.indexOf(" ")));
+            }
+        } finally {
+            in.close();
+        }
+        int rc = p.waitFor();
+        if (rc != 0)
+            System.err.println("javadoc failed, rc:" + rc);
+
+        int expectedDocletWarnCount = 2;
+        checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);
+        checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);
+    }
+
+    /**
+     * Private method should not cause a warning.
+     * @see DoesNotExist
+     */
+    private void checkEqual(String l1, int i1, String l2, int i2) throws Exception {
+        if (i1 != i2)
+            throw new Exception(l1 + " warn count, " + i1 + ", does not match "
+                        + l2 + " warn count, " + i2);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/6964914/TestUserDoclet.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2011, 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 6964914
+ * @summary javadoc does not output number of warnings using user written doclet
+ */
+
+import java.io.*;
+import com.sun.javadoc.Doclet;
+import com.sun.javadoc.RootDoc;
+
+public class TestUserDoclet extends Doclet {
+    public static void main(String... args) throws Exception {
+        new TestUserDoclet().run();
+    }
+
+    static final String docletWarning = "warning from test doclet";
+
+    /** Main doclet method. */
+    public static boolean start(RootDoc root) {
+        root.printWarning(null, docletWarning);
+        return true;
+    }
+
+    /** Main test method. */
+    void run() throws Exception {
+        File javaHome = new File(System.getProperty("java.home"));
+        if (javaHome.getName().equals("jre"))
+            javaHome = javaHome.getParentFile();
+        File javadoc = new File(new File(javaHome, "bin"), "javadoc");
+        File testSrc = new File(System.getProperty("test.src"));
+        File testClasses = new File(System.getProperty("test.classes"));
+
+        // run javadoc in separate process to ensure doclet executed under
+        // normal user conditions w.r.t. classloader
+        String thisClassName = TestUserDoclet.class.getName();
+        Process p = new ProcessBuilder()
+            .command(javadoc.getPath(),
+                "-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"),
+                "-doclet", thisClassName,
+                "-docletpath", testClasses.getPath(),
+                new File(testSrc, thisClassName + ".java").getPath())
+            .redirectErrorStream(true)
+            .start();
+
+        int actualDocletWarnCount = 0;
+        int reportedDocletWarnCount = 0;
+        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        try {
+            String line;
+            while ((line = in.readLine()) != null) {
+                System.err.println(line);
+                if (line.contains(docletWarning))
+                    actualDocletWarnCount++;
+                if (line.matches("[0-9]+ warning(s)?"))
+                    reportedDocletWarnCount =
+                            Integer.valueOf(line.substring(0, line.indexOf(" ")));
+            }
+        } finally {
+            in.close();
+        }
+        int rc = p.waitFor();
+        if (rc != 0)
+            System.err.println("javadoc failed, rc:" + rc);
+
+        int expectedDocletWarnCount = 1;
+        checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);
+        checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);
+    }
+
+    void checkEqual(String l1, int i1, String l2, int i2) throws Exception {
+        if (i1 != i2)
+            throw new Exception(l1 + " warn count, " + i1 + ", does not match "
+                        + l2 + " warn count, " + i2);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javadoc/T6968833.java	Fri Mar 04 19:59:04 2011 -0800
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2002, 2011, 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 6968833
+ * @summary javadoc reports error but still returns 0
+ */
+
+import java.io.*;
+
+public class T6968833 {
+    public static void main(String... args) throws IOException {
+        new T6968833().run();
+    }
+
+    void run() throws IOException {
+        File srcDir = new File("src");
+        // following file causes error: No public or protected classes found to document.
+        File f = writeFile(srcDir, "Foo.java", "class Foo { }");
+        String[] args = { f.getPath() };
+        int rc = com.sun.tools.javadoc.Main.execute(args);
+        if (rc == 0)
+            throw new Error("Unexpected exit from javadoc: " + rc);
+    }
+
+    File writeFile(File dir, String path, String s) throws IOException {
+        File f = new File(dir, path);
+        f.getParentFile().mkdirs();
+        try (Writer out = new FileWriter(f)) {
+            out.write(s);
+        }
+        return f;
+    }
+}
+