src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Messager.java

Print this page

        

@@ -21,17 +21,27 @@
  * 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 com.sun.tools.javadoc;
+package jdk.javadoc.internal.tool;
 
+
 import java.io.PrintWriter;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
-import com.sun.javadoc.*;
+import javax.lang.model.element.Element;
+import javax.tools.Diagnostic.Kind;
+
+import jdk.javadoc.doclet.Reporter;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.util.DocSourcePositions;
+import com.sun.source.util.DocTreePath;
+import com.sun.source.util.TreePath;
+import com.sun.tools.javac.api.JavacTrees;
+import com.sun.tools.javac.tree.JCTree;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
 import com.sun.tools.javac.util.JavacMessages;
 import com.sun.tools.javac.util.Log;

@@ -50,12 +60,12 @@
  *
  * @see java.util.ResourceBundle
  * @see java.text.MessageFormat
  * @author Neal Gafter (rewrite)
  */
-public class Messager extends Log implements DocErrorReporter {
-    public static final SourcePosition NOPOS = null;
+public class Messager extends Log implements Reporter {
+    final Context context;
 
     /** Get the current messager, which is also the compiler log. */
     public static Messager instance0(Context context) {
         Log instance = context.get(logKey);
         if (instance == null || !(instance instanceof Messager))

@@ -65,31 +75,72 @@
 
     public static void preRegister(Context context,
                                    final String programName) {
         context.put(logKey, new Context.Factory<Log>() {
             public Log make(Context c) {
-                return new Messager(c,
-                                    programName);
+                return new Messager(c, programName);
             }
         });
     }
-    public static void preRegister(Context context,
-                                   final String programName,
-                                   final PrintWriter errWriter,
-                                   final PrintWriter warnWriter,
-                                   final PrintWriter noticeWriter) {
+
+    public static void preRegister(Context context, final String programName,
+            final PrintWriter outWriter, final PrintWriter errWriter) {
         context.put(logKey, new Context.Factory<Log>() {
             public Log make(Context c) {
-                return new Messager(c,
-                                    programName,
-                                    errWriter,
-                                    warnWriter,
-                                    noticeWriter);
+                return new Messager(c, programName, outWriter, errWriter);
             }
         });
     }
 
+    @Override
+    public void print(Kind kind, String msg) {
+        switch (kind) {
+            case ERROR:
+                printError(msg);
+                return;
+            case WARNING:
+            case MANDATORY_WARNING:
+                printWarning(msg);
+                return;
+            default:
+                printNotice(msg);
+                return;
+        }
+    }
+
+    @Override
+    public void print(Kind kind, DocTreePath path, String msg) {
+        switch (kind) {
+            case ERROR:
+                printError(path, msg);
+                return;
+            case WARNING:
+            case MANDATORY_WARNING:
+                printWarning(path, msg);
+                return;
+            default:
+                printWarning(path, msg);
+                return;
+        }
+    }
+
+    @Override
+    public void print(Kind kind, Element e, String msg) {
+                switch (kind) {
+            case ERROR:
+                printError(e, msg);
+                return;
+            case WARNING:
+            case MANDATORY_WARNING:
+                printWarning(e, msg);
+                return;
+            default:
+                printWarning(e, msg);
+                return;
+        }
+    }
+
     public class ExitJavadoc extends Error {
         private static final long serialVersionUID = 0;
     }
 
     final String programName;

@@ -98,42 +149,37 @@
     private final JavacMessages messages;
     private final JCDiagnostic.Factory javadocDiags;
 
     /** The default writer for diagnostics
      */
+    static final PrintWriter defaultOutWriter = new PrintWriter(System.out);
     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
-    static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
-    static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
 
     /**
      * Constructor
      * @param programName  Name of the program (for error messages).
      */
-    protected Messager(Context context, String programName) {
-        this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
+    public Messager(Context context, String programName) {
+        this(context, programName, defaultOutWriter, defaultErrWriter);
     }
 
     /**
      * Constructor
      * @param programName  Name of the program (for error messages).
-     * @param errWriter    Stream for error messages
-     * @param warnWriter   Stream for warnings
-     * @param noticeWriter Stream for other messages
+     * @param outWriter    Stream for notices etc.
+     * @param errWriter    Stream for errors and warnings
      */
     @SuppressWarnings("deprecation")
-    protected Messager(Context context,
-                       String programName,
-                       PrintWriter errWriter,
-                       PrintWriter warnWriter,
-                       PrintWriter noticeWriter) {
-        super(context, errWriter, warnWriter, noticeWriter);
+    public Messager(Context context, String programName, PrintWriter outWriter, PrintWriter errWriter) {
+        super(context, errWriter, errWriter, outWriter);
         messages = JavacMessages.instance(context);
-        messages.add(locale -> ResourceBundle.getBundle("com.sun.tools.javadoc.resources.javadoc",
+        messages.add(locale -> ResourceBundle.getBundle("jdk.javadoc.internal.tool.resources.javadoc",
                                                          locale));
         javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
         this.programName = programName;
-
+        this.context = context;
+        locale = Locale.getDefault();
     }
 
     public void setLocale(Locale locale) {
         this.locale = locale;
     }

@@ -146,35 +192,70 @@
      */
     String getText(String key, Object... args) {
         return messages.getLocalizedString(locale, key, args);
     }
 
+    private String getDiagSource(DocTreePath path) {
+        if (path == null) {
+            return programName;
+        }
+        JavacTrees trees = JavacTrees.instance(context);
+        DocSourcePositions sourcePositions = trees.getSourcePositions();
+        CompilationUnitTree cu = path.getTreePath().getCompilationUnit();
+        long spos = sourcePositions.getStartPosition(cu, path.getDocComment(), path.getLeaf());
+        long lineNumber = cu.getLineMap().getLineNumber(spos);
+        String fname = cu.getSourceFile().getName();
+        String posString = fname + ":" + lineNumber;
+        return posString;
+    }
+
+    private String getDiagSource(Element e) {
+        if (e == null) {
+            return programName;
+        }
+        JavacTrees trees = JavacTrees.instance(context);
+        TreePath path = trees.getPath(e);
+        DocSourcePositions sourcePositions = trees.getSourcePositions();
+        JCTree tree = trees.getTree(e);
+        CompilationUnitTree cu = path.getCompilationUnit();
+        long spos = sourcePositions.getStartPosition(cu, tree);
+        long lineNumber = cu.getLineMap().getLineNumber(spos);
+        String fname = cu.getSourceFile().getName();
+        String posString = fname + ":" + lineNumber;
+        return posString;
+    }
+
     /**
      * Print error message, increment error count.
      * Part of DocErrorReporter.
      *
      * @param msg message to print
      */
     public void printError(String msg) {
-        printError(null, msg);
+        printError((DocTreePath)null, msg);
     }
 
-    /**
-     * Print error message, increment error count.
-     * Part of DocErrorReporter.
-     *
-     * @param pos the position where the error occurs
-     * @param msg message to print
-     */
-    public void printError(SourcePosition pos, String msg) {
+    public void printError(DocTreePath path, String msg) {
+        String prefix = getDiagSource(path);
         if (diagListener != null) {
-            report(DiagnosticType.ERROR, pos, msg);
+            report(DiagnosticType.ERROR, prefix, msg);
             return;
         }
+        incrementErrorCount(prefix, msg);
+    }
 
+    public void printError(Element e, String msg) {
+        String prefix = getDiagSource(e);
+        if (diagListener != null) {
+            report(DiagnosticType.ERROR, prefix, msg);
+            return;
+        }
+        incrementErrorCount(prefix, msg);
+    }
+
+    private void incrementErrorCount(String prefix, String msg) {
         if (nerrors < MaxErrors) {
-            String prefix = (pos == null) ? programName : pos.toString();
             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
             errWriter.flush();
             prompt();
             nerrors++;
         }

@@ -185,29 +266,34 @@
      * Part of DocErrorReporter.
      *
      * @param msg message to print
      */
     public void printWarning(String msg) {
-        printWarning(null, msg);
+        printWarning((DocTreePath)null, msg);
     }
 
-    /**
-     * Print warning message, increment warning count.
-     * Part of DocErrorReporter.
-     *
-     * @param pos the position where the error occurs
-     * @param msg message to print
-     */
-    public void printWarning(SourcePosition pos, String msg) {
+    public void printWarning(DocTreePath path, String msg) {
+        String prefix = getDiagSource(path);
         if (diagListener != null) {
-            report(DiagnosticType.WARNING, pos, msg);
+            report(DiagnosticType.WARNING, prefix, msg);
             return;
         }
+        incrementWarningCount(prefix, msg);
+    }
 
+    public void printWarning(Element e, String msg) {
+        String prefix = getDiagSource(e);
+        if (diagListener != null) {
+            report(DiagnosticType.WARNING, prefix, msg);
+            return;
+        }
+        incrementWarningCount(prefix, msg);
+    }
+
+    private void incrementWarningCount(String prefix, String msg) {
         if (nwarnings < MaxWarnings) {
-            String prefix = (pos == null) ? programName : pos.toString();
-            warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
+            warnWriter.println(prefix + ": " + getText("javadoc.warning") + " - " + msg);
             warnWriter.flush();
             nwarnings++;
         }
     }
 

@@ -216,52 +302,88 @@
      * Part of DocErrorReporter.
      *
      * @param msg message to print
      */
     public void printNotice(String msg) {
-        printNotice(null, msg);
+        printNotice((DocTreePath)null, msg);
     }
 
-    /**
-     * Print a message.
-     * Part of DocErrorReporter.
-     *
-     * @param pos the position where the error occurs
-     * @param msg message to print
-     */
-    public void printNotice(SourcePosition pos, String msg) {
+    public void printNotice(DocTreePath path, String msg) {
+        String prefix = getDiagSource(path);
         if (diagListener != null) {
+            report(DiagnosticType.NOTE, null, prefix + ": " + msg);
+            return;
+        }
+
+        if (path == null) {
+            noticeWriter.println(msg);
+        } else {
+            noticeWriter.println(prefix + ": " + msg);
+        }
+        noticeWriter.flush();
+    }
+
+    public void printNotice(Element e, String msg) {
+        String pos = getDiagSource(e);
+        if (diagListener != null) {
             report(DiagnosticType.NOTE, pos, msg);
             return;
         }
 
-        if (pos == null)
+        if (e == null) {
             noticeWriter.println(msg);
-        else
+        } else {
             noticeWriter.println(pos + ": " + msg);
+        }
         noticeWriter.flush();
     }
 
     /**
      * Print error message, increment error count.
      *
      * @param key selects message from resource
      */
-    public void error(SourcePosition pos, String key, Object... args) {
-        printError(pos, getText(key, args));
+    public void error(Element e, String key, Object... args) {
+        printError(e, getText(key, args));
     }
 
     /**
+     * Print error message, increment error count.
+     *
+     * @param key selects message from resource
+     */
+    public void error(DocTreePath path, String key, Object... args) {
+        printError(path, getText(key, args));
+    }
+
+    public void error(String key, Object... args) {
+        printError((Element)null, getText(key, args));
+    }
+
+    public void warning(String key, Object... args) {
+        printWarning((Element)null, getText(key, args));
+    }
+
+    /**
      * Print warning message, increment warning count.
      *
      * @param key selects message from resource
      */
-    public void warning(SourcePosition pos, String key, Object... args) {
-        printWarning(pos, getText(key, args));
+    public void warning(Element e, String key, Object... args) {
+        printWarning(e, getText(key, args));
     }
 
     /**
+     * Print warning message, increment warning count.
+     *
+     * @param key selects message from resource
+     */
+    public void warning(DocTreePath path, String key, Object... args) {
+        printWarning(path, getText(key, args));
+    }
+
+    /**
      * Print a message.
      *
      * @param key selects message from resource
      */
     public void notice(String key, Object... args) {

@@ -301,11 +423,11 @@
      */
     public void exit() {
         throw new ExitJavadoc();
     }
 
-    private void report(DiagnosticType type, SourcePosition pos, String msg) {
+    private void report(DiagnosticType type, String pos, String msg) {
         switch (type) {
             case ERROR:
             case WARNING:
                 Object prefix = (pos == null) ? programName : pos;
                 report(javadocDiags.create(type, null, null, "msg", prefix, msg));