8224083: javadoc Reporter generates warning for Kind.NOTE
authorvromero
Fri, 31 May 2019 18:35:17 -0400
changeset 55138 b564f266dda0
parent 55137 c40c3e5d7c7a
child 55139 3ea18959a349
8224083: javadoc Reporter generates warning for Kind.NOTE 8224082: NPE in javadoc Reporter Reviewed-by: jjg
src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Messager.java
test/langtools/jdk/javadoc/tool/reporter_generates_warnings/ReporterGeneratesWarningsInsteadOfNotes.java
test/langtools/jdk/javadoc/tool/reporter_generates_warnings/pkg/MyDoclet.java
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Messager.java	Wed May 22 15:30:11 2019 -0700
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Messager.java	Fri May 31 18:35:17 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -124,9 +124,11 @@
             case MANDATORY_WARNING:
                 printWarning(e, msg);
                 return;
+            case NOTE:
+                printNotice(e, msg);
+                return;
             default:
-                printWarning(e, msg);
-                return;
+                throw new IllegalArgumentException(String.format("unexpected option %s", kind));
         }
     }
 
@@ -201,6 +203,9 @@
         }
         JavacTrees trees = JavacTrees.instance(context);
         TreePath path = trees.getPath(e);
+        if (path == null) {
+            return programName;
+        }
         DocSourcePositions sourcePositions = trees.getSourcePositions();
         JCTree tree = trees.getTree(e);
         CompilationUnitTree cu = path.getCompilationUnit();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/jdk/javadoc/tool/reporter_generates_warnings/ReporterGeneratesWarningsInsteadOfNotes.java	Fri May 31 18:35:17 2019 -0400
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2019, 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 8224083 8224082
+ * @summary javadoc Reporter generates "warning" for Kind.NOTE
+ *          NPE in javadoc Reporter
+ * @library /tools/lib ../../lib
+ * @modules jdk.javadoc/jdk.javadoc.internal.tool
+ *          jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.main
+ *          jdk.javadoc/jdk.javadoc.internal.api
+ *          jdk.javadoc/jdk.javadoc.internal.tool
+ * @build toolbox.ToolBox javadoc.tester.*
+ * @compile pkg/MyDoclet.java
+ * @run main ReporterGeneratesWarningsInsteadOfNotes
+ */
+
+import java.io.PrintWriter;
+import java.util.*;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.util.ElementFilter;
+
+import jdk.javadoc.doclet.Doclet;
+import jdk.javadoc.doclet.DocletEnvironment;
+import jdk.javadoc.doclet.Reporter;
+
+import javadoc.tester.JavadocTester;
+import toolbox.JavacTask;
+import toolbox.JavadocTask;
+import toolbox.TestRunner;
+import toolbox.Task;
+import toolbox.ToolBox;
+
+public class ReporterGeneratesWarningsInsteadOfNotes extends TestRunner {
+    ToolBox tb = new ToolBox();
+
+    static final String[] outputToCheckFor = new String[]{
+            ": PACKAGE pkg",
+            ": CLASS pkg.MyDoclet",
+            ": CLASS pkg.MyDoclet.MyScanner",
+            ": CONSTRUCTOR MyScanner()",
+            ": METHOD scan(javax.lang.model.element.Element,java.lang.Integer)",
+            ": PARAMETER e",
+            ": PARAMETER depth",
+            ": CLASS pkg.MyDoclet.Option",
+            ": FIELD names",
+            ": FIELD hasArg",
+            ": FIELD description",
+            ": CONSTRUCTOR Option(java.lang.String,boolean,java.lang.String)",
+            ": PARAMETER names",
+            ": PARAMETER hasArg",
+            ": PARAMETER description",
+            ": METHOD getArgumentCount()",
+            ": METHOD getDescription()",
+            ": METHOD getKind()",
+            ": METHOD getNames()",
+            ": METHOD getParameters()",
+            ": CONSTRUCTOR MyDoclet()",
+            ": FIELD OK",
+            ": FIELD verbose",
+            ": FIELD reporter",
+            ": FIELD options",
+            ": METHOD init(java.util.Locale,jdk.javadoc.doclet.Reporter)",
+            ": PARAMETER locale",
+            ": PARAMETER reporter",
+            ": METHOD getName()",
+            ": METHOD getSupportedOptions()",
+            ": METHOD getSupportedSourceVersion()",
+            ": METHOD run(jdk.javadoc.doclet.DocletEnvironment)",
+            ": PARAMETER environment"
+    };
+
+    ReporterGeneratesWarningsInsteadOfNotes() throws Exception {
+        super(System.err);
+    }
+
+    public static void main(String... args) throws Exception {
+        ReporterGeneratesWarningsInsteadOfNotes tester = new ReporterGeneratesWarningsInsteadOfNotes();
+        tester.runTests();
+    }
+
+    protected void runTests() throws Exception {
+        runTests(m -> new Object[] { Paths.get(m.getName()) });
+    }
+
+    @Test
+    public void testMain(Path base) throws Exception {
+        String testSrc = System.getProperty("test.src");
+        String testClasses = System.getProperty("test.classes");
+        String log = new JavadocTask(tb, Task.Mode.CMDLINE)
+                .options("-docletpath", testClasses, "-doclet",  "pkg.MyDoclet", "-sourcepath", testSrc, "pkg")
+                .run(Task.Expect.SUCCESS)
+                .writeAll()
+                .getOutput(Task.OutputKind.DIRECT);
+        for (String output : outputToCheckFor) {
+            if (!log.contains(output)) {
+                throw new AssertionError("was expecting to find: " + output);
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/jdk/javadoc/tool/reporter_generates_warnings/pkg/MyDoclet.java	Fri May 31 18:35:17 2019 -0400
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2019, 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 pkg;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.util.ElementScanner9;
+import javax.tools.Diagnostic;
+
+import jdk.javadoc.doclet.Doclet;
+import jdk.javadoc.doclet.DocletEnvironment;
+import jdk.javadoc.doclet.Reporter;
+
+public class MyDoclet implements Doclet {
+    private static final boolean OK = true;
+    private boolean verbose;
+    private Reporter reporter;
+
+    Set<Option> options = Set.of(
+            new Option("--alpha -a", false, "an example no-arg option") {
+                @Override
+                public boolean process(String option, List<String> arguments) {
+                    System.out.println("received option " + option + " " + arguments);
+                    return OK;
+                }
+            },
+            new Option("--beta -b", true, "an example 1-arg option") {
+                @Override
+                public boolean process(String option, List<String> arguments) {
+                    System.out.println("received option " + option + " " + arguments);
+                    return OK;
+                }
+            },
+            new Option("--verbose", false, "report progress") {
+                @Override
+                public boolean process(String option, List<String> arguments) {
+                    verbose = true;
+                    return OK;
+                }
+            }
+    );
+
+    @Override
+    public void init(Locale locale, Reporter reporter) {
+        this.reporter = reporter;
+    }
+
+    @Override
+    public String getName() {
+        return "MyDoclet";
+    }
+
+    @Override
+    public Set<? extends Option> getSupportedOptions() {
+        return options;
+    }
+
+    @Override
+    public SourceVersion getSupportedSourceVersion() {
+        return SourceVersion.latest();
+    }
+
+    @Override
+    public boolean run(DocletEnvironment environment) {
+        MyScanner myScanner = new MyScanner();
+        for (Element e : environment.getSpecifiedElements()) {
+            myScanner.scan(e, 0);
+        }
+
+        return OK;
+    }
+
+    class MyScanner extends ElementScanner9<Void, Integer> {
+        @Override
+        public Void scan(Element e, Integer depth) {
+            String msg = e.getKind() + " " + e;
+            reporter.print(Diagnostic.Kind.NOTE, e, msg);
+            return super.scan(e, depth + 1);
+        }
+    }
+
+
+    abstract class Option implements Doclet.Option {
+        final List<String> names;
+        final boolean hasArg;
+        final String description;
+
+        Option(String names, boolean hasArg, String description) {
+            this.names = List.of(names.split("\\s+"));
+            this.hasArg = hasArg;
+            this.description = description;
+        }
+
+        @Override
+        public int getArgumentCount() {
+            return hasArg ? 1 : 0;
+        }
+
+        @Override
+        public String getDescription() {
+            return description;
+        }
+
+        @Override
+        public Kind getKind() {
+            return Kind.STANDARD;
+        }
+
+        @Override
+        public List<String> getNames() {
+            return names;
+        }
+
+        @Override
+        public String getParameters() {
+            return hasArg ? "<arg>" : null;
+        }
+    }
+}