langtools/src/share/classes/com/sun/tools/javac/code/Lint.java
author ohrstrom
Thu, 01 Nov 2012 10:48:36 +0100
changeset 14369 3d660d08d1f7
parent 13689 4d519199a6aa
child 14801 d66cab4ef397
permissions -rw-r--r--
7153951: Add new lint option -Xlint:auxiliaryclass Reviewed-by: jjg, mcimadamore, forax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
     2
 * Copyright (c) 2005, 2012, 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
     */
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
    73
    public Lint augment(Annotations annots) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
    74
        return augmentor.augment(this, annots.getAttributes());
10
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
     */
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
    81
    public Lint augment(Annotations annots, long flags) {
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 7681
diff changeset
    82
        Lint l = augmentor.augment(this, annots.getAttributes());
10
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
        /**
14369
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   132
         * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   133
         * different from the class name, and the type is not properly nested) and the referring code
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   134
         * is not located in the same source file.
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   135
         */
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   136
        AUXILIARYCLASS("auxiliaryclass"),
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   137
3d660d08d1f7 7153951: Add new lint option -Xlint:auxiliaryclass
ohrstrom
parents: 13689
diff changeset
   138
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
         * Warn about use of unnecessary casts.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        CAST("cast"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        /**
7624
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents: 7335
diff changeset
   144
         * Warn about issues related to classfile contents
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents: 7335
diff changeset
   145
         */
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents: 7335
diff changeset
   146
        CLASSFILE("classfile"),
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents: 7335
diff changeset
   147
c31b0ea95b37 6999210: javac should be able to warn of anomalous conditions in classfiles
jjg
parents: 7335
diff changeset
   148
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
         * Warn about use of deprecated items.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        DEPRECATION("deprecation"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         * Warn about items which are documented with an {@code @deprecated} JavaDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
         * comment, but which do not have {@code @Deprecated} annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        DEP_ANN("dep-ann"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
         * Warn about division by constant integer 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        DIVZERO("divzero"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
         * Warn about empty statement after if.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        EMPTY("empty"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
         * Warn about falling through from one case of a switch statement to the next.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        FALLTHROUGH("fallthrough"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
         * Warn about finally clauses that do not terminate normally.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        FINALLY("finally"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        /**
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   180
         * Warn about issues relating to use of command line options
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   181
         */
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   182
        OPTIONS("options"),
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   183
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   184
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
         * Warn about issues regarding method overrides.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        OVERRIDES("overrides"),
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 invalid path elements on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
         * Such warnings cannot be suppressed with the SuppressWarnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
         * annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        PATH("path"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        /**
1360
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   197
         * Warn about issues regarding annotation processing.
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   198
         */
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   199
        PROCESSING("processing"),
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   200
fd574e4926e3 6739427: -Xlint:processing not recognized as an option
martin
parents: 1358
diff changeset
   201
        /**
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   202
         * Warn about unchecked operations on raw types.
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   203
         */
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   204
        RAW("rawtypes"),
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   205
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   206
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
         * Warn about Serializable classes that do not provide a serial version ID.
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        SERIAL("serial"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        /**
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   212
         * Warn about issues relating to use of statics
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   213
         */
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   214
        STATIC("static"),
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   215
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   216
        /**
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   217
         * Warn about proprietary API that may be removed in a future release.
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   218
         */
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   219
        SUNAPI("sunapi", true),
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   220
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   221
        /**
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   222
         * Warn about issues relating to use of try blocks (i.e. try-with-resources)
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   223
         */
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   224
        TRY("try"),
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   225
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   226
        /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
         * Warn about unchecked operations on raw types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
         */
1358
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   229
        UNCHECKED("unchecked"),
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   230
a51c5f89f8af 6747671: -Xlint:rawtypes
mcimadamore
parents: 735
diff changeset
   231
        /**
5846
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   232
         * Warn about potentially unsafe vararg methods
6df0e6bcb388 6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents: 5520
diff changeset
   233
         */
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 7211
diff changeset
   234
        VARARGS("varargs");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        LintCategory(String option) {
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   237
            this(option, false);
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   238
        }
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   239
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   240
        LintCategory(String option, boolean hidden) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            this.option = option;
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   242
            this.hidden = hidden;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            map.put(option, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        static LintCategory get(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            return map.get(option);
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
168
25697c18650b 6307187: clean up code for -Xlint:options
jjg
parents: 10
diff changeset
   250
        public final String option;
3661
104c425e96aa 6873845: refine access to symbol file
jjg
parents: 1360
diff changeset
   251
        public final boolean hidden;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     * Checks if a warning category is enabled. A warning category may be enabled
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     * on the command line, or by default, and can be temporarily disabled with
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
     * the SuppressWarnings annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    public boolean isEnabled(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        return values.contains(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * Checks is a warning category has been specifically suppressed, by means
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * of the SuppressWarnings annotation, or, in the case of the deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     * category, whether it has been implicitly suppressed by virtue of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * current entity being itself deprecated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    public boolean isSuppressed(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        return suppressedValues.contains(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    protected static class AugmentVisitor implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        private final Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        private Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        private Lint parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        private Lint lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        AugmentVisitor(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            // to break an ugly sequence of initialization dependencies,
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            // we defer the initialization of syms until it is needed
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        Lint augment(Lint parent, Attribute.Compound attr) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            initSyms();
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            this.parent = parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            lint = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            attr.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            return (lint == null ? parent : lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        Lint augment(Lint parent, List<Attribute.Compound> attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
            initSyms();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            this.parent = parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            lint = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            for (Attribute.Compound a: attrs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                a.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            return (lint == null ? parent : lint);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        private void initSyms() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            if (syms == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        private void suppress(LintCategory lc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            if (lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                lint = new Lint(parent);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            lint.suppressedValues.add(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            lint.values.remove(lc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            if (value.type.tsym == syms.stringType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                LintCategory lc = LintCategory.get((String) (value.value));
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                if (lc != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    suppress(lc);
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
        public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        // If we find a @SuppressWarnings annotation, then we continue
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        // walking the tree, in order to suppress the individual warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        // specified in the @SuppressWarnings annotation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        public void visitCompound(Attribute.Compound compound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            if (compound.type.tsym == syms.suppressWarningsType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                for (List<Pair<MethodSymbol,Attribute>> v = compound.values;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                     v.nonEmpty(); v = v.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    Pair<MethodSymbol,Attribute> value = v.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                    if (value.fst.name.toString().equals("value"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                        value.snd.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        public void visitArray(Attribute.Array array) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            for (Attribute value : array.values)
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                value.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
}