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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Lint.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,345 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.code;
    1.30 +
    1.31 +import java.util.EnumSet;
    1.32 +import java.util.Map;
    1.33 +import com.sun.tools.javac.code.Symbol.*;
    1.34 +import com.sun.tools.javac.util.Context;
    1.35 +import com.sun.tools.javac.util.List;
    1.36 +import com.sun.tools.javac.util.Options;
    1.37 +import com.sun.tools.javac.util.Pair;
    1.38 +
    1.39 +/**
    1.40 + * A class for handling -Xlint suboptions and @SuppresssWarnings.
    1.41 + *
    1.42 + *  <p><b>This is NOT part of any supported API.
    1.43 + *  If you write code that depends on this, you do so at your own risk.
    1.44 + *  This code and its internal interfaces are subject to change or
    1.45 + *  deletion without notice.</b>
    1.46 + */
    1.47 +public class Lint
    1.48 +{
    1.49 +    /** The context key for the root Lint object. */
    1.50 +    protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
    1.51 +
    1.52 +    /** Get the root Lint instance. */
    1.53 +    public static Lint instance(Context context) {
    1.54 +        Lint instance = context.get(lintKey);
    1.55 +        if (instance == null)
    1.56 +            instance = new Lint(context);
    1.57 +        return instance;
    1.58 +    }
    1.59 +
    1.60 +    /**
    1.61 +     * Returns the result of combining the values in this object with
    1.62 +     * the given annotation.
    1.63 +     */
    1.64 +    public Lint augment(Attribute.Compound attr) {
    1.65 +        return augmentor.augment(this, attr);
    1.66 +    }
    1.67 +
    1.68 +
    1.69 +    /**
    1.70 +     * Returns the result of combining the values in this object with
    1.71 +     * the metadata on the given symbol.
    1.72 +     */
    1.73 +    public Lint augment(Symbol sym) {
    1.74 +        Lint l = augmentor.augment(this, sym.getDeclarationAttributes());
    1.75 +        if (sym.isDeprecated()) {
    1.76 +            if (l == this)
    1.77 +                l = new Lint(this);
    1.78 +            l.values.remove(LintCategory.DEPRECATION);
    1.79 +            l.suppressedValues.add(LintCategory.DEPRECATION);
    1.80 +        }
    1.81 +        return l;
    1.82 +    }
    1.83 +
    1.84 +    private final AugmentVisitor augmentor;
    1.85 +
    1.86 +    private final EnumSet<LintCategory> values;
    1.87 +    private final EnumSet<LintCategory> suppressedValues;
    1.88 +
    1.89 +    private static final Map<String, LintCategory> map =
    1.90 +            new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20);
    1.91 +
    1.92 +    protected Lint(Context context) {
    1.93 +        // initialize values according to the lint options
    1.94 +        Options options = Options.instance(context);
    1.95 +        values = EnumSet.noneOf(LintCategory.class);
    1.96 +        for (Map.Entry<String, LintCategory> e: map.entrySet()) {
    1.97 +            if (options.lint(e.getKey()))
    1.98 +                values.add(e.getValue());
    1.99 +        }
   1.100 +
   1.101 +        suppressedValues = EnumSet.noneOf(LintCategory.class);
   1.102 +
   1.103 +        context.put(lintKey, this);
   1.104 +        augmentor = new AugmentVisitor(context);
   1.105 +    }
   1.106 +
   1.107 +    protected Lint(Lint other) {
   1.108 +        this.augmentor = other.augmentor;
   1.109 +        this.values = other.values.clone();
   1.110 +        this.suppressedValues = other.suppressedValues.clone();
   1.111 +    }
   1.112 +
   1.113 +    @Override
   1.114 +    public String toString() {
   1.115 +        return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Categories of warnings that can be generated by the compiler.
   1.120 +     */
   1.121 +    public enum LintCategory {
   1.122 +        /**
   1.123 +         * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
   1.124 +         * different from the class name, and the type is not properly nested) and the referring code
   1.125 +         * is not located in the same source file.
   1.126 +         */
   1.127 +        AUXILIARYCLASS("auxiliaryclass"),
   1.128 +
   1.129 +        /**
   1.130 +         * Warn about use of unnecessary casts.
   1.131 +         */
   1.132 +        CAST("cast"),
   1.133 +
   1.134 +        /**
   1.135 +         * Warn about issues related to classfile contents
   1.136 +         */
   1.137 +        CLASSFILE("classfile"),
   1.138 +
   1.139 +        /**
   1.140 +         * Warn about use of deprecated items.
   1.141 +         */
   1.142 +        DEPRECATION("deprecation"),
   1.143 +
   1.144 +        /**
   1.145 +         * Warn about items which are documented with an {@code @deprecated} JavaDoc
   1.146 +         * comment, but which do not have {@code @Deprecated} annotation.
   1.147 +         */
   1.148 +        DEP_ANN("dep-ann"),
   1.149 +
   1.150 +        /**
   1.151 +         * Warn about division by constant integer 0.
   1.152 +         */
   1.153 +        DIVZERO("divzero"),
   1.154 +
   1.155 +        /**
   1.156 +         * Warn about empty statement after if.
   1.157 +         */
   1.158 +        EMPTY("empty"),
   1.159 +
   1.160 +        /**
   1.161 +         * Warn about falling through from one case of a switch statement to the next.
   1.162 +         */
   1.163 +        FALLTHROUGH("fallthrough"),
   1.164 +
   1.165 +        /**
   1.166 +         * Warn about finally clauses that do not terminate normally.
   1.167 +         */
   1.168 +        FINALLY("finally"),
   1.169 +
   1.170 +        /**
   1.171 +         * Warn about issues relating to use of command line options
   1.172 +         */
   1.173 +        OPTIONS("options"),
   1.174 +
   1.175 +        /**
   1.176 +         * Warn about issues regarding method overloads.
   1.177 +         */
   1.178 +        OVERLOADS("overloads"),
   1.179 +
   1.180 +        /**
   1.181 +         * Warn about issues regarding method overrides.
   1.182 +         */
   1.183 +        OVERRIDES("overrides"),
   1.184 +
   1.185 +        /**
   1.186 +         * Warn about invalid path elements on the command line.
   1.187 +         * Such warnings cannot be suppressed with the SuppressWarnings
   1.188 +         * annotation.
   1.189 +         */
   1.190 +        PATH("path"),
   1.191 +
   1.192 +        /**
   1.193 +         * Warn about issues regarding annotation processing.
   1.194 +         */
   1.195 +        PROCESSING("processing"),
   1.196 +
   1.197 +        /**
   1.198 +         * Warn about unchecked operations on raw types.
   1.199 +         */
   1.200 +        RAW("rawtypes"),
   1.201 +
   1.202 +        /**
   1.203 +         * Warn about Serializable classes that do not provide a serial version ID.
   1.204 +         */
   1.205 +        SERIAL("serial"),
   1.206 +
   1.207 +        /**
   1.208 +         * Warn about issues relating to use of statics
   1.209 +         */
   1.210 +        STATIC("static"),
   1.211 +
   1.212 +        /**
   1.213 +         * Warn about proprietary API that may be removed in a future release.
   1.214 +         */
   1.215 +        SUNAPI("sunapi", true),
   1.216 +
   1.217 +        /**
   1.218 +         * Warn about issues relating to use of try blocks (i.e. try-with-resources)
   1.219 +         */
   1.220 +        TRY("try"),
   1.221 +
   1.222 +        /**
   1.223 +         * Warn about unchecked operations on raw types.
   1.224 +         */
   1.225 +        UNCHECKED("unchecked"),
   1.226 +
   1.227 +        /**
   1.228 +         * Warn about potentially unsafe vararg methods
   1.229 +         */
   1.230 +        VARARGS("varargs");
   1.231 +
   1.232 +        LintCategory(String option) {
   1.233 +            this(option, false);
   1.234 +        }
   1.235 +
   1.236 +        LintCategory(String option, boolean hidden) {
   1.237 +            this.option = option;
   1.238 +            this.hidden = hidden;
   1.239 +            map.put(option, this);
   1.240 +        }
   1.241 +
   1.242 +        static LintCategory get(String option) {
   1.243 +            return map.get(option);
   1.244 +        }
   1.245 +
   1.246 +        public final String option;
   1.247 +        public final boolean hidden;
   1.248 +    };
   1.249 +
   1.250 +    /**
   1.251 +     * Checks if a warning category is enabled. A warning category may be enabled
   1.252 +     * on the command line, or by default, and can be temporarily disabled with
   1.253 +     * the SuppressWarnings annotation.
   1.254 +     */
   1.255 +    public boolean isEnabled(LintCategory lc) {
   1.256 +        return values.contains(lc);
   1.257 +    }
   1.258 +
   1.259 +    /**
   1.260 +     * Checks is a warning category has been specifically suppressed, by means
   1.261 +     * of the SuppressWarnings annotation, or, in the case of the deprecated
   1.262 +     * category, whether it has been implicitly suppressed by virtue of the
   1.263 +     * current entity being itself deprecated.
   1.264 +     */
   1.265 +    public boolean isSuppressed(LintCategory lc) {
   1.266 +        return suppressedValues.contains(lc);
   1.267 +    }
   1.268 +
   1.269 +    protected static class AugmentVisitor implements Attribute.Visitor {
   1.270 +        private final Context context;
   1.271 +        private Symtab syms;
   1.272 +        private Lint parent;
   1.273 +        private Lint lint;
   1.274 +
   1.275 +        AugmentVisitor(Context context) {
   1.276 +            // to break an ugly sequence of initialization dependencies,
   1.277 +            // we defer the initialization of syms until it is needed
   1.278 +            this.context = context;
   1.279 +        }
   1.280 +
   1.281 +        Lint augment(Lint parent, Attribute.Compound attr) {
   1.282 +            initSyms();
   1.283 +            this.parent = parent;
   1.284 +            lint = null;
   1.285 +            attr.accept(this);
   1.286 +            return (lint == null ? parent : lint);
   1.287 +        }
   1.288 +
   1.289 +        Lint augment(Lint parent, List<Attribute.Compound> attrs) {
   1.290 +            initSyms();
   1.291 +            this.parent = parent;
   1.292 +            lint = null;
   1.293 +            for (Attribute.Compound a: attrs) {
   1.294 +                a.accept(this);
   1.295 +            }
   1.296 +            return (lint == null ? parent : lint);
   1.297 +        }
   1.298 +
   1.299 +        private void initSyms() {
   1.300 +            if (syms == null)
   1.301 +                syms = Symtab.instance(context);
   1.302 +        }
   1.303 +
   1.304 +        private void suppress(LintCategory lc) {
   1.305 +            if (lint == null)
   1.306 +                lint = new Lint(parent);
   1.307 +            lint.suppressedValues.add(lc);
   1.308 +            lint.values.remove(lc);
   1.309 +        }
   1.310 +
   1.311 +        public void visitConstant(Attribute.Constant value) {
   1.312 +            if (value.type.tsym == syms.stringType.tsym) {
   1.313 +                LintCategory lc = LintCategory.get((String) (value.value));
   1.314 +                if (lc != null)
   1.315 +                    suppress(lc);
   1.316 +            }
   1.317 +        }
   1.318 +
   1.319 +        public void visitClass(Attribute.Class clazz) {
   1.320 +        }
   1.321 +
   1.322 +        // If we find a @SuppressWarnings annotation, then we continue
   1.323 +        // walking the tree, in order to suppress the individual warnings
   1.324 +        // specified in the @SuppressWarnings annotation.
   1.325 +        public void visitCompound(Attribute.Compound compound) {
   1.326 +            if (compound.type.tsym == syms.suppressWarningsType.tsym) {
   1.327 +                for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
   1.328 +                     v.nonEmpty(); v = v.tail) {
   1.329 +                    Pair<MethodSymbol,Attribute> value = v.head;
   1.330 +                    if (value.fst.name.toString().equals("value"))
   1.331 +                        value.snd.accept(this);
   1.332 +                }
   1.333 +
   1.334 +            }
   1.335 +        }
   1.336 +
   1.337 +        public void visitArray(Attribute.Array array) {
   1.338 +            for (Attribute value : array.values)
   1.339 +                value.accept(this);
   1.340 +        }
   1.341 +
   1.342 +        public void visitEnum(Attribute.Enum e) {
   1.343 +        }
   1.344 +
   1.345 +        public void visitError(Attribute.Error e) {
   1.346 +        }
   1.347 +    };
   1.348 +}

mercurial