author | vromero |
Mon, 02 Sep 2013 22:38:36 +0100 | |
changeset 19914 | d86271bd430a |
parent 18010 | 604faee85350 |
child 19941 | 8b91e8eb2d20 |
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 |
|
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
16301
diff
changeset
|
71 |
* the metadata on the given symbol. |
10 | 72 |
*/ |
18010
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
16301
diff
changeset
|
73 |
public Lint augment(Symbol sym) { |
604faee85350
8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents:
16301
diff
changeset
|
74 |
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
|
75 |
if (sym.isDeprecated()) { |
10 | 76 |
if (l == this) |
77 |
l = new Lint(this); |
|
78 |
l.values.remove(LintCategory.DEPRECATION); |
|
79 |
l.suppressedValues.add(LintCategory.DEPRECATION); |
|
80 |
} |
|
81 |
return l; |
|
82 |
} |
|
83 |
||
84 |
||
85 |
private final AugmentVisitor augmentor; |
|
86 |
||
87 |
private final EnumSet<LintCategory> values; |
|
88 |
private final EnumSet<LintCategory> suppressedValues; |
|
89 |
||
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14369
diff
changeset
|
90 |
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
|
91 |
new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20); |
10 | 92 |
|
93 |
||
94 |
protected Lint(Context context) { |
|
95 |
// initialize values according to the lint options |
|
96 |
Options options = Options.instance(context); |
|
97 |
values = EnumSet.noneOf(LintCategory.class); |
|
98 |
for (Map.Entry<String, LintCategory> e: map.entrySet()) { |
|
99 |
if (options.lint(e.getKey())) |
|
100 |
values.add(e.getValue()); |
|
101 |
} |
|
102 |
||
103 |
suppressedValues = EnumSet.noneOf(LintCategory.class); |
|
104 |
||
105 |
context.put(lintKey, this); |
|
106 |
augmentor = new AugmentVisitor(context); |
|
107 |
} |
|
108 |
||
109 |
protected Lint(Lint other) { |
|
110 |
this.augmentor = other.augmentor; |
|
111 |
this.values = other.values.clone(); |
|
112 |
this.suppressedValues = other.suppressedValues.clone(); |
|
113 |
} |
|
114 |
||
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
115 |
@Override |
10 | 116 |
public String toString() { |
117 |
return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]"; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Categories of warnings that can be generated by the compiler. |
|
122 |
*/ |
|
123 |
public enum LintCategory { |
|
124 |
/** |
|
14369
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
125 |
* 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
|
126 |
* 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
|
127 |
* is not located in the same source file. |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
128 |
*/ |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
129 |
AUXILIARYCLASS("auxiliaryclass"), |
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
130 |
|
3d660d08d1f7
7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents:
13689
diff
changeset
|
131 |
/** |
10 | 132 |
* Warn about use of unnecessary casts. |
133 |
*/ |
|
134 |
CAST("cast"), |
|
135 |
||
136 |
/** |
|
7624
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
137 |
* 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
|
138 |
*/ |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
139 |
CLASSFILE("classfile"), |
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
140 |
|
c31b0ea95b37
6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents:
7335
diff
changeset
|
141 |
/** |
10 | 142 |
* Warn about use of deprecated items. |
143 |
*/ |
|
144 |
DEPRECATION("deprecation"), |
|
145 |
||
146 |
/** |
|
147 |
* Warn about items which are documented with an {@code @deprecated} JavaDoc |
|
148 |
* comment, but which do not have {@code @Deprecated} annotation. |
|
149 |
*/ |
|
150 |
DEP_ANN("dep-ann"), |
|
151 |
||
152 |
/** |
|
153 |
* Warn about division by constant integer 0. |
|
154 |
*/ |
|
155 |
DIVZERO("divzero"), |
|
156 |
||
157 |
/** |
|
158 |
* Warn about empty statement after if. |
|
159 |
*/ |
|
160 |
EMPTY("empty"), |
|
161 |
||
162 |
/** |
|
163 |
* Warn about falling through from one case of a switch statement to the next. |
|
164 |
*/ |
|
165 |
FALLTHROUGH("fallthrough"), |
|
166 |
||
167 |
/** |
|
168 |
* Warn about finally clauses that do not terminate normally. |
|
169 |
*/ |
|
170 |
FINALLY("finally"), |
|
171 |
||
172 |
/** |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
173 |
* 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
|
174 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
175 |
OPTIONS("options"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
176 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
177 |
/** |
19914 | 178 |
* Warn about issues regarding method overloads. |
179 |
*/ |
|
180 |
OVERLOADS("overloads"), |
|
181 |
||
182 |
/** |
|
10 | 183 |
* Warn about issues regarding method overrides. |
184 |
*/ |
|
185 |
OVERRIDES("overrides"), |
|
186 |
||
187 |
/** |
|
188 |
* Warn about invalid path elements on the command line. |
|
189 |
* Such warnings cannot be suppressed with the SuppressWarnings |
|
190 |
* annotation. |
|
191 |
*/ |
|
192 |
PATH("path"), |
|
193 |
||
194 |
/** |
|
1360
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
195 |
* Warn about issues regarding annotation processing. |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
196 |
*/ |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
197 |
PROCESSING("processing"), |
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
198 |
|
fd574e4926e3
6739427: -Xlint:processing not recognized as an option
martin
parents:
1358
diff
changeset
|
199 |
/** |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
200 |
* 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
|
201 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
202 |
RAW("rawtypes"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
203 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
204 |
/** |
10 | 205 |
* Warn about Serializable classes that do not provide a serial version ID. |
206 |
*/ |
|
207 |
SERIAL("serial"), |
|
208 |
||
209 |
/** |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
210 |
* 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
|
211 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
212 |
STATIC("static"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
213 |
|
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 |
* 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
|
216 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
217 |
SUNAPI("sunapi", true), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
218 |
|
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 |
* 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
|
221 |
*/ |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
222 |
TRY("try"), |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
223 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
224 |
/** |
10 | 225 |
* Warn about unchecked operations on raw types. |
226 |
*/ |
|
1358 | 227 |
UNCHECKED("unchecked"), |
228 |
||
229 |
/** |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
230 |
* Warn about potentially unsafe vararg methods |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
231 |
*/ |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
7211
diff
changeset
|
232 |
VARARGS("varargs"); |
10 | 233 |
|
234 |
LintCategory(String option) { |
|
3661 | 235 |
this(option, false); |
236 |
} |
|
237 |
||
238 |
LintCategory(String option, boolean hidden) { |
|
10 | 239 |
this.option = option; |
3661 | 240 |
this.hidden = hidden; |
10 | 241 |
map.put(option, this); |
242 |
} |
|
243 |
||
244 |
static LintCategory get(String option) { |
|
245 |
return map.get(option); |
|
246 |
} |
|
247 |
||
168 | 248 |
public final String option; |
3661 | 249 |
public final boolean hidden; |
10 | 250 |
}; |
251 |
||
252 |
/** |
|
253 |
* Checks if a warning category is enabled. A warning category may be enabled |
|
254 |
* on the command line, or by default, and can be temporarily disabled with |
|
255 |
* the SuppressWarnings annotation. |
|
256 |
*/ |
|
257 |
public boolean isEnabled(LintCategory lc) { |
|
258 |
return values.contains(lc); |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Checks is a warning category has been specifically suppressed, by means |
|
263 |
* of the SuppressWarnings annotation, or, in the case of the deprecated |
|
264 |
* category, whether it has been implicitly suppressed by virtue of the |
|
265 |
* current entity being itself deprecated. |
|
266 |
*/ |
|
267 |
public boolean isSuppressed(LintCategory lc) { |
|
268 |
return suppressedValues.contains(lc); |
|
269 |
} |
|
270 |
||
271 |
protected static class AugmentVisitor implements Attribute.Visitor { |
|
272 |
private final Context context; |
|
273 |
private Symtab syms; |
|
274 |
private Lint parent; |
|
275 |
private Lint lint; |
|
276 |
||
277 |
AugmentVisitor(Context context) { |
|
278 |
// to break an ugly sequence of initialization dependencies, |
|
279 |
// we defer the initialization of syms until it is needed |
|
280 |
this.context = context; |
|
281 |
} |
|
282 |
||
283 |
Lint augment(Lint parent, Attribute.Compound attr) { |
|
284 |
initSyms(); |
|
285 |
this.parent = parent; |
|
286 |
lint = null; |
|
287 |
attr.accept(this); |
|
288 |
return (lint == null ? parent : lint); |
|
289 |
} |
|
290 |
||
291 |
Lint augment(Lint parent, List<Attribute.Compound> attrs) { |
|
292 |
initSyms(); |
|
293 |
this.parent = parent; |
|
294 |
lint = null; |
|
295 |
for (Attribute.Compound a: attrs) { |
|
296 |
a.accept(this); |
|
297 |
} |
|
298 |
return (lint == null ? parent : lint); |
|
299 |
} |
|
300 |
||
301 |
private void initSyms() { |
|
302 |
if (syms == null) |
|
303 |
syms = Symtab.instance(context); |
|
304 |
} |
|
305 |
||
306 |
private void suppress(LintCategory lc) { |
|
307 |
if (lint == null) |
|
308 |
lint = new Lint(parent); |
|
309 |
lint.suppressedValues.add(lc); |
|
310 |
lint.values.remove(lc); |
|
311 |
} |
|
312 |
||
313 |
public void visitConstant(Attribute.Constant value) { |
|
314 |
if (value.type.tsym == syms.stringType.tsym) { |
|
315 |
LintCategory lc = LintCategory.get((String) (value.value)); |
|
316 |
if (lc != null) |
|
317 |
suppress(lc); |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
public void visitClass(Attribute.Class clazz) { |
|
322 |
} |
|
323 |
||
324 |
// If we find a @SuppressWarnings annotation, then we continue |
|
325 |
// walking the tree, in order to suppress the individual warnings |
|
326 |
// specified in the @SuppressWarnings annotation. |
|
327 |
public void visitCompound(Attribute.Compound compound) { |
|
328 |
if (compound.type.tsym == syms.suppressWarningsType.tsym) { |
|
329 |
for (List<Pair<MethodSymbol,Attribute>> v = compound.values; |
|
330 |
v.nonEmpty(); v = v.tail) { |
|
331 |
Pair<MethodSymbol,Attribute> value = v.head; |
|
332 |
if (value.fst.name.toString().equals("value")) |
|
333 |
value.snd.accept(this); |
|
334 |
} |
|
335 |
||
336 |
} |
|
337 |
} |
|
338 |
||
339 |
public void visitArray(Attribute.Array array) { |
|
340 |
for (Attribute value : array.values) |
|
341 |
value.accept(this); |
|
342 |
} |
|
343 |
||
344 |
public void visitEnum(Attribute.Enum e) { |
|
345 |
} |
|
346 |
||
347 |
public void visitError(Attribute.Error e) { |
|
348 |
} |
|
349 |
}; |
|
350 |
} |