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

Thu, 28 Mar 2013 10:49:39 -0700

author
jjg
date
Thu, 28 Mar 2013 10:49:39 -0700
changeset 1668
991f11e13598
parent 1483
d2eb08b3f64f
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8006346: doclint should make allowance for headers generated by standard doclet
Reviewed-by: mcimadamore

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

mercurial