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

Mon, 04 Feb 2013 18:08:53 -0500

author
dholmes
date
Mon, 04 Feb 2013 18:08:53 -0500
changeset 1570
f91144b7da75
parent 1521
71f35e4b93a5
child 1588
2620c953e9fe
permissions
-rw-r--r--

Merge

     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.HashMap;
    30 import java.util.Map;
    31 import java.util.Set;
    32 import javax.lang.model.element.Modifier;
    33 import com.sun.tools.javac.code.Symbol.*;
    34 import com.sun.tools.javac.util.Context;
    35 import com.sun.tools.javac.util.List;
    36 import com.sun.tools.javac.util.Options;
    37 import com.sun.tools.javac.util.Pair;
    39 import static com.sun.tools.javac.code.Flags.*;
    42 /**
    43  * A class for handling -Xlint suboptions and @SuppresssWarnings.
    44  *
    45  *  <p><b>This is NOT part of any supported API.
    46  *  If you write code that depends on this, you do so at your own risk.
    47  *  This code and its internal interfaces are subject to change or
    48  *  deletion without notice.</b>
    49  */
    50 public class Lint
    51 {
    52     /** The context key for the root Lint object. */
    53     protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
    55     /** Get the root Lint instance. */
    56     public static Lint instance(Context context) {
    57         Lint instance = context.get(lintKey);
    58         if (instance == null)
    59             instance = new Lint(context);
    60         return instance;
    61     }
    63     /**
    64      * Returns the result of combining the values in this object with
    65      * the given annotation.
    66      */
    67     public Lint augment(Attribute.Compound attr) {
    68         return augmentor.augment(this, attr);
    69     }
    72     /**
    73      * Returns the result of combining the values in this object with
    74      * the given annotations.
    75      */
    76     public Lint augment(Annotations annots) {
    77         return augmentor.augment(this, annots.getDeclarationAttributes());
    78     }
    80     /**
    81      * Returns the result of combining the values in this object with
    82      * the given annotations and flags.
    83      */
    84     public Lint augment(Annotations annots, long flags) {
    85         Lint l = augmentor.augment(this, annots.getDeclarationAttributes());
    86         if ((flags & DEPRECATED) != 0) {
    87             if (l == this)
    88                 l = new Lint(this);
    89             l.values.remove(LintCategory.DEPRECATION);
    90             l.suppressedValues.add(LintCategory.DEPRECATION);
    91         }
    92         return l;
    93     }
    96     private final AugmentVisitor augmentor;
    98     private final EnumSet<LintCategory> values;
    99     private final EnumSet<LintCategory> suppressedValues;
   101     private static final Map<String, LintCategory> map =
   102             new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20);
   105     protected Lint(Context context) {
   106         // initialize values according to the lint options
   107         Options options = Options.instance(context);
   108         values = EnumSet.noneOf(LintCategory.class);
   109         for (Map.Entry<String, LintCategory> e: map.entrySet()) {
   110             if (options.lint(e.getKey()))
   111                 values.add(e.getValue());
   112         }
   114         suppressedValues = EnumSet.noneOf(LintCategory.class);
   116         context.put(lintKey, this);
   117         augmentor = new AugmentVisitor(context);
   118     }
   120     protected Lint(Lint other) {
   121         this.augmentor = other.augmentor;
   122         this.values = other.values.clone();
   123         this.suppressedValues = other.suppressedValues.clone();
   124     }
   126     @Override
   127     public String toString() {
   128         return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
   129     }
   131     /**
   132      * Categories of warnings that can be generated by the compiler.
   133      */
   134     public enum LintCategory {
   135         /**
   136          * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
   137          * different from the class name, and the type is not properly nested) and the referring code
   138          * is not located in the same source file.
   139          */
   140         AUXILIARYCLASS("auxiliaryclass"),
   142         /**
   143          * Warn about use of unnecessary casts.
   144          */
   145         CAST("cast"),
   147         /**
   148          * Warn about issues related to classfile contents
   149          */
   150         CLASSFILE("classfile"),
   152         /**
   153          * Warn about use of deprecated items.
   154          */
   155         DEPRECATION("deprecation"),
   157         /**
   158          * Warn about items which are documented with an {@code @deprecated} JavaDoc
   159          * comment, but which do not have {@code @Deprecated} annotation.
   160          */
   161         DEP_ANN("dep-ann"),
   163         /**
   164          * Warn about division by constant integer 0.
   165          */
   166         DIVZERO("divzero"),
   168         /**
   169          * Warn about empty statement after if.
   170          */
   171         EMPTY("empty"),
   173         /**
   174          * Warn about falling through from one case of a switch statement to the next.
   175          */
   176         FALLTHROUGH("fallthrough"),
   178         /**
   179          * Warn about finally clauses that do not terminate normally.
   180          */
   181         FINALLY("finally"),
   183         /**
   184          * Warn about issues relating to use of command line options
   185          */
   186         OPTIONS("options"),
   188         /**
   189          * Warn about issues regarding method overrides.
   190          */
   191         OVERRIDES("overrides"),
   193         /**
   194          * Warn about invalid path elements on the command line.
   195          * Such warnings cannot be suppressed with the SuppressWarnings
   196          * annotation.
   197          */
   198         PATH("path"),
   200         /**
   201          * Warn about issues regarding annotation processing.
   202          */
   203         PROCESSING("processing"),
   205         /**
   206          * Warn about unchecked operations on raw types.
   207          */
   208         RAW("rawtypes"),
   210         /**
   211          * Warn about Serializable classes that do not provide a serial version ID.
   212          */
   213         SERIAL("serial"),
   215         /**
   216          * Warn about issues relating to use of statics
   217          */
   218         STATIC("static"),
   220         /**
   221          * Warn about proprietary API that may be removed in a future release.
   222          */
   223         SUNAPI("sunapi", true),
   225         /**
   226          * Warn about issues relating to use of try blocks (i.e. try-with-resources)
   227          */
   228         TRY("try"),
   230         /**
   231          * Warn about unchecked operations on raw types.
   232          */
   233         UNCHECKED("unchecked"),
   235         /**
   236          * Warn about potentially unsafe vararg methods
   237          */
   238         VARARGS("varargs");
   240         LintCategory(String option) {
   241             this(option, false);
   242         }
   244         LintCategory(String option, boolean hidden) {
   245             this.option = option;
   246             this.hidden = hidden;
   247             map.put(option, this);
   248         }
   250         static LintCategory get(String option) {
   251             return map.get(option);
   252         }
   254         public final String option;
   255         public final boolean hidden;
   256     };
   258     /**
   259      * Checks if a warning category is enabled. A warning category may be enabled
   260      * on the command line, or by default, and can be temporarily disabled with
   261      * the SuppressWarnings annotation.
   262      */
   263     public boolean isEnabled(LintCategory lc) {
   264         return values.contains(lc);
   265     }
   267     /**
   268      * Checks is a warning category has been specifically suppressed, by means
   269      * of the SuppressWarnings annotation, or, in the case of the deprecated
   270      * category, whether it has been implicitly suppressed by virtue of the
   271      * current entity being itself deprecated.
   272      */
   273     public boolean isSuppressed(LintCategory lc) {
   274         return suppressedValues.contains(lc);
   275     }
   277     protected static class AugmentVisitor implements Attribute.Visitor {
   278         private final Context context;
   279         private Symtab syms;
   280         private Lint parent;
   281         private Lint lint;
   283         AugmentVisitor(Context context) {
   284             // to break an ugly sequence of initialization dependencies,
   285             // we defer the initialization of syms until it is needed
   286             this.context = context;
   287         }
   289         Lint augment(Lint parent, Attribute.Compound attr) {
   290             initSyms();
   291             this.parent = parent;
   292             lint = null;
   293             attr.accept(this);
   294             return (lint == null ? parent : lint);
   295         }
   297         Lint augment(Lint parent, List<Attribute.Compound> attrs) {
   298             initSyms();
   299             this.parent = parent;
   300             lint = null;
   301             for (Attribute.Compound a: attrs) {
   302                 a.accept(this);
   303             }
   304             return (lint == null ? parent : lint);
   305         }
   307         private void initSyms() {
   308             if (syms == null)
   309                 syms = Symtab.instance(context);
   310         }
   312         private void suppress(LintCategory lc) {
   313             if (lint == null)
   314                 lint = new Lint(parent);
   315             lint.suppressedValues.add(lc);
   316             lint.values.remove(lc);
   317         }
   319         public void visitConstant(Attribute.Constant value) {
   320             if (value.type.tsym == syms.stringType.tsym) {
   321                 LintCategory lc = LintCategory.get((String) (value.value));
   322                 if (lc != null)
   323                     suppress(lc);
   324             }
   325         }
   327         public void visitClass(Attribute.Class clazz) {
   328         }
   330         // If we find a @SuppressWarnings annotation, then we continue
   331         // walking the tree, in order to suppress the individual warnings
   332         // specified in the @SuppressWarnings annotation.
   333         public void visitCompound(Attribute.Compound compound) {
   334             if (compound.type.tsym == syms.suppressWarningsType.tsym) {
   335                 for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
   336                      v.nonEmpty(); v = v.tail) {
   337                     Pair<MethodSymbol,Attribute> value = v.head;
   338                     if (value.fst.name.toString().equals("value"))
   339                         value.snd.accept(this);
   340                 }
   342             }
   343         }
   345         public void visitArray(Attribute.Array array) {
   346             for (Attribute value : array.values)
   347                 value.accept(this);
   348         }
   350         public void visitEnum(Attribute.Enum e) {
   351         }
   353         public void visitError(Attribute.Error e) {
   354         }
   355     };
   356 }

mercurial