author | ksrini |
Fri, 09 Feb 2018 13:58:17 -0800 | |
changeset 48840 | 5e2d2067da48 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
48840
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, 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 |
||
37938
42baa89d2156
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents:
28892
diff
changeset
|
26 |
package com.sun.tools.javadoc.main; |
10 | 27 |
|
14543 | 28 |
import java.io.PrintWriter; |
29 |
import java.util.Locale; |
|
28892
7e09121db885
8071836: javadoc fails as javadoc resource bundle not visible to com.sun.tools.javac.util.JavacMessages
ksrini
parents:
25874
diff
changeset
|
30 |
import java.util.ResourceBundle; |
10 | 31 |
|
32 |
import com.sun.javadoc.*; |
|
33 |
import com.sun.tools.javac.util.Context; |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
34 |
import com.sun.tools.javac.util.Context.Factory; |
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 |
*/ |
|
48840
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
56 |
@Deprecated(since="9", forRemoval=true) |
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
57 |
@SuppressWarnings("removal") |
10 | 58 |
public class Messager extends Log implements DocErrorReporter { |
14543 | 59 |
public static final SourcePosition NOPOS = null; |
10 | 60 |
|
61 |
/** Get the current messager, which is also the compiler log. */ |
|
62 |
public static Messager instance0(Context context) { |
|
63 |
Log instance = context.get(logKey); |
|
64 |
if (instance == null || !(instance instanceof Messager)) |
|
65 |
throw new InternalError("no messager instance!"); |
|
66 |
return (Messager)instance; |
|
67 |
} |
|
68 |
||
8614 | 69 |
public static void preRegister(Context context, |
10 | 70 |
final String programName) { |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
71 |
context.put(logKey, (Factory<Log>)c -> new Messager(c, programName)); |
10 | 72 |
} |
8614 | 73 |
public static void preRegister(Context context, |
10 | 74 |
final String programName, |
75 |
final PrintWriter errWriter, |
|
76 |
final PrintWriter warnWriter, |
|
77 |
final PrintWriter noticeWriter) { |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
78 |
context.put(logKey, (Factory<Log>)c -> new Messager(c, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
79 |
programName, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
80 |
errWriter, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
81 |
warnWriter, |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40232
diff
changeset
|
82 |
noticeWriter)); |
10 | 83 |
} |
84 |
||
85 |
public class ExitJavadoc extends Error { |
|
86 |
private static final long serialVersionUID = 0; |
|
87 |
} |
|
88 |
||
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
89 |
final String programName; |
10 | 90 |
|
14543 | 91 |
private Locale locale; |
92 |
private final JavacMessages messages; |
|
93 |
private final JCDiagnostic.Factory javadocDiags; |
|
10 | 94 |
|
95 |
/** The default writer for diagnostics |
|
96 |
*/ |
|
97 |
static final PrintWriter defaultErrWriter = new PrintWriter(System.err); |
|
98 |
static final PrintWriter defaultWarnWriter = new PrintWriter(System.err); |
|
99 |
static final PrintWriter defaultNoticeWriter = new PrintWriter(System.out); |
|
100 |
||
101 |
/** |
|
102 |
* Constructor |
|
103 |
* @param programName Name of the program (for error messages). |
|
104 |
*/ |
|
105 |
protected Messager(Context context, String programName) { |
|
106 |
this(context, programName, defaultErrWriter, defaultWarnWriter, defaultNoticeWriter); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Constructor |
|
111 |
* @param programName Name of the program (for error messages). |
|
112 |
* @param errWriter Stream for error messages |
|
113 |
* @param warnWriter Stream for warnings |
|
114 |
* @param noticeWriter Stream for other messages |
|
115 |
*/ |
|
1870
57a1138dffc8
6795903: fix latent build warnings in langtools repository
jjg
parents:
10
diff
changeset
|
116 |
@SuppressWarnings("deprecation") |
10 | 117 |
protected Messager(Context context, |
118 |
String programName, |
|
119 |
PrintWriter errWriter, |
|
120 |
PrintWriter warnWriter, |
|
121 |
PrintWriter noticeWriter) { |
|
122 |
super(context, errWriter, warnWriter, noticeWriter); |
|
14543 | 123 |
messages = JavacMessages.instance(context); |
28892
7e09121db885
8071836: javadoc fails as javadoc resource bundle not visible to com.sun.tools.javac.util.JavacMessages
ksrini
parents:
25874
diff
changeset
|
124 |
messages.add(locale -> ResourceBundle.getBundle("com.sun.tools.javadoc.resources.javadoc", |
7e09121db885
8071836: javadoc fails as javadoc resource bundle not visible to com.sun.tools.javac.util.JavacMessages
ksrini
parents:
25874
diff
changeset
|
125 |
locale)); |
14543 | 126 |
javadocDiags = new JCDiagnostic.Factory(messages, "javadoc"); |
10 | 127 |
this.programName = programName; |
28892
7e09121db885
8071836: javadoc fails as javadoc resource bundle not visible to com.sun.tools.javac.util.JavacMessages
ksrini
parents:
25874
diff
changeset
|
128 |
|
10 | 129 |
} |
130 |
||
14543 | 131 |
public void setLocale(Locale locale) { |
132 |
this.locale = locale; |
|
10 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* get and format message string from resource |
|
137 |
* |
|
138 |
* @param key selects message from resource |
|
14543 | 139 |
* @param args arguments for the message |
10 | 140 |
*/ |
14543 | 141 |
String getText(String key, Object... args) { |
142 |
return messages.getLocalizedString(locale, key, args); |
|
10 | 143 |
} |
144 |
||
145 |
/** |
|
146 |
* Print error message, increment error count. |
|
147 |
* Part of DocErrorReporter. |
|
148 |
* |
|
149 |
* @param msg message to print |
|
150 |
*/ |
|
151 |
public void printError(String msg) { |
|
152 |
printError(null, msg); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Print error message, increment error count. |
|
157 |
* Part of DocErrorReporter. |
|
158 |
* |
|
159 |
* @param pos the position where the error occurs |
|
160 |
* @param msg message to print |
|
161 |
*/ |
|
162 |
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
|
163 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
164 |
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
|
165 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
166 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
167 |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
168 |
if (nerrors < MaxErrors) { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
169 |
String prefix = (pos == null) ? programName : pos.toString(); |
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
38617
diff
changeset
|
170 |
PrintWriter errWriter = getWriter(WriterKind.ERROR); |
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
171 |
errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
172 |
errWriter.flush(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
173 |
prompt(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
174 |
nerrors++; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
175 |
} |
10 | 176 |
} |
177 |
||
178 |
/** |
|
179 |
* Print warning message, increment warning count. |
|
180 |
* Part of DocErrorReporter. |
|
181 |
* |
|
182 |
* @param msg message to print |
|
183 |
*/ |
|
184 |
public void printWarning(String msg) { |
|
185 |
printWarning(null, msg); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Print warning message, increment warning count. |
|
190 |
* Part of DocErrorReporter. |
|
191 |
* |
|
192 |
* @param pos the position where the error occurs |
|
193 |
* @param msg message to print |
|
194 |
*/ |
|
195 |
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
|
196 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
197 |
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
|
198 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
199 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
200 |
|
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
201 |
if (nwarnings < MaxWarnings) { |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
202 |
String prefix = (pos == null) ? programName : pos.toString(); |
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
38617
diff
changeset
|
203 |
PrintWriter warnWriter = getWriter(WriterKind.WARNING); |
5850
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
204 |
warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
205 |
warnWriter.flush(); |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
206 |
nwarnings++; |
6f095ff5b469
6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents:
5520
diff
changeset
|
207 |
} |
10 | 208 |
} |
209 |
||
210 |
/** |
|
211 |
* Print a message. |
|
212 |
* Part of DocErrorReporter. |
|
213 |
* |
|
214 |
* @param msg message to print |
|
215 |
*/ |
|
216 |
public void printNotice(String msg) { |
|
217 |
printNotice(null, msg); |
|
218 |
} |
|
219 |
||
220 |
/** |
|
221 |
* Print a message. |
|
222 |
* Part of DocErrorReporter. |
|
223 |
* |
|
224 |
* @param pos the position where the error occurs |
|
225 |
* @param msg message to print |
|
226 |
*/ |
|
227 |
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
|
228 |
if (diagListener != null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
229 |
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
|
230 |
return; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
231 |
} |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
232 |
|
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
38617
diff
changeset
|
233 |
PrintWriter noticeWriter = getWriter(WriterKind.NOTICE); |
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 |
} |