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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial