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

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1755
ddb4a2bfcd82
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

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

mercurial