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