src/share/classes/com/sun/tools/javac/code/Lint.java

Mon, 29 Sep 2008 12:00:29 +0100

author
mcimadamore
date
Mon, 29 Sep 2008 12:00:29 +0100
changeset 122
1a9276e7cb18
parent 54
eaf608c64fec
child 124
b81a9aa785ba
permissions
-rw-r--r--

6747671: -Xlint:rawtypes
Summary: add an Xlint option for detecting all raw types usages (ccc-approved)
Reviewed-by: jjg

duke@1 1 /*
xdono@54 2 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
duke@1 28 import java.util.EnumSet;
duke@1 29 import java.util.HashMap;
duke@1 30 import java.util.Map;
duke@1 31 import com.sun.tools.javac.code.Symbol.*;
duke@1 32 import com.sun.tools.javac.util.Context;
duke@1 33 import com.sun.tools.javac.util.List;
duke@1 34 import com.sun.tools.javac.util.Options;
duke@1 35 import com.sun.tools.javac.util.Pair;
duke@1 36 import static com.sun.tools.javac.code.Flags.*;
duke@1 37
duke@1 38
duke@1 39 /**
duke@1 40 * A class for handling -Xlint suboptions and @SuppresssWarnings.
duke@1 41 *
duke@1 42 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 43 * you write code that depends on this, you do so at your own risk.
duke@1 44 * This code and its internal interfaces are subject to change or
duke@1 45 * deletion without notice.</b>
duke@1 46 */
duke@1 47 public class Lint
duke@1 48 {
duke@1 49 /** The context key for the root Lint object. */
duke@1 50 protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
duke@1 51
duke@1 52 /** Get the root Lint instance. */
duke@1 53 public static Lint instance(Context context) {
duke@1 54 Lint instance = context.get(lintKey);
duke@1 55 if (instance == null)
duke@1 56 instance = new Lint(context);
duke@1 57 return instance;
duke@1 58 }
duke@1 59
duke@1 60 /**
duke@1 61 * Returns the result of combining the values in this object with
duke@1 62 * the given annotation.
duke@1 63 */
duke@1 64 public Lint augment(Attribute.Compound attr) {
duke@1 65 return augmentor.augment(this, attr);
duke@1 66 }
duke@1 67
duke@1 68
duke@1 69 /**
duke@1 70 * Returns the result of combining the values in this object with
duke@1 71 * the given annotations.
duke@1 72 */
duke@1 73 public Lint augment(List<Attribute.Compound> attrs) {
duke@1 74 return augmentor.augment(this, attrs);
duke@1 75 }
duke@1 76
duke@1 77 /**
duke@1 78 * Returns the result of combining the values in this object with
duke@1 79 * the given annotations and flags.
duke@1 80 */
duke@1 81 public Lint augment(List<Attribute.Compound> attrs, long flags) {
duke@1 82 Lint l = augmentor.augment(this, attrs);
duke@1 83 if ((flags & DEPRECATED) != 0) {
duke@1 84 if (l == this)
duke@1 85 l = new Lint(this);
duke@1 86 l.values.remove(LintCategory.DEPRECATION);
duke@1 87 l.suppressedValues.add(LintCategory.DEPRECATION);
duke@1 88 }
duke@1 89 return l;
duke@1 90 }
duke@1 91
duke@1 92
duke@1 93 private final AugmentVisitor augmentor;
duke@1 94
duke@1 95 private final EnumSet<LintCategory> values;
duke@1 96 private final EnumSet<LintCategory> suppressedValues;
duke@1 97
duke@1 98 private static Map<String, LintCategory> map = new HashMap<String,LintCategory>();
duke@1 99
duke@1 100
duke@1 101 protected Lint(Context context) {
duke@1 102 // initialize values according to the lint options
duke@1 103 Options options = Options.instance(context);
duke@1 104 values = EnumSet.noneOf(LintCategory.class);
duke@1 105 for (Map.Entry<String, LintCategory> e: map.entrySet()) {
duke@1 106 if (options.lint(e.getKey()))
duke@1 107 values.add(e.getValue());
duke@1 108 }
duke@1 109
duke@1 110 suppressedValues = EnumSet.noneOf(LintCategory.class);
duke@1 111
duke@1 112 context.put(lintKey, this);
duke@1 113 augmentor = new AugmentVisitor(context);
duke@1 114 }
duke@1 115
duke@1 116 protected Lint(Lint other) {
duke@1 117 this.augmentor = other.augmentor;
duke@1 118 this.values = other.values.clone();
duke@1 119 this.suppressedValues = other.suppressedValues.clone();
duke@1 120 }
duke@1 121
duke@1 122 public String toString() {
duke@1 123 return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
duke@1 124 }
duke@1 125
duke@1 126 /**
duke@1 127 * Categories of warnings that can be generated by the compiler.
duke@1 128 */
duke@1 129 public enum LintCategory {
duke@1 130 /**
duke@1 131 * Warn about use of unnecessary casts.
duke@1 132 */
duke@1 133 CAST("cast"),
duke@1 134
duke@1 135 /**
duke@1 136 * Warn about use of deprecated items.
duke@1 137 */
duke@1 138 DEPRECATION("deprecation"),
duke@1 139
duke@1 140 /**
duke@1 141 * Warn about items which are documented with an {@code @deprecated} JavaDoc
duke@1 142 * comment, but which do not have {@code @Deprecated} annotation.
duke@1 143 */
duke@1 144 DEP_ANN("dep-ann"),
duke@1 145
duke@1 146 /**
duke@1 147 * Warn about division by constant integer 0.
duke@1 148 */
duke@1 149 DIVZERO("divzero"),
duke@1 150
duke@1 151 /**
duke@1 152 * Warn about empty statement after if.
duke@1 153 */
duke@1 154 EMPTY("empty"),
duke@1 155
duke@1 156 /**
duke@1 157 * Warn about falling through from one case of a switch statement to the next.
duke@1 158 */
duke@1 159 FALLTHROUGH("fallthrough"),
duke@1 160
duke@1 161 /**
duke@1 162 * Warn about finally clauses that do not terminate normally.
duke@1 163 */
duke@1 164 FINALLY("finally"),
duke@1 165
duke@1 166 /**
duke@1 167 * Warn about issues regarding method overrides.
duke@1 168 */
duke@1 169 OVERRIDES("overrides"),
duke@1 170
duke@1 171 /**
duke@1 172 * Warn about invalid path elements on the command line.
duke@1 173 * Such warnings cannot be suppressed with the SuppressWarnings
duke@1 174 * annotation.
duke@1 175 */
duke@1 176 PATH("path"),
duke@1 177
duke@1 178 /**
duke@1 179 * Warn about Serializable classes that do not provide a serial version ID.
duke@1 180 */
duke@1 181 SERIAL("serial"),
duke@1 182
duke@1 183 /**
duke@1 184 * Warn about unchecked operations on raw types.
duke@1 185 */
mcimadamore@122 186 UNCHECKED("unchecked"),
mcimadamore@122 187
mcimadamore@122 188 /**
mcimadamore@122 189 * Warn about unchecked operations on raw types.
mcimadamore@122 190 */
mcimadamore@122 191 RAW("rawtypes");
duke@1 192
duke@1 193 LintCategory(String option) {
duke@1 194 this.option = option;
duke@1 195 map.put(option, this);
duke@1 196 }
duke@1 197
duke@1 198 static LintCategory get(String option) {
duke@1 199 return map.get(option);
duke@1 200 }
duke@1 201
jjg@11 202 public final String option;
duke@1 203 };
duke@1 204
duke@1 205 /**
duke@1 206 * Checks if a warning category is enabled. A warning category may be enabled
duke@1 207 * on the command line, or by default, and can be temporarily disabled with
duke@1 208 * the SuppressWarnings annotation.
duke@1 209 */
duke@1 210 public boolean isEnabled(LintCategory lc) {
duke@1 211 return values.contains(lc);
duke@1 212 }
duke@1 213
duke@1 214 /**
duke@1 215 * Checks is a warning category has been specifically suppressed, by means
duke@1 216 * of the SuppressWarnings annotation, or, in the case of the deprecated
duke@1 217 * category, whether it has been implicitly suppressed by virtue of the
duke@1 218 * current entity being itself deprecated.
duke@1 219 */
duke@1 220 public boolean isSuppressed(LintCategory lc) {
duke@1 221 return suppressedValues.contains(lc);
duke@1 222 }
duke@1 223
duke@1 224 protected static class AugmentVisitor implements Attribute.Visitor {
duke@1 225 private final Context context;
duke@1 226 private Symtab syms;
duke@1 227 private Lint parent;
duke@1 228 private Lint lint;
duke@1 229
duke@1 230 AugmentVisitor(Context context) {
duke@1 231 // to break an ugly sequence of initialization dependencies,
duke@1 232 // we defer the initialization of syms until it is needed
duke@1 233 this.context = context;
duke@1 234 }
duke@1 235
duke@1 236 Lint augment(Lint parent, Attribute.Compound attr) {
duke@1 237 initSyms();
duke@1 238 this.parent = parent;
duke@1 239 lint = null;
duke@1 240 attr.accept(this);
duke@1 241 return (lint == null ? parent : lint);
duke@1 242 }
duke@1 243
duke@1 244 Lint augment(Lint parent, List<Attribute.Compound> attrs) {
duke@1 245 initSyms();
duke@1 246 this.parent = parent;
duke@1 247 lint = null;
duke@1 248 for (Attribute.Compound a: attrs) {
duke@1 249 a.accept(this);
duke@1 250 }
duke@1 251 return (lint == null ? parent : lint);
duke@1 252 }
duke@1 253
duke@1 254 private void initSyms() {
duke@1 255 if (syms == null)
duke@1 256 syms = Symtab.instance(context);
duke@1 257 }
duke@1 258
duke@1 259 private void suppress(LintCategory lc) {
duke@1 260 if (lint == null)
duke@1 261 lint = new Lint(parent);
duke@1 262 lint.suppressedValues.add(lc);
duke@1 263 lint.values.remove(lc);
duke@1 264 }
duke@1 265
duke@1 266 public void visitConstant(Attribute.Constant value) {
duke@1 267 if (value.type.tsym == syms.stringType.tsym) {
duke@1 268 LintCategory lc = LintCategory.get((String) (value.value));
duke@1 269 if (lc != null)
duke@1 270 suppress(lc);
duke@1 271 }
duke@1 272 }
duke@1 273
duke@1 274 public void visitClass(Attribute.Class clazz) {
duke@1 275 }
duke@1 276
duke@1 277 // If we find a @SuppressWarnings annotation, then we continue
duke@1 278 // walking the tree, in order to suppress the individual warnings
duke@1 279 // specified in the @SuppressWarnings annotation.
duke@1 280 public void visitCompound(Attribute.Compound compound) {
duke@1 281 if (compound.type.tsym == syms.suppressWarningsType.tsym) {
duke@1 282 for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
duke@1 283 v.nonEmpty(); v = v.tail) {
duke@1 284 Pair<MethodSymbol,Attribute> value = v.head;
duke@1 285 if (value.fst.name.toString().equals("value"))
duke@1 286 value.snd.accept(this);
duke@1 287 }
duke@1 288
duke@1 289 }
duke@1 290 }
duke@1 291
duke@1 292 public void visitArray(Attribute.Array array) {
duke@1 293 for (Attribute value : array.values)
duke@1 294 value.accept(this);
duke@1 295 }
duke@1 296
duke@1 297 public void visitEnum(Attribute.Enum e) {
duke@1 298 }
duke@1 299
duke@1 300 public void visitError(Attribute.Error e) {
duke@1 301 }
duke@1 302 };
duke@1 303 }

mercurial