Fri, 30 Nov 2012 15:14:48 +0000
8002099: Add support for intersection types in cast expression
Summary: Add parser and type-checking support for intersection types in cast expressions
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
26 package com.sun.tools.javadoc;
28 import java.io.PrintWriter;
29 import java.text.MessageFormat;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
33 import com.sun.javadoc.*;
34 import com.sun.tools.javac.util.Context;
35 import com.sun.tools.javac.util.JCDiagnostic;
36 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
37 import com.sun.tools.javac.util.JavacMessages;
38 import com.sun.tools.javac.util.Log;
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 *
47 * <p><b>This is NOT part of any supported API.
48 * If you write code that depends on this, you do so at your own risk.
49 * This code and its internal interfaces are subject to change or
50 * deletion without notice.</b>
51 *
52 * @see java.util.ResourceBundle
53 * @see java.text.MessageFormat
54 * @author Neal Gafter (rewrite)
55 */
56 public class Messager extends Log implements DocErrorReporter {
57 public static final SourcePosition NOPOS = null;
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 }
67 public static void preRegister(Context context,
68 final String programName) {
69 context.put(logKey, new Context.Factory<Log>() {
70 public Log make(Context c) {
71 return new Messager(c,
72 programName);
73 }
74 });
75 }
76 public static void preRegister(Context context,
77 final String programName,
78 final PrintWriter errWriter,
79 final PrintWriter warnWriter,
80 final PrintWriter noticeWriter) {
81 context.put(logKey, new Context.Factory<Log>() {
82 public Log make(Context c) {
83 return new Messager(c,
84 programName,
85 errWriter,
86 warnWriter,
87 noticeWriter);
88 }
89 });
90 }
92 public class ExitJavadoc extends Error {
93 private static final long serialVersionUID = 0;
94 }
96 final String programName;
98 private Locale locale;
99 private final JavacMessages messages;
100 private final JCDiagnostic.Factory javadocDiags;
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);
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 }
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 */
123 @SuppressWarnings("deprecation")
124 protected Messager(Context context,
125 String programName,
126 PrintWriter errWriter,
127 PrintWriter warnWriter,
128 PrintWriter noticeWriter) {
129 super(context, errWriter, warnWriter, noticeWriter);
130 messages = JavacMessages.instance(context);
131 messages.add("com.sun.tools.javadoc.resources.javadoc");
132 javadocDiags = new JCDiagnostic.Factory(messages, "javadoc");
133 this.programName = programName;
134 }
136 @Override
137 protected int getDefaultMaxErrors() {
138 return Integer.MAX_VALUE;
139 }
141 @Override
142 protected int getDefaultMaxWarnings() {
143 return Integer.MAX_VALUE;
144 }
146 public void setLocale(Locale locale) {
147 this.locale = locale;
148 }
150 /**
151 * get and format message string from resource
152 *
153 * @param key selects message from resource
154 * @param args arguments for the message
155 */
156 String getText(String key, Object... args) {
157 return messages.getLocalizedString(locale, key, args);
158 }
160 /**
161 * Print error message, increment error count.
162 * Part of DocErrorReporter.
163 *
164 * @param msg message to print
165 */
166 public void printError(String msg) {
167 printError(null, msg);
168 }
170 /**
171 * Print error message, increment error count.
172 * Part of DocErrorReporter.
173 *
174 * @param pos the position where the error occurs
175 * @param msg message to print
176 */
177 public void printError(SourcePosition pos, String msg) {
178 if (diagListener != null) {
179 report(DiagnosticType.ERROR, pos, msg);
180 return;
181 }
183 if (nerrors < MaxErrors) {
184 String prefix = (pos == null) ? programName : pos.toString();
185 errWriter.println(prefix + ": " + getText("javadoc.error") + " - " + msg);
186 errWriter.flush();
187 prompt();
188 nerrors++;
189 }
190 }
192 /**
193 * Print warning message, increment warning count.
194 * Part of DocErrorReporter.
195 *
196 * @param msg message to print
197 */
198 public void printWarning(String msg) {
199 printWarning(null, msg);
200 }
202 /**
203 * Print warning message, increment warning count.
204 * Part of DocErrorReporter.
205 *
206 * @param pos the position where the error occurs
207 * @param msg message to print
208 */
209 public void printWarning(SourcePosition pos, String msg) {
210 if (diagListener != null) {
211 report(DiagnosticType.WARNING, pos, msg);
212 return;
213 }
215 if (nwarnings < MaxWarnings) {
216 String prefix = (pos == null) ? programName : pos.toString();
217 warnWriter.println(prefix + ": " + getText("javadoc.warning") +" - " + msg);
218 warnWriter.flush();
219 nwarnings++;
220 }
221 }
223 /**
224 * Print a message.
225 * Part of DocErrorReporter.
226 *
227 * @param msg message to print
228 */
229 public void printNotice(String msg) {
230 printNotice(null, msg);
231 }
233 /**
234 * Print a message.
235 * Part of DocErrorReporter.
236 *
237 * @param pos the position where the error occurs
238 * @param msg message to print
239 */
240 public void printNotice(SourcePosition pos, String msg) {
241 if (diagListener != null) {
242 report(DiagnosticType.NOTE, pos, msg);
243 return;
244 }
246 if (pos == null)
247 noticeWriter.println(msg);
248 else
249 noticeWriter.println(pos + ": " + msg);
250 noticeWriter.flush();
251 }
253 /**
254 * Print error message, increment error count.
255 *
256 * @param key selects message from resource
257 */
258 public void error(SourcePosition pos, String key, Object... args) {
259 printError(pos, getText(key, args));
260 }
262 /**
263 * Print warning message, increment warning count.
264 *
265 * @param key selects message from resource
266 */
267 public void warning(SourcePosition pos, String key, Object... args) {
268 printWarning(pos, getText(key, args));
269 }
271 /**
272 * Print a message.
273 *
274 * @param key selects message from resource
275 */
276 public void notice(String key, Object... args) {
277 printNotice(getText(key, args));
278 }
280 /**
281 * Return total number of errors, including those recorded
282 * in the compilation log.
283 */
284 public int nerrors() { return nerrors; }
286 /**
287 * Return total number of warnings, including those recorded
288 * in the compilation log.
289 */
290 public int nwarnings() { return nwarnings; }
292 /**
293 * Print exit message.
294 */
295 public void exitNotice() {
296 if (nerrors > 0) {
297 notice((nerrors > 1) ? "main.errors" : "main.error",
298 "" + nerrors);
299 }
300 if (nwarnings > 0) {
301 notice((nwarnings > 1) ? "main.warnings" : "main.warning",
302 "" + nwarnings);
303 }
304 }
306 /**
307 * Force program exit, e.g., from a fatal error.
308 * <p>
309 * TODO: This method does not really belong here.
310 */
311 public void exit() {
312 throw new ExitJavadoc();
313 }
315 private void report(DiagnosticType type, SourcePosition pos, String msg) {
316 switch (type) {
317 case ERROR:
318 case WARNING:
319 Object prefix = (pos == null) ? programName : pos;
320 report(javadocDiags.create(type, null, null, "msg", prefix, msg));
321 break;
323 case NOTE:
324 String key = (pos == null) ? "msg" : "pos.msg";
325 report(javadocDiags.create(type, null, null, key, pos, msg));
326 break;
328 default:
329 throw new IllegalArgumentException(type.toString());
330 }
331 }
332 }