< prev index next >

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

Print this page




   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  */
  25 
  26 package com.sun.tools.javac.code;
  27 

  28 import java.util.EnumSet;
  29 import java.util.Map;
  30 import java.util.concurrent.ConcurrentHashMap;
  31 
  32 import com.sun.tools.javac.code.Symbol.*;

  33 import com.sun.tools.javac.util.Context;
  34 import com.sun.tools.javac.util.List;
  35 import com.sun.tools.javac.util.Options;
  36 import com.sun.tools.javac.util.Pair;
  37 
  38 /**
  39  * A class for handling -Xlint suboptions and @SuppresssWarnings.
  40  *
  41  *  <p><b>This is NOT part of any supported API.
  42  *  If you write code that depends on this, you do so at your own risk.
  43  *  This code and its internal interfaces are subject to change or
  44  *  deletion without notice.</b>
  45  */
  46 public class Lint
  47 {
  48     /** The context key for the root Lint object. */
  49     protected static final Context.Key<Lint> lintKey = new Context.Key<>();
  50 
  51     /** Get the root Lint instance. */
  52     public static Lint instance(Context context) {


  64         return augmentor.augment(this, attr);
  65     }
  66 
  67 
  68     /**
  69      * Returns the result of combining the values in this object with
  70      * the metadata on the given symbol.
  71      */
  72     public Lint augment(Symbol sym) {
  73         Lint l = augmentor.augment(this, sym.getDeclarationAttributes());
  74         if (sym.isDeprecated()) {
  75             if (l == this)
  76                 l = new Lint(this);
  77             l.values.remove(LintCategory.DEPRECATION);
  78             l.suppressedValues.add(LintCategory.DEPRECATION);
  79         }
  80         return l;
  81     }
  82 
  83     /**
  84      * Returns a new Lint that has the given LintCategory suppressed.

  85      */
  86     public Lint suppress(LintCategory lc) {
  87         Lint l = new Lint(this);
  88         l.values.remove(lc);
  89         l.suppressedValues.add(lc);
  90         return l;
  91     }
  92 
  93     private final AugmentVisitor augmentor;
  94 
  95     private final EnumSet<LintCategory> values;
  96     private final EnumSet<LintCategory> suppressedValues;
  97 
  98     private static final Map<String, LintCategory> map = new ConcurrentHashMap<>(20);
  99 
 100     protected Lint(Context context) {
 101         // initialize values according to the lint options
 102         Options options = Options.instance(context);









 103         values = EnumSet.noneOf(LintCategory.class);
 104         for (Map.Entry<String, LintCategory> e: map.entrySet()) {
 105             if (options.lint(e.getKey()))
 106                 values.add(e.getValue());












 107         }
 108 
 109         suppressedValues = EnumSet.noneOf(LintCategory.class);
 110 
 111         context.put(lintKey, this);
 112         augmentor = new AugmentVisitor(context);
 113     }
 114 
 115     protected Lint(Lint other) {
 116         this.augmentor = other.augmentor;
 117         this.values = other.values.clone();
 118         this.suppressedValues = other.suppressedValues.clone();
 119     }
 120 
 121     @Override
 122     public String toString() {
 123         return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
 124     }
 125 
 126     /**


 194          * Warn about issues regarding method overrides.
 195          */
 196         OVERRIDES("overrides"),
 197 
 198         /**
 199          * Warn about invalid path elements on the command line.
 200          * Such warnings cannot be suppressed with the SuppressWarnings
 201          * annotation.
 202          */
 203         PATH("path"),
 204 
 205         /**
 206          * Warn about issues regarding annotation processing.
 207          */
 208         PROCESSING("processing"),
 209 
 210         /**
 211          * Warn about unchecked operations on raw types.
 212          */
 213         RAW("rawtypes"),





 214 
 215         /**
 216          * Warn about Serializable classes that do not provide a serial version ID.
 217          */
 218         SERIAL("serial"),
 219 
 220         /**
 221          * Warn about issues relating to use of statics
 222          */
 223         STATIC("static"),
 224 
 225         /**
 226          * Warn about issues relating to use of try blocks (i.e. try-with-resources)
 227          */
 228         TRY("try"),
 229 
 230         /**
 231          * Warn about unchecked operations on raw types.
 232          */
 233         UNCHECKED("unchecked"),




   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  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.util.Arrays;
  29 import java.util.EnumSet;
  30 import java.util.Map;
  31 import java.util.concurrent.ConcurrentHashMap;
  32 
  33 import com.sun.tools.javac.code.Symbol.*;
  34 import com.sun.tools.javac.main.Option;
  35 import com.sun.tools.javac.util.Context;
  36 import com.sun.tools.javac.util.List;
  37 import com.sun.tools.javac.util.Options;
  38 import com.sun.tools.javac.util.Pair;
  39 
  40 /**
  41  * A class for handling -Xlint suboptions and @SuppresssWarnings.
  42  *
  43  *  <p><b>This is NOT part of any supported API.
  44  *  If you write code that depends on this, you do so at your own risk.
  45  *  This code and its internal interfaces are subject to change or
  46  *  deletion without notice.</b>
  47  */
  48 public class Lint
  49 {
  50     /** The context key for the root Lint object. */
  51     protected static final Context.Key<Lint> lintKey = new Context.Key<>();
  52 
  53     /** Get the root Lint instance. */
  54     public static Lint instance(Context context) {


  66         return augmentor.augment(this, attr);
  67     }
  68 
  69 
  70     /**
  71      * Returns the result of combining the values in this object with
  72      * the metadata on the given symbol.
  73      */
  74     public Lint augment(Symbol sym) {
  75         Lint l = augmentor.augment(this, sym.getDeclarationAttributes());
  76         if (sym.isDeprecated()) {
  77             if (l == this)
  78                 l = new Lint(this);
  79             l.values.remove(LintCategory.DEPRECATION);
  80             l.suppressedValues.add(LintCategory.DEPRECATION);
  81         }
  82         return l;
  83     }
  84 
  85     /**
  86      * Returns a new Lint that has the given LintCategorys suppressed.
  87      * @param lc one or more categories to be suppressed
  88      */
  89     public Lint suppress(LintCategory... lc) {
  90         Lint l = new Lint(this);
  91         l.values.removeAll(Arrays.asList(lc));
  92         l.suppressedValues.addAll(Arrays.asList(lc));
  93         return l;
  94     }
  95 
  96     private final AugmentVisitor augmentor;
  97 
  98     private final EnumSet<LintCategory> values;
  99     private final EnumSet<LintCategory> suppressedValues;
 100 
 101     private static final Map<String, LintCategory> map = new ConcurrentHashMap<>(20);
 102 
 103     protected Lint(Context context) {
 104         // initialize values according to the lint options
 105         Options options = Options.instance(context);
 106 
 107         if (options.isSet(Option.XLINT) || options.isSet(Option.XLINT_CUSTOM, "all")) {
 108             // If -Xlint or -Xlint:all is given, enable all categories by default
 109             values = EnumSet.allOf(LintCategory.class);
 110         } else if (options.isSet(Option.XLINT_CUSTOM, "none")) {
 111             // if -Xlint:none is given, disable all categories by default
 112             values = EnumSet.noneOf(LintCategory.class);
 113         } else {
 114             // otherwise, enable on-by-default categories
 115             values = EnumSet.noneOf(LintCategory.class);
 116 
 117             Source source = Source.instance(context);
 118             if (source.compareTo(Source.JDK1_9) >= 0) {
 119                 values.add(LintCategory.DEP_ANN);
 120             }
 121             values.add(LintCategory.REMOVAL);
 122         }
 123 
 124         // Look for specific overrides
 125         for (LintCategory lc : LintCategory.values()) {
 126             if (options.isSet(Option.XLINT_CUSTOM, lc.option)) {
 127                 values.add(lc);
 128             } else if (options.isSet(Option.XLINT_CUSTOM, "-" + lc.option)) {
 129                 values.remove(lc);
 130             }
 131         }
 132         
 133         suppressedValues = EnumSet.noneOf(LintCategory.class);
 134 
 135         context.put(lintKey, this);
 136         augmentor = new AugmentVisitor(context);
 137     }
 138 
 139     protected Lint(Lint other) {
 140         this.augmentor = other.augmentor;
 141         this.values = other.values.clone();
 142         this.suppressedValues = other.suppressedValues.clone();
 143     }
 144 
 145     @Override
 146     public String toString() {
 147         return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
 148     }
 149 
 150     /**


 218          * Warn about issues regarding method overrides.
 219          */
 220         OVERRIDES("overrides"),
 221 
 222         /**
 223          * Warn about invalid path elements on the command line.
 224          * Such warnings cannot be suppressed with the SuppressWarnings
 225          * annotation.
 226          */
 227         PATH("path"),
 228 
 229         /**
 230          * Warn about issues regarding annotation processing.
 231          */
 232         PROCESSING("processing"),
 233 
 234         /**
 235          * Warn about unchecked operations on raw types.
 236          */
 237         RAW("rawtypes"),
 238 
 239         /**
 240          * Warn about use of deprecated-for-removal items.
 241          */
 242         REMOVAL("removal"),
 243 
 244         /**
 245          * Warn about Serializable classes that do not provide a serial version ID.
 246          */
 247         SERIAL("serial"),
 248 
 249         /**
 250          * Warn about issues relating to use of statics
 251          */
 252         STATIC("static"),
 253 
 254         /**
 255          * Warn about issues relating to use of try blocks (i.e. try-with-resources)
 256          */
 257         TRY("try"),
 258 
 259         /**
 260          * Warn about unchecked operations on raw types.
 261          */
 262         UNCHECKED("unchecked"),


< prev index next >