Thu, 10 Jun 2010 09:29:23 +0100
6945418: Project Coin: Simplified Varargs Method Invocation
Summary: Add new mandatory warning for unsafe vararg method declaration. Warning can be suppressed as usual (@SuppressWarnings("varargs")/-Xlint:-varargs)
Reviewed-by: jjg, darcy
1 /*
2 * Copyright (c) 1999, 2008, 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.javac.util;
28 import java.util.HashMap;
29 import java.util.Map;
30 import javax.tools.JavaFileObject;
32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
33 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
36 /**
37 * A base class for error logs. Reports errors and warnings, and
38 * keeps track of error numbers and positions.
39 *
40 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
41 * you write code that depends on this, you do so at your own risk.
42 * This code and its internal interfaces are subject to change or
43 * deletion without notice.</b>
44 */
45 public abstract class AbstractLog {
46 AbstractLog(JCDiagnostic.Factory diags) {
47 this.diags = diags;
48 sourceMap = new HashMap<JavaFileObject, DiagnosticSource>();
49 }
51 /** Re-assign source, returning previous setting.
52 */
53 public JavaFileObject useSource(JavaFileObject file) {
54 JavaFileObject prev = (source == null ? null : source.getFile());
55 source = getSource(file);
56 return prev;
57 }
59 protected DiagnosticSource getSource(JavaFileObject file) {
60 if (file == null)
61 return DiagnosticSource.NO_SOURCE;
62 DiagnosticSource s = sourceMap.get(file);
63 if (s == null) {
64 s = new DiagnosticSource(file, this);
65 sourceMap.put(file, s);
66 }
67 return s;
68 }
70 /** Return the underlying diagnostic source
71 */
72 public DiagnosticSource currentSource() {
73 return source;
74 }
76 /** Report an error, unless another error was already reported at same
77 * source position.
78 * @param key The key for the localized error message.
79 * @param args Fields of the error message.
80 */
81 public void error(String key, Object ... args) {
82 report(diags.error(source, null, key, args));
83 }
85 /** Report an error, unless another error was already reported at same
86 * source position.
87 * @param pos The source position at which to report the error.
88 * @param key The key for the localized error message.
89 * @param args Fields of the error message.
90 */
91 public void error(DiagnosticPosition pos, String key, Object ... args) {
92 report(diags.error(source, pos, key, args));
93 }
95 /** Report an error, unless another error was already reported at same
96 * source position.
97 * @param pos The source position at which to report the error.
98 * @param key The key for the localized error message.
99 * @param args Fields of the error message.
100 */
101 public void error(int pos, String key, Object ... args) {
102 report(diags.error(source, wrap(pos), key, args));
103 }
105 /** Report a warning, unless suppressed by the -nowarn option or the
106 * maximum number of warnings has been reached.
107 * @param pos The source position at which to report the warning.
108 * @param key The key for the localized warning message.
109 * @param args Fields of the warning message.
110 */
111 public void warning(String key, Object ... args) {
112 report(diags.warning(source, null, key, args));
113 }
115 /** Report a warning, unless suppressed by the -nowarn option or the
116 * maximum number of warnings has been reached.
117 * @param pos The source position at which to report the warning.
118 * @param key The key for the localized warning message.
119 * @param args Fields of the warning message.
120 */
121 public void warning(DiagnosticPosition pos, String key, Object ... args) {
122 report(diags.warning(source, pos, key, args));
123 }
125 /** Report a warning, unless suppressed by the -nowarn option or the
126 * maximum number of warnings has been reached.
127 * @param pos The source position at which to report the warning.
128 * @param key The key for the localized warning message.
129 * @param args Fields of the warning message.
130 */
131 public void warning(int pos, String key, Object ... args) {
132 report(diags.warning(source, wrap(pos), key, args));
133 }
135 /** Report a warning.
136 * @param pos The source position at which to report the warning.
137 * @param key The key for the localized warning message.
138 * @param args Fields of the warning message.
139 */
140 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
141 report(diags.mandatoryWarning(source, pos, key, args));
142 }
144 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
145 * @param key The key for the localized notification message.
146 * @param args Fields of the notint an error or warning message:
147 */
148 public void note(String key, Object ... args) {
149 report(diags.note(source, null, key, args));
150 }
152 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
153 * @param key The key for the localized notification message.
154 * @param args Fields of the notification message.
155 */
156 public void note(DiagnosticPosition pos, String key, Object ... args) {
157 report(diags.note(source, pos, key, args));
158 }
160 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
161 * @param key The key for the localized notification message.
162 * @param args Fields of the notification message.
163 */
164 public void note(int pos, String key, Object ... args) {
165 report(diags.note(source, wrap(pos), key, args));
166 }
168 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
169 * @param key The key for the localized notification message.
170 * @param args Fields of the notification message.
171 */
172 public void note(JavaFileObject file, String key, Object ... args) {
173 report(diags.note(getSource(file), null, key, args));
174 }
176 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
177 * @param key The key for the localized notification message.
178 * @param args Fields of the notification message.
179 */
180 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
181 report(diags.mandatoryNote(getSource(file), key, args));
182 }
184 protected abstract void report(JCDiagnostic diagnostic);
186 protected abstract void directError(String key, Object... args);
188 private DiagnosticPosition wrap(int pos) {
189 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
190 }
192 /** Factory for diagnostics
193 */
194 protected JCDiagnostic.Factory diags;
196 /** The file that's currently being translated.
197 */
198 protected DiagnosticSource source;
200 /** A cache of lightweight DiagnosticSource objects.
201 */
202 protected Map<JavaFileObject, DiagnosticSource> sourceMap;
203 }