aoqi@0: /*
aoqi@0: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.code;
aoqi@0:
aoqi@0: import java.util.EnumSet;
aoqi@0: import java.util.Map;
aoqi@0: import com.sun.tools.javac.code.Symbol.*;
aoqi@0: import com.sun.tools.javac.util.Context;
aoqi@0: import com.sun.tools.javac.util.List;
aoqi@0: import com.sun.tools.javac.util.Options;
aoqi@0: import com.sun.tools.javac.util.Pair;
aoqi@0:
aoqi@0: /**
aoqi@0: * A class for handling -Xlint suboptions and @SuppresssWarnings.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class Lint
aoqi@0: {
aoqi@0: /** The context key for the root Lint object. */
aoqi@0: protected static final Context.Key lintKey = new Context.Key();
aoqi@0:
aoqi@0: /** Get the root Lint instance. */
aoqi@0: public static Lint instance(Context context) {
aoqi@0: Lint instance = context.get(lintKey);
aoqi@0: if (instance == null)
aoqi@0: instance = new Lint(context);
aoqi@0: return instance;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the result of combining the values in this object with
aoqi@0: * the given annotation.
aoqi@0: */
aoqi@0: public Lint augment(Attribute.Compound attr) {
aoqi@0: return augmentor.augment(this, attr);
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Returns the result of combining the values in this object with
aoqi@0: * the metadata on the given symbol.
aoqi@0: */
aoqi@0: public Lint augment(Symbol sym) {
aoqi@0: Lint l = augmentor.augment(this, sym.getDeclarationAttributes());
aoqi@0: if (sym.isDeprecated()) {
aoqi@0: if (l == this)
aoqi@0: l = new Lint(this);
aoqi@0: l.values.remove(LintCategory.DEPRECATION);
aoqi@0: l.suppressedValues.add(LintCategory.DEPRECATION);
aoqi@0: }
aoqi@0: return l;
aoqi@0: }
aoqi@0:
aoqi@0: private final AugmentVisitor augmentor;
aoqi@0:
aoqi@0: private final EnumSet values;
aoqi@0: private final EnumSet suppressedValues;
aoqi@0:
aoqi@0: private static final Map map =
aoqi@0: new java.util.concurrent.ConcurrentHashMap(20);
aoqi@0:
aoqi@0: protected Lint(Context context) {
aoqi@0: // initialize values according to the lint options
aoqi@0: Options options = Options.instance(context);
aoqi@0: values = EnumSet.noneOf(LintCategory.class);
aoqi@0: for (Map.Entry e: map.entrySet()) {
aoqi@0: if (options.lint(e.getKey()))
aoqi@0: values.add(e.getValue());
aoqi@0: }
aoqi@0:
aoqi@0: suppressedValues = EnumSet.noneOf(LintCategory.class);
aoqi@0:
aoqi@0: context.put(lintKey, this);
aoqi@0: augmentor = new AugmentVisitor(context);
aoqi@0: }
aoqi@0:
aoqi@0: protected Lint(Lint other) {
aoqi@0: this.augmentor = other.augmentor;
aoqi@0: this.values = other.values.clone();
aoqi@0: this.suppressedValues = other.suppressedValues.clone();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Categories of warnings that can be generated by the compiler.
aoqi@0: */
aoqi@0: public enum LintCategory {
aoqi@0: /**
aoqi@0: * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
aoqi@0: * different from the class name, and the type is not properly nested) and the referring code
aoqi@0: * is not located in the same source file.
aoqi@0: */
aoqi@0: AUXILIARYCLASS("auxiliaryclass"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about use of unnecessary casts.
aoqi@0: */
aoqi@0: CAST("cast"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues related to classfile contents
aoqi@0: */
aoqi@0: CLASSFILE("classfile"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about use of deprecated items.
aoqi@0: */
aoqi@0: DEPRECATION("deprecation"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about items which are documented with an {@code @deprecated} JavaDoc
aoqi@0: * comment, but which do not have {@code @Deprecated} annotation.
aoqi@0: */
aoqi@0: DEP_ANN("dep-ann"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about division by constant integer 0.
aoqi@0: */
aoqi@0: DIVZERO("divzero"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about empty statement after if.
aoqi@0: */
aoqi@0: EMPTY("empty"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about falling through from one case of a switch statement to the next.
aoqi@0: */
aoqi@0: FALLTHROUGH("fallthrough"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about finally clauses that do not terminate normally.
aoqi@0: */
aoqi@0: FINALLY("finally"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues relating to use of command line options
aoqi@0: */
aoqi@0: OPTIONS("options"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues regarding method overloads.
aoqi@0: */
aoqi@0: OVERLOADS("overloads"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues regarding method overrides.
aoqi@0: */
aoqi@0: OVERRIDES("overrides"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about invalid path elements on the command line.
aoqi@0: * Such warnings cannot be suppressed with the SuppressWarnings
aoqi@0: * annotation.
aoqi@0: */
aoqi@0: PATH("path"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues regarding annotation processing.
aoqi@0: */
aoqi@0: PROCESSING("processing"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about unchecked operations on raw types.
aoqi@0: */
aoqi@0: RAW("rawtypes"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about Serializable classes that do not provide a serial version ID.
aoqi@0: */
aoqi@0: SERIAL("serial"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues relating to use of statics
aoqi@0: */
aoqi@0: STATIC("static"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about proprietary API that may be removed in a future release.
aoqi@0: */
aoqi@0: SUNAPI("sunapi", true),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about issues relating to use of try blocks (i.e. try-with-resources)
aoqi@0: */
aoqi@0: TRY("try"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about unchecked operations on raw types.
aoqi@0: */
aoqi@0: UNCHECKED("unchecked"),
aoqi@0:
aoqi@0: /**
aoqi@0: * Warn about potentially unsafe vararg methods
aoqi@0: */
aoqi@0: VARARGS("varargs");
aoqi@0:
aoqi@0: LintCategory(String option) {
aoqi@0: this(option, false);
aoqi@0: }
aoqi@0:
aoqi@0: LintCategory(String option, boolean hidden) {
aoqi@0: this.option = option;
aoqi@0: this.hidden = hidden;
aoqi@0: map.put(option, this);
aoqi@0: }
aoqi@0:
aoqi@0: static LintCategory get(String option) {
aoqi@0: return map.get(option);
aoqi@0: }
aoqi@0:
aoqi@0: public final String option;
aoqi@0: public final boolean hidden;
aoqi@0: };
aoqi@0:
aoqi@0: /**
aoqi@0: * Checks if a warning category is enabled. A warning category may be enabled
aoqi@0: * on the command line, or by default, and can be temporarily disabled with
aoqi@0: * the SuppressWarnings annotation.
aoqi@0: */
aoqi@0: public boolean isEnabled(LintCategory lc) {
aoqi@0: return values.contains(lc);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Checks is a warning category has been specifically suppressed, by means
aoqi@0: * of the SuppressWarnings annotation, or, in the case of the deprecated
aoqi@0: * category, whether it has been implicitly suppressed by virtue of the
aoqi@0: * current entity being itself deprecated.
aoqi@0: */
aoqi@0: public boolean isSuppressed(LintCategory lc) {
aoqi@0: return suppressedValues.contains(lc);
aoqi@0: }
aoqi@0:
aoqi@0: protected static class AugmentVisitor implements Attribute.Visitor {
aoqi@0: private final Context context;
aoqi@0: private Symtab syms;
aoqi@0: private Lint parent;
aoqi@0: private Lint lint;
aoqi@0:
aoqi@0: AugmentVisitor(Context context) {
aoqi@0: // to break an ugly sequence of initialization dependencies,
aoqi@0: // we defer the initialization of syms until it is needed
aoqi@0: this.context = context;
aoqi@0: }
aoqi@0:
aoqi@0: Lint augment(Lint parent, Attribute.Compound attr) {
aoqi@0: initSyms();
aoqi@0: this.parent = parent;
aoqi@0: lint = null;
aoqi@0: attr.accept(this);
aoqi@0: return (lint == null ? parent : lint);
aoqi@0: }
aoqi@0:
aoqi@0: Lint augment(Lint parent, List attrs) {
aoqi@0: initSyms();
aoqi@0: this.parent = parent;
aoqi@0: lint = null;
aoqi@0: for (Attribute.Compound a: attrs) {
aoqi@0: a.accept(this);
aoqi@0: }
aoqi@0: return (lint == null ? parent : lint);
aoqi@0: }
aoqi@0:
aoqi@0: private void initSyms() {
aoqi@0: if (syms == null)
aoqi@0: syms = Symtab.instance(context);
aoqi@0: }
aoqi@0:
aoqi@0: private void suppress(LintCategory lc) {
aoqi@0: if (lint == null)
aoqi@0: lint = new Lint(parent);
aoqi@0: lint.suppressedValues.add(lc);
aoqi@0: lint.values.remove(lc);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitConstant(Attribute.Constant value) {
aoqi@0: if (value.type.tsym == syms.stringType.tsym) {
aoqi@0: LintCategory lc = LintCategory.get((String) (value.value));
aoqi@0: if (lc != null)
aoqi@0: suppress(lc);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void visitClass(Attribute.Class clazz) {
aoqi@0: }
aoqi@0:
aoqi@0: // If we find a @SuppressWarnings annotation, then we continue
aoqi@0: // walking the tree, in order to suppress the individual warnings
aoqi@0: // specified in the @SuppressWarnings annotation.
aoqi@0: public void visitCompound(Attribute.Compound compound) {
aoqi@0: if (compound.type.tsym == syms.suppressWarningsType.tsym) {
aoqi@0: for (List> v = compound.values;
aoqi@0: v.nonEmpty(); v = v.tail) {
aoqi@0: Pair value = v.head;
aoqi@0: if (value.fst.name.toString().equals("value"))
aoqi@0: value.snd.accept(this);
aoqi@0: }
aoqi@0:
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void visitArray(Attribute.Array array) {
aoqi@0: for (Attribute value : array.values)
aoqi@0: value.accept(this);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitEnum(Attribute.Enum e) {
aoqi@0: }
aoqi@0:
aoqi@0: public void visitError(Attribute.Error e) {
aoqi@0: }
aoqi@0: };
aoqi@0: }