langtools/src/share/classes/com/sun/tools/javac/code/Lint.java
author jjg
Mon, 26 Jul 2010 14:25:56 -0700
changeset 6151 dd513881e71d
parent 6148 3a8158299c51
child 7211 163fe60f63de
permissions -rw-r--r--
6957438: improve code for generating warning messages containing option names Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5002
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.code;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.Pair;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * A class for handling -Xlint suboptions and @SuppresssWarnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    42
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    43
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class Lint
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
{
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /** The context key for the root Lint object. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /** Get the root Lint instance. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static Lint instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        Lint instance = context.get(lintKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            instance = new Lint(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * Returns the result of combining the values in this object with
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * the given annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    public Lint augment(Attribute.Compound attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        return augmentor.augment(this, attr);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * Returns the result of combining the values in this object with
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * the given annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public Lint augment(List<Attribute.Compound> attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        return augmentor.augment(this, attrs);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Returns the result of combining the values in this object with
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * the given annotations and flags.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public Lint augment(List<Attribute.Compound> attrs, long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        Lint l = augmentor.augment(this, attrs);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if ((flags & DEPRECATED) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            if (l == this)
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                l = new Lint(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            l.values.remove(LintCategory.DEPRECATION);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            l.suppressedValues.add(LintCategory.DEPRECATION);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return l;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    private final AugmentVisitor augmentor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    private final EnumSet<LintCategory> values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    private final EnumSet<LintCategory> suppressedValues;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    private static Map<String, LintCategory> map = new HashMap<String,LintCategory>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    protected Lint(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        // initialize values according to the lint options
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        values = EnumSet.noneOf(LintCategory.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        for (Map.Entry<String, LintCategory> e: map.entrySet()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            if (options.lint(e.getKey()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                values.add(e.getValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        suppressedValues = EnumSet.noneOf(LintCategory.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        context.put(lintKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        augmentor = new AugmentVisitor(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    protected Lint(Lint other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        this.augmentor = other.augmentor;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        this.values = other.values.clone();
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        this.suppressedValues = other.suppressedValues.clone();
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 6148
diff changeset
   122
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * Categories of warnings that can be generated by the compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public enum LintCategory {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
         * Warn about use of unnecessary casts.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        CAST("cast"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
         * Warn about use of deprecated items.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        DEPRECATION("deprecation"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
         * Warn about items which are documented with an {@code @deprecated} JavaDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
         * comment, but which do not have {@code @Deprecated} annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        DEP_ANN("dep-ann"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
         * Warn about division by constant integer 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        DIVZERO("divzero"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
         * Warn about empty statement after if.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        EMPTY("empty"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
         * Warn about falling through from one case of a switch statement to the next.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        FALLTHROUGH("fallthrough"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
         * Warn about finally clauses that do not terminate normally.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        FINALLY("finally"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
         * Warn about issues regarding method overrides.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        OVERRIDES("overrides"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
         * Warn about invalid path elements on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
         * Such warnings cannot be suppressed with the SuppressWarnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
         * annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        PATH("path"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        /**
1360
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   180
         * Warn about issues regarding annotation processing.
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   181
         */
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   182
        PROCESSING("processing"),
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   183
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   184
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
         * Warn about Serializable classes that do not provide a serial version ID.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        SERIAL("serial"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
         * Warn about unchecked operations on raw types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
         */
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   192
        UNCHECKED("unchecked"),
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   193
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   194
        /**
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   195
         * Warn about unchecked operations on raw types.
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   196
         */
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   197
        RAW("rawtypes"),
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   198
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   199
        /**
5848
c5a4ce47e780 6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5847
diff changeset
   200
         * Warn about proprietary API that may be removed in a future release.
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   201
         */
5002
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 3661
diff changeset
   202
        SUNAPI("sunapi", true),
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 3661
diff changeset
   203
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 3661
diff changeset
   204
        /**
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 3661
diff changeset
   205
         * Warn about issues relating to use of statics
12a9e8562200 4880220: Add a warning when accessing a static method via an reference
jjg
parents: 3661
diff changeset
   206
         */
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   207
        STATIC("static"),
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   208
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   209
        /**
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   210
         * Warn about potentially unsafe vararg methods
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   211
         */
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   212
        VARARGS("varargs"),
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   213
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   214
        /**
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   215
         * Warn about arm resources
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   216
         */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 5848
diff changeset
   217
        ARM("arm");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        LintCategory(String option) {
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   220
            this(option, false);
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   221
        }
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   222
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   223
        LintCategory(String option, boolean hidden) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            this.option = option;
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   225
            this.hidden = hidden;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            map.put(option, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        static LintCategory get(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
            return map.get(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
168
25697c18650b 6307187: clean up code for -Xlint:options
jjg
parents: 10
diff changeset
   233
        public final String option;
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   234
        public final boolean hidden;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     * Checks if a warning category is enabled. A warning category may be enabled
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     * on the command line, or by default, and can be temporarily disabled with
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     * the SuppressWarnings annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    public boolean isEnabled(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        return values.contains(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     * Checks is a warning category has been specifically suppressed, by means
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     * of the SuppressWarnings annotation, or, in the case of the deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     * category, whether it has been implicitly suppressed by virtue of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     * current entity being itself deprecated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    public boolean isSuppressed(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        return suppressedValues.contains(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    protected static class AugmentVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        private final Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        private Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        private Lint parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        private Lint lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        AugmentVisitor(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            // to break an ugly sequence of initialization dependencies,
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            // we defer the initialization of syms until it is needed
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        Lint augment(Lint parent, Attribute.Compound attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            initSyms();
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            this.parent = parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            lint = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            attr.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            return (lint == null ? parent : lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        Lint augment(Lint parent, List<Attribute.Compound> attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            initSyms();
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            this.parent = parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            lint = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            for (Attribute.Compound a: attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                a.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            return (lint == null ? parent : lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        private void initSyms() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            if (syms == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        private void suppress(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                lint = new Lint(parent);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            lint.suppressedValues.add(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            lint.values.remove(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            if (value.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                LintCategory lc = LintCategory.get((String) (value.value));
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                if (lc != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                    suppress(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        // If we find a @SuppressWarnings annotation, then we continue
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        // walking the tree, in order to suppress the individual warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        // specified in the @SuppressWarnings annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        public void visitCompound(Attribute.Compound compound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            if (compound.type.tsym == syms.suppressWarningsType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                     v.nonEmpty(); v = v.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    Pair<MethodSymbol,Attribute> value = v.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                    if (value.fst.name.toString().equals("value"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                        value.snd.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        public void visitArray(Attribute.Array array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            for (Attribute value : array.values)
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                value.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
}