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

Mon, 02 Sep 2013 22:38:36 +0100

author
vromero
date
Mon, 02 Sep 2013 22:38:36 +0100
changeset 2000
4a6acc42c3a1
parent 1802
8fb68f73d4b1
child 2027
4932bb04c4b8
permissions
-rw-r--r--

8016177: structural most specific and stuckness
Reviewed-by: jjg, vromero
Contributed-by: maurizio.cimadamore@oracle.com

     1 /*
     2  * Copyright (c) 2005, 2013, 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.Map;
    30 import com.sun.tools.javac.code.Symbol.*;
    31 import com.sun.tools.javac.util.Context;
    32 import com.sun.tools.javac.util.List;
    33 import com.sun.tools.javac.util.Options;
    34 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 metadata on the given symbol.
    72      */
    73     public Lint augment(Symbol sym) {
    74         Lint l = augmentor.augment(this, sym.getDeclarationAttributes());
    75         if (sym.isDeprecated()) {
    76             if (l == this)
    77                 l = new Lint(this);
    78             l.values.remove(LintCategory.DEPRECATION);
    79             l.suppressedValues.add(LintCategory.DEPRECATION);
    80         }
    81         return l;
    82     }
    85     private final AugmentVisitor augmentor;
    87     private final EnumSet<LintCategory> values;
    88     private final EnumSet<LintCategory> suppressedValues;
    90     private static final Map<String, LintCategory> map =
    91             new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20);
    94     protected Lint(Context context) {
    95         // initialize values according to the lint options
    96         Options options = Options.instance(context);
    97         values = EnumSet.noneOf(LintCategory.class);
    98         for (Map.Entry<String, LintCategory> e: map.entrySet()) {
    99             if (options.lint(e.getKey()))
   100                 values.add(e.getValue());
   101         }
   103         suppressedValues = EnumSet.noneOf(LintCategory.class);
   105         context.put(lintKey, this);
   106         augmentor = new AugmentVisitor(context);
   107     }
   109     protected Lint(Lint other) {
   110         this.augmentor = other.augmentor;
   111         this.values = other.values.clone();
   112         this.suppressedValues = other.suppressedValues.clone();
   113     }
   115     @Override
   116     public String toString() {
   117         return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
   118     }
   120     /**
   121      * Categories of warnings that can be generated by the compiler.
   122      */
   123     public enum LintCategory {
   124         /**
   125          * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
   126          * different from the class name, and the type is not properly nested) and the referring code
   127          * is not located in the same source file.
   128          */
   129         AUXILIARYCLASS("auxiliaryclass"),
   131         /**
   132          * Warn about use of unnecessary casts.
   133          */
   134         CAST("cast"),
   136         /**
   137          * Warn about issues related to classfile contents
   138          */
   139         CLASSFILE("classfile"),
   141         /**
   142          * Warn about use of deprecated items.
   143          */
   144         DEPRECATION("deprecation"),
   146         /**
   147          * Warn about items which are documented with an {@code @deprecated} JavaDoc
   148          * comment, but which do not have {@code @Deprecated} annotation.
   149          */
   150         DEP_ANN("dep-ann"),
   152         /**
   153          * Warn about division by constant integer 0.
   154          */
   155         DIVZERO("divzero"),
   157         /**
   158          * Warn about empty statement after if.
   159          */
   160         EMPTY("empty"),
   162         /**
   163          * Warn about falling through from one case of a switch statement to the next.
   164          */
   165         FALLTHROUGH("fallthrough"),
   167         /**
   168          * Warn about finally clauses that do not terminate normally.
   169          */
   170         FINALLY("finally"),
   172         /**
   173          * Warn about issues relating to use of command line options
   174          */
   175         OPTIONS("options"),
   177         /**
   178          * Warn about issues regarding method overloads.
   179          */
   180         OVERLOADS("overloads"),
   182         /**
   183          * Warn about issues regarding method overrides.
   184          */
   185         OVERRIDES("overrides"),
   187         /**
   188          * Warn about invalid path elements on the command line.
   189          * Such warnings cannot be suppressed with the SuppressWarnings
   190          * annotation.
   191          */
   192         PATH("path"),
   194         /**
   195          * Warn about issues regarding annotation processing.
   196          */
   197         PROCESSING("processing"),
   199         /**
   200          * Warn about unchecked operations on raw types.
   201          */
   202         RAW("rawtypes"),
   204         /**
   205          * Warn about Serializable classes that do not provide a serial version ID.
   206          */
   207         SERIAL("serial"),
   209         /**
   210          * Warn about issues relating to use of statics
   211          */
   212         STATIC("static"),
   214         /**
   215          * Warn about proprietary API that may be removed in a future release.
   216          */
   217         SUNAPI("sunapi", true),
   219         /**
   220          * Warn about issues relating to use of try blocks (i.e. try-with-resources)
   221          */
   222         TRY("try"),
   224         /**
   225          * Warn about unchecked operations on raw types.
   226          */
   227         UNCHECKED("unchecked"),
   229         /**
   230          * Warn about potentially unsafe vararg methods
   231          */
   232         VARARGS("varargs");
   234         LintCategory(String option) {
   235             this(option, false);
   236         }
   238         LintCategory(String option, boolean hidden) {
   239             this.option = option;
   240             this.hidden = hidden;
   241             map.put(option, this);
   242         }
   244         static LintCategory get(String option) {
   245             return map.get(option);
   246         }
   248         public final String option;
   249         public final boolean hidden;
   250     };
   252     /**
   253      * Checks if a warning category is enabled. A warning category may be enabled
   254      * on the command line, or by default, and can be temporarily disabled with
   255      * the SuppressWarnings annotation.
   256      */
   257     public boolean isEnabled(LintCategory lc) {
   258         return values.contains(lc);
   259     }
   261     /**
   262      * Checks is a warning category has been specifically suppressed, by means
   263      * of the SuppressWarnings annotation, or, in the case of the deprecated
   264      * category, whether it has been implicitly suppressed by virtue of the
   265      * current entity being itself deprecated.
   266      */
   267     public boolean isSuppressed(LintCategory lc) {
   268         return suppressedValues.contains(lc);
   269     }
   271     protected static class AugmentVisitor implements Attribute.Visitor {
   272         private final Context context;
   273         private Symtab syms;
   274         private Lint parent;
   275         private Lint lint;
   277         AugmentVisitor(Context context) {
   278             // to break an ugly sequence of initialization dependencies,
   279             // we defer the initialization of syms until it is needed
   280             this.context = context;
   281         }
   283         Lint augment(Lint parent, Attribute.Compound attr) {
   284             initSyms();
   285             this.parent = parent;
   286             lint = null;
   287             attr.accept(this);
   288             return (lint == null ? parent : lint);
   289         }
   291         Lint augment(Lint parent, List<Attribute.Compound> attrs) {
   292             initSyms();
   293             this.parent = parent;
   294             lint = null;
   295             for (Attribute.Compound a: attrs) {
   296                 a.accept(this);
   297             }
   298             return (lint == null ? parent : lint);
   299         }
   301         private void initSyms() {
   302             if (syms == null)
   303                 syms = Symtab.instance(context);
   304         }
   306         private void suppress(LintCategory lc) {
   307             if (lint == null)
   308                 lint = new Lint(parent);
   309             lint.suppressedValues.add(lc);
   310             lint.values.remove(lc);
   311         }
   313         public void visitConstant(Attribute.Constant value) {
   314             if (value.type.tsym == syms.stringType.tsym) {
   315                 LintCategory lc = LintCategory.get((String) (value.value));
   316                 if (lc != null)
   317                     suppress(lc);
   318             }
   319         }
   321         public void visitClass(Attribute.Class clazz) {
   322         }
   324         // If we find a @SuppressWarnings annotation, then we continue
   325         // walking the tree, in order to suppress the individual warnings
   326         // specified in the @SuppressWarnings annotation.
   327         public void visitCompound(Attribute.Compound compound) {
   328             if (compound.type.tsym == syms.suppressWarningsType.tsym) {
   329                 for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
   330                      v.nonEmpty(); v = v.tail) {
   331                     Pair<MethodSymbol,Attribute> value = v.head;
   332                     if (value.fst.name.toString().equals("value"))
   333                         value.snd.accept(this);
   334                 }
   336             }
   337         }
   339         public void visitArray(Attribute.Array array) {
   340             for (Attribute value : array.values)
   341                 value.accept(this);
   342         }
   344         public void visitEnum(Attribute.Enum e) {
   345         }
   347         public void visitError(Attribute.Error e) {
   348         }
   349     };
   350 }

mercurial