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

changeset 1
9a66ca7c79fa
child 11
b66d15dfd001
     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	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,298 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any 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.HashMap;
    1.33 +import java.util.Map;
    1.34 +import com.sun.tools.javac.code.Symbol.*;
    1.35 +import com.sun.tools.javac.util.Context;
    1.36 +import com.sun.tools.javac.util.List;
    1.37 +import com.sun.tools.javac.util.Options;
    1.38 +import com.sun.tools.javac.util.Pair;
    1.39 +import static com.sun.tools.javac.code.Flags.*;
    1.40 +
    1.41 +
    1.42 +/**
    1.43 + * A class for handling -Xlint suboptions and @SuppresssWarnings.
    1.44 + *
    1.45 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.46 + *  you write code that depends on this, you do so at your own risk.
    1.47 + *  This code and its internal interfaces are subject to change or
    1.48 + *  deletion without notice.</b>
    1.49 + */
    1.50 +public class Lint
    1.51 +{
    1.52 +    /** The context key for the root Lint object. */
    1.53 +    protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
    1.54 +
    1.55 +    /** Get the root Lint instance. */
    1.56 +    public static Lint instance(Context context) {
    1.57 +        Lint instance = context.get(lintKey);
    1.58 +        if (instance == null)
    1.59 +            instance = new Lint(context);
    1.60 +        return instance;
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * Returns the result of combining the values in this object with
    1.65 +     * the given annotation.
    1.66 +     */
    1.67 +    public Lint augment(Attribute.Compound attr) {
    1.68 +        return augmentor.augment(this, attr);
    1.69 +    }
    1.70 +
    1.71 +
    1.72 +    /**
    1.73 +     * Returns the result of combining the values in this object with
    1.74 +     * the given annotations.
    1.75 +     */
    1.76 +    public Lint augment(List<Attribute.Compound> attrs) {
    1.77 +        return augmentor.augment(this, attrs);
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Returns the result of combining the values in this object with
    1.82 +     * the given annotations and flags.
    1.83 +     */
    1.84 +    public Lint augment(List<Attribute.Compound> attrs, long flags) {
    1.85 +        Lint l = augmentor.augment(this, attrs);
    1.86 +        if ((flags & DEPRECATED) != 0) {
    1.87 +            if (l == this)
    1.88 +                l = new Lint(this);
    1.89 +            l.values.remove(LintCategory.DEPRECATION);
    1.90 +            l.suppressedValues.add(LintCategory.DEPRECATION);
    1.91 +        }
    1.92 +        return l;
    1.93 +    }
    1.94 +
    1.95 +
    1.96 +    private final AugmentVisitor augmentor;
    1.97 +
    1.98 +    private final EnumSet<LintCategory> values;
    1.99 +    private final EnumSet<LintCategory> suppressedValues;
   1.100 +
   1.101 +    private static Map<String, LintCategory> map = new HashMap<String,LintCategory>();
   1.102 +
   1.103 +
   1.104 +    protected Lint(Context context) {
   1.105 +        // initialize values according to the lint options
   1.106 +        Options options = Options.instance(context);
   1.107 +        values = EnumSet.noneOf(LintCategory.class);
   1.108 +        for (Map.Entry<String, LintCategory> e: map.entrySet()) {
   1.109 +            if (options.lint(e.getKey()))
   1.110 +                values.add(e.getValue());
   1.111 +        }
   1.112 +
   1.113 +        suppressedValues = EnumSet.noneOf(LintCategory.class);
   1.114 +
   1.115 +        context.put(lintKey, this);
   1.116 +        augmentor = new AugmentVisitor(context);
   1.117 +    }
   1.118 +
   1.119 +    protected Lint(Lint other) {
   1.120 +        this.augmentor = other.augmentor;
   1.121 +        this.values = other.values.clone();
   1.122 +        this.suppressedValues = other.suppressedValues.clone();
   1.123 +    }
   1.124 +
   1.125 +    public String toString() {
   1.126 +        return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Categories of warnings that can be generated by the compiler.
   1.131 +     */
   1.132 +    public enum LintCategory {
   1.133 +        /**
   1.134 +         * Warn about use of unnecessary casts.
   1.135 +         */
   1.136 +        CAST("cast"),
   1.137 +
   1.138 +        /**
   1.139 +         * Warn about use of deprecated items.
   1.140 +         */
   1.141 +        DEPRECATION("deprecation"),
   1.142 +
   1.143 +        /**
   1.144 +         * Warn about items which are documented with an {@code @deprecated} JavaDoc
   1.145 +         * comment, but which do not have {@code @Deprecated} annotation.
   1.146 +         */
   1.147 +        DEP_ANN("dep-ann"),
   1.148 +
   1.149 +        /**
   1.150 +         * Warn about division by constant integer 0.
   1.151 +         */
   1.152 +        DIVZERO("divzero"),
   1.153 +
   1.154 +        /**
   1.155 +         * Warn about empty statement after if.
   1.156 +         */
   1.157 +        EMPTY("empty"),
   1.158 +
   1.159 +        /**
   1.160 +         * Warn about falling through from one case of a switch statement to the next.
   1.161 +         */
   1.162 +        FALLTHROUGH("fallthrough"),
   1.163 +
   1.164 +        /**
   1.165 +         * Warn about finally clauses that do not terminate normally.
   1.166 +         */
   1.167 +        FINALLY("finally"),
   1.168 +
   1.169 +        /**
   1.170 +         * Warn about issues regarding method overrides.
   1.171 +         */
   1.172 +        OVERRIDES("overrides"),
   1.173 +
   1.174 +        /**
   1.175 +         * Warn about invalid path elements on the command line.
   1.176 +         * Such warnings cannot be suppressed with the SuppressWarnings
   1.177 +         * annotation.
   1.178 +         */
   1.179 +        PATH("path"),
   1.180 +
   1.181 +        /**
   1.182 +         * Warn about Serializable classes that do not provide a serial version ID.
   1.183 +         */
   1.184 +        SERIAL("serial"),
   1.185 +
   1.186 +        /**
   1.187 +         * Warn about unchecked operations on raw types.
   1.188 +         */
   1.189 +        UNCHECKED("unchecked");
   1.190 +
   1.191 +        LintCategory(String option) {
   1.192 +            this.option = option;
   1.193 +            map.put(option, this);
   1.194 +        }
   1.195 +
   1.196 +        static LintCategory get(String option) {
   1.197 +            return map.get(option);
   1.198 +        }
   1.199 +
   1.200 +        private final String option;
   1.201 +    };
   1.202 +
   1.203 +    /**
   1.204 +     * Checks if a warning category is enabled. A warning category may be enabled
   1.205 +     * on the command line, or by default, and can be temporarily disabled with
   1.206 +     * the SuppressWarnings annotation.
   1.207 +     */
   1.208 +    public boolean isEnabled(LintCategory lc) {
   1.209 +        return values.contains(lc);
   1.210 +    }
   1.211 +
   1.212 +    /**
   1.213 +     * Checks is a warning category has been specifically suppressed, by means
   1.214 +     * of the SuppressWarnings annotation, or, in the case of the deprecated
   1.215 +     * category, whether it has been implicitly suppressed by virtue of the
   1.216 +     * current entity being itself deprecated.
   1.217 +     */
   1.218 +    public boolean isSuppressed(LintCategory lc) {
   1.219 +        return suppressedValues.contains(lc);
   1.220 +    }
   1.221 +
   1.222 +    protected static class AugmentVisitor implements Attribute.Visitor {
   1.223 +        private final Context context;
   1.224 +        private Symtab syms;
   1.225 +        private Lint parent;
   1.226 +        private Lint lint;
   1.227 +
   1.228 +        AugmentVisitor(Context context) {
   1.229 +            // to break an ugly sequence of initialization dependencies,
   1.230 +            // we defer the initialization of syms until it is needed
   1.231 +            this.context = context;
   1.232 +        }
   1.233 +
   1.234 +        Lint augment(Lint parent, Attribute.Compound attr) {
   1.235 +            initSyms();
   1.236 +            this.parent = parent;
   1.237 +            lint = null;
   1.238 +            attr.accept(this);
   1.239 +            return (lint == null ? parent : lint);
   1.240 +        }
   1.241 +
   1.242 +        Lint augment(Lint parent, List<Attribute.Compound> attrs) {
   1.243 +            initSyms();
   1.244 +            this.parent = parent;
   1.245 +            lint = null;
   1.246 +            for (Attribute.Compound a: attrs) {
   1.247 +                a.accept(this);
   1.248 +            }
   1.249 +            return (lint == null ? parent : lint);
   1.250 +        }
   1.251 +
   1.252 +        private void initSyms() {
   1.253 +            if (syms == null)
   1.254 +                syms = Symtab.instance(context);
   1.255 +        }
   1.256 +
   1.257 +        private void suppress(LintCategory lc) {
   1.258 +            if (lint == null)
   1.259 +                lint = new Lint(parent);
   1.260 +            lint.suppressedValues.add(lc);
   1.261 +            lint.values.remove(lc);
   1.262 +        }
   1.263 +
   1.264 +        public void visitConstant(Attribute.Constant value) {
   1.265 +            if (value.type.tsym == syms.stringType.tsym) {
   1.266 +                LintCategory lc = LintCategory.get((String) (value.value));
   1.267 +                if (lc != null)
   1.268 +                    suppress(lc);
   1.269 +            }
   1.270 +        }
   1.271 +
   1.272 +        public void visitClass(Attribute.Class clazz) {
   1.273 +        }
   1.274 +
   1.275 +        // If we find a @SuppressWarnings annotation, then we continue
   1.276 +        // walking the tree, in order to suppress the individual warnings
   1.277 +        // specified in the @SuppressWarnings annotation.
   1.278 +        public void visitCompound(Attribute.Compound compound) {
   1.279 +            if (compound.type.tsym == syms.suppressWarningsType.tsym) {
   1.280 +                for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
   1.281 +                     v.nonEmpty(); v = v.tail) {
   1.282 +                    Pair<MethodSymbol,Attribute> value = v.head;
   1.283 +                    if (value.fst.name.toString().equals("value"))
   1.284 +                        value.snd.accept(this);
   1.285 +                }
   1.286 +
   1.287 +            }
   1.288 +        }
   1.289 +
   1.290 +        public void visitArray(Attribute.Array array) {
   1.291 +            for (Attribute value : array.values)
   1.292 +                value.accept(this);
   1.293 +        }
   1.294 +
   1.295 +        public void visitEnum(Attribute.Enum e) {
   1.296 +        }
   1.297 +
   1.298 +        public void visitError(Attribute.Error e) {
   1.299 +        }
   1.300 +    };
   1.301 +}

mercurial