Thu, 01 Nov 2012 10:48:36 +0100
7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax
1 /*
2 * Copyright (c) 2005, 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.javac.code;
28 import java.util.EnumSet;
29 import java.util.HashMap;
30 import java.util.Map;
31 import com.sun.tools.javac.code.Symbol.*;
32 import com.sun.tools.javac.util.Context;
33 import com.sun.tools.javac.util.List;
34 import com.sun.tools.javac.util.Options;
35 import com.sun.tools.javac.util.Pair;
36 import static com.sun.tools.javac.code.Flags.*;
39 /**
40 * A class for handling -Xlint suboptions and @SuppresssWarnings.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
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>();
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 }
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 }
69 /**
70 * Returns the result of combining the values in this object with
71 * the given annotations.
72 */
73 public Lint augment(Annotations annots) {
74 return augmentor.augment(this, annots.getAttributes());
75 }
77 /**
78 * Returns the result of combining the values in this object with
79 * the given annotations and flags.
80 */
81 public Lint augment(Annotations annots, long flags) {
82 Lint l = augmentor.augment(this, annots.getAttributes());
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 }
93 private final AugmentVisitor augmentor;
95 private final EnumSet<LintCategory> values;
96 private final EnumSet<LintCategory> suppressedValues;
98 private static Map<String, LintCategory> map = new HashMap<String,LintCategory>();
101 protected Lint(Context context) {
102 // initialize values according to the lint options
103 Options options = Options.instance(context);
104 values = EnumSet.noneOf(LintCategory.class);
105 for (Map.Entry<String, LintCategory> e: map.entrySet()) {
106 if (options.lint(e.getKey()))
107 values.add(e.getValue());
108 }
110 suppressedValues = EnumSet.noneOf(LintCategory.class);
112 context.put(lintKey, this);
113 augmentor = new AugmentVisitor(context);
114 }
116 protected Lint(Lint other) {
117 this.augmentor = other.augmentor;
118 this.values = other.values.clone();
119 this.suppressedValues = other.suppressedValues.clone();
120 }
122 @Override
123 public String toString() {
124 return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
125 }
127 /**
128 * Categories of warnings that can be generated by the compiler.
129 */
130 public enum LintCategory {
131 /**
132 * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
133 * different from the class name, and the type is not properly nested) and the referring code
134 * is not located in the same source file.
135 */
136 AUXILIARYCLASS("auxiliaryclass"),
138 /**
139 * Warn about use of unnecessary casts.
140 */
141 CAST("cast"),
143 /**
144 * Warn about issues related to classfile contents
145 */
146 CLASSFILE("classfile"),
148 /**
149 * Warn about use of deprecated items.
150 */
151 DEPRECATION("deprecation"),
153 /**
154 * Warn about items which are documented with an {@code @deprecated} JavaDoc
155 * comment, but which do not have {@code @Deprecated} annotation.
156 */
157 DEP_ANN("dep-ann"),
159 /**
160 * Warn about division by constant integer 0.
161 */
162 DIVZERO("divzero"),
164 /**
165 * Warn about empty statement after if.
166 */
167 EMPTY("empty"),
169 /**
170 * Warn about falling through from one case of a switch statement to the next.
171 */
172 FALLTHROUGH("fallthrough"),
174 /**
175 * Warn about finally clauses that do not terminate normally.
176 */
177 FINALLY("finally"),
179 /**
180 * Warn about issues relating to use of command line options
181 */
182 OPTIONS("options"),
184 /**
185 * Warn about issues regarding method overrides.
186 */
187 OVERRIDES("overrides"),
189 /**
190 * Warn about invalid path elements on the command line.
191 * Such warnings cannot be suppressed with the SuppressWarnings
192 * annotation.
193 */
194 PATH("path"),
196 /**
197 * Warn about issues regarding annotation processing.
198 */
199 PROCESSING("processing"),
201 /**
202 * Warn about unchecked operations on raw types.
203 */
204 RAW("rawtypes"),
206 /**
207 * Warn about Serializable classes that do not provide a serial version ID.
208 */
209 SERIAL("serial"),
211 /**
212 * Warn about issues relating to use of statics
213 */
214 STATIC("static"),
216 /**
217 * Warn about proprietary API that may be removed in a future release.
218 */
219 SUNAPI("sunapi", true),
221 /**
222 * Warn about issues relating to use of try blocks (i.e. try-with-resources)
223 */
224 TRY("try"),
226 /**
227 * Warn about unchecked operations on raw types.
228 */
229 UNCHECKED("unchecked"),
231 /**
232 * Warn about potentially unsafe vararg methods
233 */
234 VARARGS("varargs");
236 LintCategory(String option) {
237 this(option, false);
238 }
240 LintCategory(String option, boolean hidden) {
241 this.option = option;
242 this.hidden = hidden;
243 map.put(option, this);
244 }
246 static LintCategory get(String option) {
247 return map.get(option);
248 }
250 public final String option;
251 public final boolean hidden;
252 };
254 /**
255 * Checks if a warning category is enabled. A warning category may be enabled
256 * on the command line, or by default, and can be temporarily disabled with
257 * the SuppressWarnings annotation.
258 */
259 public boolean isEnabled(LintCategory lc) {
260 return values.contains(lc);
261 }
263 /**
264 * Checks is a warning category has been specifically suppressed, by means
265 * of the SuppressWarnings annotation, or, in the case of the deprecated
266 * category, whether it has been implicitly suppressed by virtue of the
267 * current entity being itself deprecated.
268 */
269 public boolean isSuppressed(LintCategory lc) {
270 return suppressedValues.contains(lc);
271 }
273 protected static class AugmentVisitor implements Attribute.Visitor {
274 private final Context context;
275 private Symtab syms;
276 private Lint parent;
277 private Lint lint;
279 AugmentVisitor(Context context) {
280 // to break an ugly sequence of initialization dependencies,
281 // we defer the initialization of syms until it is needed
282 this.context = context;
283 }
285 Lint augment(Lint parent, Attribute.Compound attr) {
286 initSyms();
287 this.parent = parent;
288 lint = null;
289 attr.accept(this);
290 return (lint == null ? parent : lint);
291 }
293 Lint augment(Lint parent, List<Attribute.Compound> attrs) {
294 initSyms();
295 this.parent = parent;
296 lint = null;
297 for (Attribute.Compound a: attrs) {
298 a.accept(this);
299 }
300 return (lint == null ? parent : lint);
301 }
303 private void initSyms() {
304 if (syms == null)
305 syms = Symtab.instance(context);
306 }
308 private void suppress(LintCategory lc) {
309 if (lint == null)
310 lint = new Lint(parent);
311 lint.suppressedValues.add(lc);
312 lint.values.remove(lc);
313 }
315 public void visitConstant(Attribute.Constant value) {
316 if (value.type.tsym == syms.stringType.tsym) {
317 LintCategory lc = LintCategory.get((String) (value.value));
318 if (lc != null)
319 suppress(lc);
320 }
321 }
323 public void visitClass(Attribute.Class clazz) {
324 }
326 // If we find a @SuppressWarnings annotation, then we continue
327 // walking the tree, in order to suppress the individual warnings
328 // specified in the @SuppressWarnings annotation.
329 public void visitCompound(Attribute.Compound compound) {
330 if (compound.type.tsym == syms.suppressWarningsType.tsym) {
331 for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
332 v.nonEmpty(); v = v.tail) {
333 Pair<MethodSymbol,Attribute> value = v.head;
334 if (value.fst.name.toString().equals("value"))
335 value.snd.accept(this);
336 }
338 }
339 }
341 public void visitArray(Attribute.Array array) {
342 for (Attribute value : array.values)
343 value.accept(this);
344 }
346 public void visitEnum(Attribute.Enum e) {
347 }
349 public void visitError(Attribute.Error e) {
350 }
351 };
352 }