author | jjg |
Tue, 14 May 2013 15:04:06 -0700 | |
changeset 17578 | 46ac954e4a84 |
parent 16301 | b6fd735ea78e |
child 18010 | 604faee85350 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15385 | 2 |
* Copyright (c) 2005, 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.javac.code; |
|
27 |
||
28 |
import java.util.EnumSet; |
|
29 |
import java.util.Map; |
|
30 |
import com.sun.tools.javac.code.Symbol.*; |
|
31 |
import com.sun.tools.javac.util.Context; |
|
32 |
import com.sun.tools.javac.util.List; |
|
33 |
import com.sun.tools.javac.util.Options; |
|
34 |
import com.sun.tools.javac.util.Pair; |
|
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14369
diff
changeset
|
35 |
|
10 | 36 |
import static com.sun.tools.javac.code.Flags.*; |
37 |
||
38 |
||
39 |
/** |
|
40 |
* A class for handling -Xlint suboptions and @SuppresssWarnings. |
|
41 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
42 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
43 |
* If you write code that depends on this, you do so at your own risk. |
10 | 44 |
* This code and its internal interfaces are subject to change or |
45 |
* deletion without notice.</b> |
|
46 |
*/ |
|
47 |
public class Lint |
|
48 |
{ |
|
49 |
/** The context key for the root Lint object. */ |
|
50 |
protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>(); |
|
51 |
||
52 |
/** Get the root Lint instance. */ |
|
53 |
public static Lint instance(Context context) { |
|
54 |
Lint instance = context.get(lintKey); |
|
55 |
if (instance == null) |
|
56 |
instance = new Lint(context); |
|
57 |
return instance; |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Returns the result of combining the values in this object with |
|
62 |
* the given annotation. |
|
63 |
*/ |
|
64 |
public Lint augment(Attribute.Compound attr) { |
|
65 |
return augmentor.augment(this, attr); |
|
66 |
} |
|
67 |
||
68 |
||
69 |
/** |
|
70 |
* Returns the result of combining the values in this object with |
|
71 |
* the given annotations. |
|
72 |
*/ |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
7681
diff
changeset
|
73 |
public Lint augment(Annotations annots) { |
15385 | 74 |
return augmentor.augment(this, annots.getDeclarationAttributes()); |
10 | 75 |
} |
76 |
||
77 |
/** |
|
78 |
* Returns the result of combining the values in this object with |
|
79 |
* the given annotations and flags. |
|
80 |
*/ |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
7681
diff
changeset
|
81 |
public Lint augment(Annotations annots, long flags) { |
15385 | 82 |
Lint l = augmentor.augment(this, annots.getDeclarationAttributes()); |
10 | 83 |
if ((flags & DEPRECATED) != 0) { |
84 |
if (l == this) |
|
85 |
l = new Lint(this); |
|
86 |
l.values.remove(LintCategory.DEPRECATION); |
|
87 |
l.suppressedValues.add(LintCategory.DEPRECATION); |
|
88 |
} |
|
89 |
return l; |
|
90 |
} |
|
91 |
||
92 |
||
93 |
private final AugmentVisitor augmentor; |
|
94 |
||
95 |
private final EnumSet<LintCategory> values; |
|
96 |
private final EnumSet<LintCategory> suppressedValues; |
|
97 |
||
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14369
diff
changeset
|
98 |
private static final Map<String, LintCategory> map = |
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14369
diff
changeset
|
99 |
new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20); |
10 | 100 |
|
101 |
||
102 |
protected Lint(Context context) { |
|
103 |
// initialize values according to the lint options |
|
104 |
Options options = Options.instance(context); |
|
105 |
values = EnumSet.noneOf(LintCategory.class); |
|
106 |
for (Map.Entry<String, LintCategory> e: map.entrySet()) { |
|
107 |
if (options.lint(e.getKey())) |
|
108 |
values.add(e.getValue()); |
|
109 |
} |
|
110 |
||
111 |
suppressedValues = EnumSet.noneOf(LintCategory.class); |
|
112 |
||
113 |
context.put(lintKey, this); |
|
114 |
augmentor = new AugmentVisitor(context); |
|
115 |
} |
|
116 |
||
117 |
protected Lint(Lint other) { |
|
118 |
this.augmentor = other.augmentor; |
|
119 |
this.values = other.values.clone(); |
|
120 |
this.suppressedValues = other.suppressedValues.clone(); |
|
121 |
} |
|
122 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
123 |
@Override |
10 | 124 |
public String toString() { |
125 |
return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]"; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Categories of warnings that can be generated by the compiler. |
|
130 |
*/ |
|
131 |
public enum LintCategory { |
|
132 |
/** |
|
14369
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
133 |
* Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
134 |
* different from the class name, and the type is not properly nested) and the referring code |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
135 |
* is not located in the same source file. |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
136 |
*/ |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
137 |
AUXILIARYCLASS("auxiliaryclass"), |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
138 |
|
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
139 |
/** |
10 | 140 |
* Warn about use of unnecessary casts. |
141 |
*/ |
|
142 |
CAST("cast"), |
|
143 |
||
144 |
/** |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
145 |
* Warn about issues related to classfile contents |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
146 |
*/ |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
147 |
CLASSFILE("classfile"), |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
148 |
|
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
149 |
/** |
10 | 150 |
* Warn about use of deprecated items. |
151 |
*/ |
|
152 |
DEPRECATION("deprecation"), |
|
153 |
||
154 |
/** |
|
155 |
* Warn about items which are documented with an {@code @deprecated} JavaDoc |
|
156 |
* comment, but which do not have {@code @Deprecated} annotation. |
|
157 |
*/ |
|
158 |
DEP_ANN("dep-ann"), |
|
159 |
||
160 |
/** |
|
161 |
* Warn about division by constant integer 0. |
|
162 |
*/ |
|
163 |
DIVZERO("divzero"), |
|
164 |
||
165 |
/** |
|
166 |
* Warn about empty statement after if. |
|
167 |
*/ |
|
168 |
EMPTY("empty"), |
|
169 |
||
170 |
/** |
|
171 |
* Warn about falling through from one case of a switch statement to the next. |
|
172 |
*/ |
|
173 |
FALLTHROUGH("fallthrough"), |
|
174 |
||
175 |
/** |
|
176 |
* Warn about finally clauses that do not terminate normally. |
|
177 |
*/ |
|
178 |
FINALLY("finally"), |
|
179 |
||
180 |
/** |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
181 |
* Warn about issues relating to use of command line options |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
182 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
183 |
OPTIONS("options"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
184 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
185 |
/** |
10 | 186 |
* Warn about issues regarding method overrides. |
187 |
*/ |
|
188 |
OVERRIDES("overrides"), |
|
189 |
||
190 |
/** |
|
191 |
* Warn about invalid path elements on the command line. |
|
192 |
* Such warnings cannot be suppressed with the SuppressWarnings |
|
193 |
* annotation. |
|
194 |
*/ |
|
195 |
PATH("path"), |
|
196 |
||
197 |
/** |
|
1360
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
198 |
* Warn about issues regarding annotation processing. |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
199 |
*/ |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
200 |
PROCESSING("processing"), |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
201 |
|
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
202 |
/** |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
203 |
* Warn about unchecked operations on raw types. |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
204 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
205 |
RAW("rawtypes"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
206 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
207 |
/** |
10 | 208 |
* Warn about Serializable classes that do not provide a serial version ID. |
209 |
*/ |
|
210 |
SERIAL("serial"), |
|
211 |
||
212 |
/** |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
213 |
* Warn about issues relating to use of statics |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
214 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
215 |
STATIC("static"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
216 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
217 |
/** |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
218 |
* Warn about proprietary API that may be removed in a future release. |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
219 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
220 |
SUNAPI("sunapi", true), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
221 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
222 |
/** |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
223 |
* Warn about issues relating to use of try blocks (i.e. try-with-resources) |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
224 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
225 |
TRY("try"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
226 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
227 |
/** |
10 | 228 |
* Warn about unchecked operations on raw types. |
229 |
*/ |
|
1358 | 230 |
UNCHECKED("unchecked"), |
231 |
||
232 |
/** |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
233 |
* Warn about potentially unsafe vararg methods |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
234 |
*/ |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
235 |
VARARGS("varargs"); |
10 | 236 |
|
237 |
LintCategory(String option) { |
|
3661 | 238 |
this(option, false); |
239 |
} |
|
240 |
||
241 |
LintCategory(String option, boolean hidden) { |
|
10 | 242 |
this.option = option; |
3661 | 243 |
this.hidden = hidden; |
10 | 244 |
map.put(option, this); |
245 |
} |
|
246 |
||
247 |
static LintCategory get(String option) { |
|
248 |
return map.get(option); |
|
249 |
} |
|
250 |
||
168 | 251 |
public final String option; |
3661 | 252 |
public final boolean hidden; |
10 | 253 |
}; |
254 |
||
255 |
/** |
|
256 |
* Checks if a warning category is enabled. A warning category may be enabled |
|
257 |
* on the command line, or by default, and can be temporarily disabled with |
|
258 |
* the SuppressWarnings annotation. |
|
259 |
*/ |
|
260 |
public boolean isEnabled(LintCategory lc) { |
|
261 |
return values.contains(lc); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Checks is a warning category has been specifically suppressed, by means |
|
266 |
* of the SuppressWarnings annotation, or, in the case of the deprecated |
|
267 |
* category, whether it has been implicitly suppressed by virtue of the |
|
268 |
* current entity being itself deprecated. |
|
269 |
*/ |
|
270 |
public boolean isSuppressed(LintCategory lc) { |
|
271 |
return suppressedValues.contains(lc); |
|
272 |
} |
|
273 |
||
274 |
protected static class AugmentVisitor implements Attribute.Visitor { |
|
275 |
private final Context context; |
|
276 |
private Symtab syms; |
|
277 |
private Lint parent; |
|
278 |
private Lint lint; |
|
279 |
||
280 |
AugmentVisitor(Context context) { |
|
281 |
// to break an ugly sequence of initialization dependencies, |
|
282 |
// we defer the initialization of syms until it is needed |
|
283 |
this.context = context; |
|
284 |
} |
|
285 |
||
286 |
Lint augment(Lint parent, Attribute.Compound attr) { |
|
287 |
initSyms(); |
|
288 |
this.parent = parent; |
|
289 |
lint = null; |
|
290 |
attr.accept(this); |
|
291 |
return (lint == null ? parent : lint); |
|
292 |
} |
|
293 |
||
294 |
Lint augment(Lint parent, List<Attribute.Compound> attrs) { |
|
295 |
initSyms(); |
|
296 |
this.parent = parent; |
|
297 |
lint = null; |
|
298 |
for (Attribute.Compound a: attrs) { |
|
299 |
a.accept(this); |
|
300 |
} |
|
301 |
return (lint == null ? parent : lint); |
|
302 |
} |
|
303 |
||
304 |
private void initSyms() { |
|
305 |
if (syms == null) |
|
306 |
syms = Symtab.instance(context); |
|
307 |
} |
|
308 |
||
309 |
private void suppress(LintCategory lc) { |
|
310 |
if (lint == null) |
|
311 |
lint = new Lint(parent); |
|
312 |
lint.suppressedValues.add(lc); |
|
313 |
lint.values.remove(lc); |
|
314 |
} |
|
315 |
||
316 |
public void visitConstant(Attribute.Constant value) { |
|
317 |
if (value.type.tsym == syms.stringType.tsym) { |
|
318 |
LintCategory lc = LintCategory.get((String) (value.value)); |
|
319 |
if (lc != null) |
|
320 |
suppress(lc); |
|
321 |
} |
|
322 |
} |
|
323 |
||
324 |
public void visitClass(Attribute.Class clazz) { |
|
325 |
} |
|
326 |
||
327 |
// If we find a @SuppressWarnings annotation, then we continue |
|
328 |
// walking the tree, in order to suppress the individual warnings |
|
329 |
// specified in the @SuppressWarnings annotation. |
|
330 |
public void visitCompound(Attribute.Compound compound) { |
|
331 |
if (compound.type.tsym == syms.suppressWarningsType.tsym) { |
|
332 |
for (List<Pair<MethodSymbol,Attribute>> v = compound.values; |
|
333 |
v.nonEmpty(); v = v.tail) { |
|
334 |
Pair<MethodSymbol,Attribute> value = v.head; |
|
335 |
if (value.fst.name.toString().equals("value")) |
|
336 |
value.snd.accept(this); |
|
337 |
} |
|
338 |
||
339 |
} |
|
340 |
} |
|
341 |
||
342 |
public void visitArray(Attribute.Array array) { |
|
343 |
for (Attribute value : array.values) |
|
344 |
value.accept(this); |
|
345 |
} |
|
346 |
||
347 |
public void visitEnum(Attribute.Enum e) { |
|
348 |
} |
|
349 |
||
350 |
public void visitError(Attribute.Error e) { |
|
351 |
} |
|
352 |
}; |
|
353 |
} |