src/share/classes/com/sun/tools/javadoc/Messager.java

Tue, 14 May 2013 15:04:06 -0700

author
jjg
date
Tue, 14 May 2013 15:04:06 -0700
changeset 1755
ddb4a2bfcd82
parent 1483
d2eb08b3f64f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8013852: update reference impl for type-annotations
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com, joel.franck@oracle.com, alex.buckley@oracle.com

     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javadoc;
    28 import java.io.PrintWriter;
    29 import java.util.Locale;
    31 import com.sun.javadoc.*;
    32 import com.sun.tools.javac.util.Context;
    33 import com.sun.tools.javac.util.JCDiagnostic;
    34 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
    35 import com.sun.tools.javac.util.JavacMessages;
    36 import com.sun.tools.javac.util.Log;
    38 /**
    39  * Utility for integrating with javadoc tools and for localization.
    40  * Handle Resources. Access to error and warning counts.
    41  * Message formatting.
    42  * <br>
    43  * Also provides implementation for DocErrorReporter.
    44  *
    45  *  <p><b>This is NOT part of any supported API.
    46  *  If you write code that depends on this, you do so at your own risk.
    47  *  This code and its internal interfaces are subject to change or
    48  *  deletion without notice.</b>
    49  *
    50  * @see java.util.ResourceBundle
    51  * @see java.text.MessageFormat
    52  * @author Neal Gafter (rewrite)
    53  */
    54 public class Messager extends Log implements DocErrorReporter {
    55     public static final SourcePosition NOPOS = null;
    57     /** Get the current messager, which is also the compiler log. */
    58     public static Messager instance0(Context context) {
    59         Log instance = context.get(logKey);
    60         if (instance == null || !(instance instanceof Messager))
    61             throw new InternalError("no messager instance!");
    62         return (Messager)instance;
    63     }
    65     public static void preRegister(Context context,
    66                                    final String programName) {
    67         context.put(logKey, new Context.Factory<Log>() {
    68             public Log make(Context c) {
    69                 return new Messager(c,
    70                                     programName);
    71             }
    72         });
    73     }
    74     public static void preRegister(Context context,
    75                                    final String programName,
    76                                    final PrintWriter errWriter,
    77                                    final PrintWriter warnWriter,
    78                                    final PrintWriter noticeWriter) {
    79         context.put(logKey, new Context.Factory<Log>() {
    80             public Log make(Context c) {
    81                 return new Messager(c,
    82                                     programName,
    83                                     errWriter,
    84                                     warnWriter,
    85                                     noticeWriter);
    86             }
    87         });
    88     }
    90     public class ExitJavadoc extends Error {
    91         private static final long serialVersionUID = 0;
    92     }
    94     final String programName;
    96     private Locale locale;
    97     private final JavacMessages messages;
    98     private final JCDiagnostic.Factory javadocDiags;
   100     /** The default writer for diagnostics
   101      */
   102     static final PrintWriter defaultErrWriter = new PrintWriter(System.err);
   103     static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);
   104     static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);
   106     /**
   107      * Constructor
   108      * @param programName  Name of the program (for error messages).
   109      */
   110     protected Messager(Context context, String programName) {
   111         this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);
   112     }
   114     /**
   115      * Constructor
   116      * @param programName  Name of the program (for error messages).
   117      * @param errWriter    Stream for error messages
   118      * @param warnWriter   Stream for warnings
   119      * @param noticeWriter Stream for other messages
   120      */
   121     @SuppressWarnings("deprecation")
   122     protected Messager(Context context,
   123                        String programName,
   124                        PrintWriter errWriter,
   125                        PrintWriter warnWriter,
   126                        PrintWriter noticeWriter) {
   127         super(context, errWriter, warnWriter, noticeWriter);
   128         messages = JavacMessages.instance(context);
   129         messages.add("com.sun.tools.javadoc.resources.javadoc");
   130         javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
   131         this.programName = programName;
   132     }
   134     public void setLocale(Locale locale) {
   135         this.locale = locale;
   136     }
   138     /**
   139      * get and format message string from resource
   140      *
   141      * @param key selects message from resource
   142      * @param args arguments for the message
   143      */
   144     String getText(String key, Object... args) {
   145         return messages.getLocalizedString(locale, key, args);
   146     }
   148     /**
   149      * Print error message, increment error count.
   150      * Part of DocErrorReporter.
   151      *
   152      * @param msg message to print
   153      */
   154     public void printError(String msg) {
   155         printError(null, msg);
   156     }
   158     /**
   159      * Print error message, increment error count.
   160      * Part of DocErrorReporter.
   161      *
   162      * @param pos the position where the error occurs
   163      * @param msg message to print
   164      */
   165     public void printError(SourcePosition pos, String msg) {
   166         if (diagListener != null) {
   167             report(DiagnosticType.ERROR, pos, msg);
   168             return;
   169         }
   171         if (nerrors < MaxErrors) {
   172             String prefix = (pos == null) ? programName : pos.toString();
   173             errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
   174             errWriter.flush();
   175             prompt();
   176             nerrors++;
   177         }
   178     }
   180     /**
   181      * Print warning message, increment warning count.
   182      * Part of DocErrorReporter.
   183      *
   184      * @param msg message to print
   185      */
   186     public void printWarning(String msg) {
   187         printWarning(null, msg);
   188     }
   190     /**
   191      * Print warning message, increment warning count.
   192      * Part of DocErrorReporter.
   193      *
   194      * @param pos the position where the error occurs
   195      * @param msg message to print
   196      */
   197     public void printWarning(SourcePosition pos, String msg) {
   198         if (diagListener != null) {
   199             report(DiagnosticType.WARNING, pos, msg);
   200             return;
   201         }
   203         if (nwarnings < MaxWarnings) {
   204             String prefix = (pos == null) ? programName : pos.toString();
   205             warnWriter.println(prefix +  ": " + getText("javadoc.warning") +" - " + msg);
   206             warnWriter.flush();
   207             nwarnings++;
   208         }
   209     }
   211     /**
   212      * Print a message.
   213      * Part of DocErrorReporter.
   214      *
   215      * @param msg message to print
   216      */
   217     public void printNotice(String msg) {
   218         printNotice(null, msg);
   219     }
   221     /**
   222      * Print a message.
   223      * Part of DocErrorReporter.
   224      *
   225      * @param pos the position where the error occurs
   226      * @param msg message to print
   227      */
   228     public void printNotice(SourcePosition pos, String msg) {
   229         if (diagListener != null) {
   230             report(DiagnosticType.NOTE, pos, msg);
   231             return;
   232         }
   234         if (pos == null)
   235             noticeWriter.println(msg);
   236         else
   237             noticeWriter.println(pos + ": " + msg);
   238         noticeWriter.flush();
   239     }
   241     /**
   242      * Print error message, increment error count.
   243      *
   244      * @param key selects message from resource
   245      */
   246     public void error(SourcePosition pos, String key, Object... args) {
   247         printError(pos, getText(key, args));
   248     }
   250     /**
   251      * Print warning message, increment warning count.
   252      *
   253      * @param key selects message from resource
   254      */
   255     public void warning(SourcePosition pos, String key, Object... args) {
   256         printWarning(pos, getText(key, args));
   257     }
   259     /**
   260      * Print a message.
   261      *
   262      * @param key selects message from resource
   263      */
   264     public void notice(String key, Object... args) {
   265         printNotice(getText(key, args));
   266     }
   268     /**
   269      * Return total number of errors, including those recorded
   270      * in the compilation log.
   271      */
   272     public int nerrors() { return nerrors; }
   274     /**
   275      * Return total number of warnings, including those recorded
   276      * in the compilation log.
   277      */
   278     public int nwarnings() { return nwarnings; }
   280     /**
   281      * Print exit message.
   282      */
   283     public void exitNotice() {
   284         if (nerrors > 0) {
   285             notice((nerrors > 1) ? "main.errors" : "main.error",
   286                    "" + nerrors);
   287         }
   288         if (nwarnings > 0) {
   289             notice((nwarnings > 1) ?  "main.warnings" : "main.warning",
   290                    "" + nwarnings);
   291         }
   292     }
   294     /**
   295      * Force program exit, e.g., from a fatal error.
   296      * <p>
   297      * TODO: This method does not really belong here.
   298      */
   299     public void exit() {
   300         throw new ExitJavadoc();
   301     }
   303     private void report(DiagnosticType type, SourcePosition pos, String msg) {
   304         switch (type) {
   305             case ERROR:
   306             case WARNING:
   307                 Object prefix = (pos == null) ? programName : pos;
   308                 report(javadocDiags.create(type, null, null, "msg", prefix, msg));
   309                 break;
   311             case NOTE:
   312                 String key = (pos == null) ? "msg" : "pos.msg";
   313                 report(javadocDiags.create(type, null, null, key, pos, msg));
   314                 break;
   316             default:
   317                 throw new IllegalArgumentException(type.toString());
   318         }
   319     }
   320 }

mercurial