author | bpatel |
Sat, 29 Mar 2014 11:06:33 -0700 | |
changeset 23799 | daa645653200 |
parent 17578 | 46ac954e4a84 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15041 | 2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javadoc; |
|
27 |
||
14543 | 28 |
import java.io.PrintWriter; |
29 |
import java.util.Locale; |
|
10 | 30 |
|
31 |
import com.sun.javadoc.*; |
|
32 |
import com.sun.tools.javac.util.Context; |
|
14543 | 33 |
import com.sun.tools.javac.util.JCDiagnostic; |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
34 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
14543 | 35 |
import com.sun.tools.javac.util.JavacMessages; |
14258 | 36 |
import com.sun.tools.javac.util.Log; |
10 | 37 |
|
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 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
45 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
46 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
47 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
48 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
49 |
* |
10 | 50 |
* @see java.util.ResourceBundle |
51 |
* @see java.text.MessageFormat |
|
52 |
* @author Neal Gafter (rewrite) |
|
53 |
*/ |
|
54 |
public class Messager extends Log implements DocErrorReporter { |
|
14543 | 55 |
public static final SourcePosition NOPOS = null; |
10 | 56 |
|
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 |
} |
|
64 |
||
8614 | 65 |
public static void preRegister(Context context, |
10 | 66 |
final String programName) { |
67 |
context.put(logKey, new Context.Factory<Log>() { |
|
8614 | 68 |
public Log make(Context c) { |
69 |
return new Messager(c, |
|
10 | 70 |
programName); |
71 |
} |
|
72 |
}); |
|
73 |
} |
|
8614 | 74 |
public static void preRegister(Context context, |
10 | 75 |
final String programName, |
76 |
final PrintWriter errWriter, |
|
77 |
final PrintWriter warnWriter, |
|
78 |
final PrintWriter noticeWriter) { |
|
79 |
context.put(logKey, new Context.Factory<Log>() { |
|
8614 | 80 |
public Log make(Context c) { |
81 |
return new Messager(c, |
|
10 | 82 |
programName, |
83 |
errWriter, |
|
84 |
warnWriter, |
|
85 |
noticeWriter); |
|
86 |
} |
|
87 |
}); |
|
88 |
} |
|
89 |
||
90 |
public class ExitJavadoc extends Error { |
|
91 |
private static final long serialVersionUID = 0; |
|
92 |
} |
|
93 |
||
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
94 |
final String programName; |
10 | 95 |
|
14543 | 96 |
private Locale locale; |
97 |
private final JavacMessages messages; |
|
98 |
private final JCDiagnostic.Factory javadocDiags; |
|
10 | 99 |
|
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); |
|
105 |
||
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 |
} |
|
113 |
||
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 |
*/ |
|
1870
57a1138dffc8
6795903: fix latent build warnings in langtools repository
jjg
parents:
10
diff
changeset
|
121 |
@SuppressWarnings("deprecation") |
10 | 122 |
protected Messager(Context context, |
123 |
String programName, |
|
124 |
PrintWriter errWriter, |
|
125 |
PrintWriter warnWriter, |
|
126 |
PrintWriter noticeWriter) { |
|
127 |
super(context, errWriter, warnWriter, noticeWriter); |
|
14543 | 128 |
messages = JavacMessages.instance(context); |
129 |
messages.add("com.sun.tools.javadoc.resources.javadoc"); |
|
130 |
javadocDiags = new JCDiagnostic.Factory(messages, "javadoc"); |
|
10 | 131 |
this.programName = programName; |
132 |
} |
|
133 |
||
14543 | 134 |
public void setLocale(Locale locale) { |
135 |
this.locale = locale; |
|
10 | 136 |
} |
137 |
||
138 |
/** |
|
139 |
* get and format message string from resource |
|
140 |
* |
|
141 |
* @param key selects message from resource |
|
14543 | 142 |
* @param args arguments for the message |
10 | 143 |
*/ |
14543 | 144 |
String getText(String key, Object... args) { |
145 |
return messages.getLocalizedString(locale, key, args); |
|
10 | 146 |
} |
147 |
||
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 |
} |
|
157 |
||
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) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
166 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
167 |
report(DiagnosticType.ERROR, pos, msg); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
168 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
169 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
170 |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
171 |
if (nerrors < MaxErrors) { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
172 |
String prefix = (pos == null) ? programName : pos.toString(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
173 |
errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
174 |
errWriter.flush(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
175 |
prompt(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
176 |
nerrors++; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
177 |
} |
10 | 178 |
} |
179 |
||
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 |
} |
|
189 |
||
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) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
198 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
199 |
report(DiagnosticType.WARNING, pos, msg); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
200 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
201 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
202 |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
203 |
if (nwarnings < MaxWarnings) { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
204 |
String prefix = (pos == null) ? programName : pos.toString(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
205 |
warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
206 |
warnWriter.flush(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
207 |
nwarnings++; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
208 |
} |
10 | 209 |
} |
210 |
||
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 |
} |
|
220 |
||
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) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
229 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
230 |
report(DiagnosticType.NOTE, pos, msg); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
231 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
232 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
233 |
|
10 | 234 |
if (pos == null) |
235 |
noticeWriter.println(msg); |
|
236 |
else |
|
237 |
noticeWriter.println(pos + ": " + msg); |
|
238 |
noticeWriter.flush(); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Print error message, increment error count. |
|
243 |
* |
|
244 |
* @param key selects message from resource |
|
245 |
*/ |
|
14543 | 246 |
public void error(SourcePosition pos, String key, Object... args) { |
247 |
printError(pos, getText(key, args)); |
|
10 | 248 |
} |
249 |
||
250 |
/** |
|
251 |
* Print warning message, increment warning count. |
|
252 |
* |
|
253 |
* @param key selects message from resource |
|
254 |
*/ |
|
14543 | 255 |
public void warning(SourcePosition pos, String key, Object... args) { |
256 |
printWarning(pos, getText(key, args)); |
|
10 | 257 |
} |
258 |
||
259 |
/** |
|
260 |
* Print a message. |
|
261 |
* |
|
262 |
* @param key selects message from resource |
|
263 |
*/ |
|
14543 | 264 |
public void notice(String key, Object... args) { |
265 |
printNotice(getText(key, args)); |
|
10 | 266 |
} |
267 |
||
268 |
/** |
|
269 |
* Return total number of errors, including those recorded |
|
270 |
* in the compilation log. |
|
271 |
*/ |
|
272 |
public int nerrors() { return nerrors; } |
|
273 |
||
274 |
/** |
|
275 |
* Return total number of warnings, including those recorded |
|
276 |
* in the compilation log. |
|
277 |
*/ |
|
278 |
public int nwarnings() { return nwarnings; } |
|
279 |
||
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 |
} |
|
293 |
||
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 |
} |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
302 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
303 |
private void report(DiagnosticType type, SourcePosition pos, String msg) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
304 |
switch (type) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
305 |
case ERROR: |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
306 |
case WARNING: |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
307 |
Object prefix = (pos == null) ? programName : pos; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
308 |
report(javadocDiags.create(type, null, null, "msg", prefix, msg)); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
309 |
break; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
310 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
311 |
case NOTE: |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
312 |
String key = (pos == null) ? "msg" : "pos.msg"; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
313 |
report(javadocDiags.create(type, null, null, key, pos, msg)); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
314 |
break; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
315 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
316 |
default: |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
317 |
throw new IllegalArgumentException(type.toString()); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
318 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
319 |
} |
10 | 320 |
} |