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