Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javadoc/Messager.java
38899 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javadoc;2627import java.io.PrintWriter;28import java.util.Locale;2930import com.sun.javadoc.*;31import com.sun.tools.javac.util.Context;32import com.sun.tools.javac.util.JCDiagnostic;33import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;34import com.sun.tools.javac.util.JavacMessages;35import com.sun.tools.javac.util.Log;3637/**38* Utility for integrating with javadoc tools and for localization.39* Handle Resources. Access to error and warning counts.40* Message formatting.41* <br>42* Also provides implementation for DocErrorReporter.43*44* <p><b>This is NOT part of any supported API.45* If you write code that depends on this, you do so at your own risk.46* This code and its internal interfaces are subject to change or47* deletion without notice.</b>48*49* @see java.util.ResourceBundle50* @see java.text.MessageFormat51* @author Neal Gafter (rewrite)52*/53public class Messager extends Log implements DocErrorReporter {54public static final SourcePosition NOPOS = null;5556/** Get the current messager, which is also the compiler log. */57public static Messager instance0(Context context) {58Log instance = context.get(logKey);59if (instance == null || !(instance instanceof Messager))60throw new InternalError("no messager instance!");61return (Messager)instance;62}6364public static void preRegister(Context context,65final String programName) {66context.put(logKey, new Context.Factory<Log>() {67public Log make(Context c) {68return new Messager(c,69programName);70}71});72}73public static void preRegister(Context context,74final String programName,75final PrintWriter errWriter,76final PrintWriter warnWriter,77final PrintWriter noticeWriter) {78context.put(logKey, new Context.Factory<Log>() {79public Log make(Context c) {80return new Messager(c,81programName,82errWriter,83warnWriter,84noticeWriter);85}86});87}8889public class ExitJavadoc extends Error {90private static final long serialVersionUID = 0;91}9293final String programName;9495private Locale locale;96private final JavacMessages messages;97private final JCDiagnostic.Factory javadocDiags;9899/** The default writer for diagnostics100*/101static final PrintWriter defaultErrWriter = new PrintWriter(System.err);102static final PrintWriter defaultWarnWriter = new PrintWriter(System.err);103static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out);104105/**106* Constructor107* @param programName Name of the program (for error messages).108*/109protected Messager(Context context, String programName) {110this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter);111}112113/**114* Constructor115* @param programName Name of the program (for error messages).116* @param errWriter Stream for error messages117* @param warnWriter Stream for warnings118* @param noticeWriter Stream for other messages119*/120@SuppressWarnings("deprecation")121protected Messager(Context context,122String programName,123PrintWriter errWriter,124PrintWriter warnWriter,125PrintWriter noticeWriter) {126super(context, errWriter, warnWriter, noticeWriter);127messages = JavacMessages.instance(context);128messages.add("com.sun.tools.javadoc.resources.javadoc");129javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");130this.programName = programName;131}132133public void setLocale(Locale locale) {134this.locale = locale;135}136137/**138* get and format message string from resource139*140* @param key selects message from resource141* @param args arguments for the message142*/143String getText(String key, Object... args) {144return messages.getLocalizedString(locale, key, args);145}146147/**148* Print error message, increment error count.149* Part of DocErrorReporter.150*151* @param msg message to print152*/153public void printError(String msg) {154printError(null, msg);155}156157/**158* Print error message, increment error count.159* Part of DocErrorReporter.160*161* @param pos the position where the error occurs162* @param msg message to print163*/164public void printError(SourcePosition pos, String msg) {165if (diagListener != null) {166report(DiagnosticType.ERROR, pos, msg);167return;168}169170if (nerrors < MaxErrors) {171String prefix = (pos == null) ? programName : pos.toString();172errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);173errWriter.flush();174prompt();175nerrors++;176}177}178179/**180* Print warning message, increment warning count.181* Part of DocErrorReporter.182*183* @param msg message to print184*/185public void printWarning(String msg) {186printWarning(null, msg);187}188189/**190* Print warning message, increment warning count.191* Part of DocErrorReporter.192*193* @param pos the position where the error occurs194* @param msg message to print195*/196public void printWarning(SourcePosition pos, String msg) {197if (diagListener != null) {198report(DiagnosticType.WARNING, pos, msg);199return;200}201202if (nwarnings < MaxWarnings) {203String prefix = (pos == null) ? programName : pos.toString();204warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg);205warnWriter.flush();206nwarnings++;207}208}209210/**211* Print a message.212* Part of DocErrorReporter.213*214* @param msg message to print215*/216public void printNotice(String msg) {217printNotice(null, msg);218}219220/**221* Print a message.222* Part of DocErrorReporter.223*224* @param pos the position where the error occurs225* @param msg message to print226*/227public void printNotice(SourcePosition pos, String msg) {228if (diagListener != null) {229report(DiagnosticType.NOTE, pos, msg);230return;231}232233if (pos == null)234noticeWriter.println(msg);235else236noticeWriter.println(pos + ": " + msg);237noticeWriter.flush();238}239240/**241* Print error message, increment error count.242*243* @param key selects message from resource244*/245public void error(SourcePosition pos, String key, Object... args) {246printError(pos, getText(key, args));247}248249/**250* Print warning message, increment warning count.251*252* @param key selects message from resource253*/254public void warning(SourcePosition pos, String key, Object... args) {255printWarning(pos, getText(key, args));256}257258/**259* Print a message.260*261* @param key selects message from resource262*/263public void notice(String key, Object... args) {264printNotice(getText(key, args));265}266267/**268* Return total number of errors, including those recorded269* in the compilation log.270*/271public int nerrors() { return nerrors; }272273/**274* Return total number of warnings, including those recorded275* in the compilation log.276*/277public int nwarnings() { return nwarnings; }278279/**280* Print exit message.281*/282public void exitNotice() {283if (nerrors > 0) {284notice((nerrors > 1) ? "main.errors" : "main.error",285"" + nerrors);286}287if (nwarnings > 0) {288notice((nwarnings > 1) ? "main.warnings" : "main.warning",289"" + nwarnings);290}291}292293/**294* Force program exit, e.g., from a fatal error.295* <p>296* TODO: This method does not really belong here.297*/298public void exit() {299throw new ExitJavadoc();300}301302private void report(DiagnosticType type, SourcePosition pos, String msg) {303switch (type) {304case ERROR:305case WARNING:306Object prefix = (pos == null) ? programName : pos;307report(javadocDiags.create(type, null, null, "msg", prefix, msg));308break;309310case NOTE:311String key = (pos == null) ? "msg" : "pos.msg";312report(javadocDiags.create(type, null, null, key, pos, msg));313break;314315default:316throw new IllegalArgumentException(type.toString());317}318}319}320321322