001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.code;
027:
028: import java.util.EnumSet;
029: import java.util.HashMap;
030: import java.util.Map;
031: import com.sun.tools.javac.code.Symbol.*;
032: import com.sun.tools.javac.util.Context;
033: import com.sun.tools.javac.util.List;
034: import com.sun.tools.javac.util.Options;
035: import com.sun.tools.javac.util.Pair;
036: import com.sun.tools.javac.util.Version;
037: import static com.sun.tools.javac.code.Flags.*;
038:
039: /**
040: * A class for handling -Xlint suboptions and @SuppresssWarnings.
041: *
042: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
043: * you write code that depends on this, you do so at your own risk.
044: * This code and its internal interfaces are subject to change or
045: * deletion without notice.</b>
046: */
047: @Version("@(#)Lint.java 1.15 07/05/05")
048: public class Lint {
049: /** The context key for the root Lint object. */
050: protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
051:
052: /** Get the root Lint instance. */
053: public static Lint instance(Context context) {
054: Lint instance = context.get(lintKey);
055: if (instance == null)
056: instance = new Lint(context);
057: return instance;
058: }
059:
060: /**
061: * Returns the result of combining the values in this object with
062: * the given annotation.
063: */
064: public Lint augment(Attribute.Compound attr) {
065: return augmentor.augment(this , attr);
066: }
067:
068: /**
069: * Returns the result of combining the values in this object with
070: * the given annotations.
071: */
072: public Lint augment(List<Attribute.Compound> attrs) {
073: return augmentor.augment(this , attrs);
074: }
075:
076: /**
077: * Returns the result of combining the values in this object with
078: * the given annotations and flags.
079: */
080: public Lint augment(List<Attribute.Compound> attrs, long flags) {
081: Lint l = augmentor.augment(this , attrs);
082: if ((flags & DEPRECATED) != 0) {
083: if (l == this )
084: l = new Lint(this );
085: l.values.remove(LintCategory.DEPRECATION);
086: l.suppressedValues.add(LintCategory.DEPRECATION);
087: }
088: return l;
089: }
090:
091: private final AugmentVisitor augmentor;
092:
093: private final EnumSet<LintCategory> values;
094: private final EnumSet<LintCategory> suppressedValues;
095:
096: private static Map<String, LintCategory> map = new HashMap<String, LintCategory>();
097:
098: protected Lint(Context context) {
099: // initialize values according to the lint options
100: Options options = Options.instance(context);
101: values = EnumSet.noneOf(LintCategory.class);
102: for (Map.Entry<String, LintCategory> e : map.entrySet()) {
103: if (options.lint(e.getKey()))
104: values.add(e.getValue());
105: }
106:
107: suppressedValues = EnumSet.noneOf(LintCategory.class);
108:
109: context.put(lintKey, this );
110: augmentor = new AugmentVisitor(context);
111: }
112:
113: protected Lint(Lint other) {
114: this .augmentor = other.augmentor;
115: this .values = other.values.clone();
116: this .suppressedValues = other.suppressedValues.clone();
117: }
118:
119: public String toString() {
120: return "Lint:[values" + values + " suppressedValues"
121: + suppressedValues + "]";
122: }
123:
124: /**
125: * Categories of warnings that can be generated by the compiler.
126: */
127: public enum LintCategory {
128: /**
129: * Warn about use of unnecessary casts.
130: */
131: CAST("cast"),
132:
133: /**
134: * Warn about use of deprecated items.
135: */
136: DEPRECATION("deprecation"),
137:
138: /**
139: * Warn about items which are documented with an {@code @deprecated} JavaDoc
140: * comment, but which do not have {@code @Deprecated} annotation.
141: */
142: DEP_ANN("dep-ann"),
143:
144: /**
145: * Warn about division by constant integer 0.
146: */
147: DIVZERO("divzero"),
148:
149: /**
150: * Warn about empty statement after if.
151: */
152: EMPTY("empty"),
153:
154: /**
155: * Warn about falling through from one case of a switch statement to the next.
156: */
157: FALLTHROUGH("fallthrough"),
158:
159: /**
160: * Warn about finally clauses that do not terminate normally.
161: */
162: FINALLY("finally"),
163:
164: /**
165: * Warn about issues regarding method overrides.
166: */
167: OVERRIDES("overrides"),
168:
169: /**
170: * Warn about invalid path elements on the command line.
171: * Such warnings cannot be suppressed with the SuppressWarnings
172: * annotation.
173: */
174: PATH("path"),
175:
176: /**
177: * Warn about Serializable classes that do not provide a serial version ID.
178: */
179: SERIAL("serial"),
180:
181: /**
182: * Warn about unchecked operations on raw types.
183: */
184: UNCHECKED("unchecked");
185:
186: LintCategory(String option) {
187: this .option = option;
188: map.put(option, this );
189: }
190:
191: static LintCategory get(String option) {
192: return map.get(option);
193: }
194:
195: private final String option;
196: };
197:
198: /**
199: * Checks if a warning category is enabled. A warning category may be enabled
200: * on the command line, or by default, and can be temporarily disabled with
201: * the SuppressWarnings annotation.
202: */
203: public boolean isEnabled(LintCategory lc) {
204: return values.contains(lc);
205: }
206:
207: /**
208: * Checks is a warning category has been specifically suppressed, by means
209: * of the SuppressWarnings annotation, or, in the case of the deprecated
210: * category, whether it has been implicitly suppressed by virtue of the
211: * current entity being itself deprecated.
212: */
213: public boolean isSuppressed(LintCategory lc) {
214: return suppressedValues.contains(lc);
215: }
216:
217: protected static class AugmentVisitor implements Attribute.Visitor {
218: private final Context context;
219: private Symtab syms;
220: private Lint parent;
221: private Lint lint;
222:
223: AugmentVisitor(Context context) {
224: // to break an ugly sequence of initialization dependencies,
225: // we defer the initialization of syms until it is needed
226: this .context = context;
227: }
228:
229: Lint augment(Lint parent, Attribute.Compound attr) {
230: initSyms();
231: this .parent = parent;
232: lint = null;
233: attr.accept(this );
234: return (lint == null ? parent : lint);
235: }
236:
237: Lint augment(Lint parent, List<Attribute.Compound> attrs) {
238: initSyms();
239: this .parent = parent;
240: lint = null;
241: for (Attribute.Compound a : attrs) {
242: a.accept(this );
243: }
244: return (lint == null ? parent : lint);
245: }
246:
247: private void initSyms() {
248: if (syms == null)
249: syms = Symtab.instance(context);
250: }
251:
252: private void suppress(LintCategory lc) {
253: if (lint == null)
254: lint = new Lint(parent);
255: lint.suppressedValues.add(lc);
256: lint.values.remove(lc);
257: }
258:
259: public void visitConstant(Attribute.Constant value) {
260: if (value.type.tsym == syms.stringType.tsym) {
261: LintCategory lc = LintCategory
262: .get((String) (value.value));
263: if (lc != null)
264: suppress(lc);
265: }
266: }
267:
268: public void visitClass(Attribute.Class clazz) {
269: }
270:
271: // If we find a @SuppressWarnings annotation, then we continue
272: // walking the tree, in order to suppress the individual warnings
273: // specified in the @SuppressWarnings annotation.
274: public void visitCompound(Attribute.Compound compound) {
275: if (compound.type.tsym == syms.suppressWarningsType.tsym) {
276: for (List<Pair<MethodSymbol, Attribute>> v = compound.values; v
277: .nonEmpty(); v = v.tail) {
278: Pair<MethodSymbol, Attribute> value = v.head;
279: if (value.fst.name.toString().equals("value"))
280: value.snd.accept(this );
281: }
282:
283: }
284: }
285:
286: public void visitArray(Attribute.Array array) {
287: for (Attribute value : array.values)
288: value.accept(this );
289: }
290:
291: public void visitEnum(Attribute.Enum e) {
292: }
293:
294: public void visitError(Attribute.Error e) {
295: }
296: };
297: }
|