langtools/make/tools/crules/MutableFieldsAnalyzer.java
author mcimadamore
Thu, 05 Mar 2015 13:10:49 +0000
changeset 29294 376a915b4ff0
parent 27579 d1a63c99cdd5
child 37848 3c8ff4204d2d
permissions -rw-r--r--
8073645: Add lambda-based lazy eval versions of Assert.check methods Summary: Enhance Assert so that lazy string computation can occurr where needed; enhance static roding rule checkers to make sure the right version of the method is called. Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     1
/*
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     4
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     8
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    13
 * accompanied this code).
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    14
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    18
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    21
 * questions.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    22
 */
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    23
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    24
package crules;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    25
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    26
import java.util.Arrays;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    27
import java.util.HashMap;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    28
import java.util.List;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    29
import java.util.Map;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    30
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    31
import com.sun.source.util.JavacTask;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    32
import com.sun.source.util.TaskEvent.Kind;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    33
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    34
import com.sun.tools.javac.tree.TreeScanner;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    35
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    36
import static com.sun.tools.javac.code.Flags.ENUM;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    37
import static com.sun.tools.javac.code.Flags.FINAL;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    38
import static com.sun.tools.javac.code.Flags.STATIC;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    39
import static com.sun.tools.javac.code.Flags.SYNTHETIC;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25848
diff changeset
    40
import static com.sun.tools.javac.code.Kinds.Kind.*;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    41
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    42
public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    43
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    44
    public MutableFieldsAnalyzer(JavacTask task) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    45
        super(task);
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    46
        treeVisitor = new MutableFieldsVisitor();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    47
        eventKind = Kind.ANALYZE;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    48
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    49
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    50
    private boolean ignoreField(String className, String field) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    51
        List<String> currentFieldsToIgnore =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    52
                classFieldsToIgnoreMap.get(className);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    53
        if (currentFieldsToIgnore != null) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    54
            for (String fieldToIgnore : currentFieldsToIgnore) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    55
                if (field.equals(fieldToIgnore)) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    56
                    return true;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    57
                }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    58
            }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    59
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    60
        return false;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    61
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    62
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    63
    class MutableFieldsVisitor extends TreeScanner {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    64
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    65
        @Override
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    66
        public void visitVarDef(JCVariableDecl tree) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    67
            boolean isJavacPack = tree.sym.outermostClass().fullname.toString()
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    68
                    .contains(packageToCheck);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    69
            if (isJavacPack &&
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    70
                (tree.sym.flags() & SYNTHETIC) == 0 &&
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25848
diff changeset
    71
                tree.sym.owner.kind == TYP) {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    72
                if (!ignoreField(tree.sym.owner.flatName().toString(),
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    73
                        tree.getName().toString())) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    74
                    boolean enumClass = (tree.sym.owner.flags() & ENUM) != 0;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    75
                    boolean nonFinalStaticEnumField =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    76
                            (tree.sym.flags() & (ENUM | FINAL)) == 0;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    77
                    boolean nonFinalStaticField =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    78
                            (tree.sym.flags() & STATIC) != 0 &&
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    79
                            (tree.sym.flags() & FINAL) == 0;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    80
                    if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    81
                        messages.error(tree, "crules.err.var.must.be.final", tree);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    82
                    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    83
                }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    84
            }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    85
            super.visitVarDef(tree);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    86
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    87
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    88
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    89
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    90
    private static final String packageToCheck = "com.sun.tools.javac";
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    91
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    92
    private static final Map<String, List<String>> classFieldsToIgnoreMap =
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    93
                new HashMap<>();
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    94
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    95
    static {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    96
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    97
                put("com.sun.tools.javac.util.JCDiagnostic",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    98
                    Arrays.asList("fragmentFormatter"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    99
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   100
                put("com.sun.tools.javac.util.JavacMessages",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   101
                    Arrays.asList("defaultBundle", "defaultMessages"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   102
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   103
                put("com.sun.tools.javac.file.ZipFileIndexCache",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   104
                    Arrays.asList("sharedInstance"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   105
        classFieldsToIgnoreMap.
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27224
diff changeset
   106
                put("com.sun.tools.javac.file.JRTIndex",
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27224
diff changeset
   107
                    Arrays.asList("sharedInstance"));
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27224
diff changeset
   108
        classFieldsToIgnoreMap.
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   109
                put("com.sun.tools.javac.main.JavaCompiler",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   110
                    Arrays.asList("versionRB"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   111
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   112
                put("com.sun.tools.javac.code.Type",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   113
                    Arrays.asList("moreInfo"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   114
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   115
                put("com.sun.tools.javac.util.SharedNameTable",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   116
                    Arrays.asList("freelist"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   117
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   118
                put("com.sun.tools.javac.util.Log",
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   119
                    Arrays.asList("useRawMessages"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   120
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   121
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   122
}