langtools/make/tools/crules/MutableFieldsAnalyzer.java
author chegar
Tue, 14 Jan 2014 15:15:45 +0000
changeset 22275 de7efc0bb1a5
parent 17283 cddfdf252f4a
child 25848 3bc09f4676a9
permissions -rw-r--r--
8031708: Windows x86 build failure: JNU_ThrowOutOfMemoryError undefined Reviewed-by: alanb
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
/*
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    31
import com.sun.tools.javac.code.Kinds;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    32
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
    33
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    34
import static com.sun.source.util.TaskEvent.Kind;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    35
import static com.sun.tools.javac.code.Flags.*;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    36
import static com.sun.tools.javac.tree.JCTree.JCVariableDecl;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    37
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    38
public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    39
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    40
    public MutableFieldsAnalyzer() {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    41
        treeVisitor = new MutableFieldsVisitor();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    42
        eventKind = Kind.ANALYZE;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    43
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    44
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    45
    public String getName() {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    46
        return "mutable_fields_analyzer";
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    47
    }
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
    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
    50
        List<String> currentFieldsToIgnore =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    51
                classFieldsToIgnoreMap.get(className);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    52
        if (currentFieldsToIgnore != null) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    53
            for (String fieldToIgnore : currentFieldsToIgnore) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    54
                if (field.equals(fieldToIgnore)) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    55
                    return true;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    56
                }
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
        return false;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    60
    }
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
    class MutableFieldsVisitor extends TreeScanner {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    63
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    64
        @Override
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    65
        public void visitVarDef(JCVariableDecl tree) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    66
            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
    67
                    .contains(packageToCheck);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    68
            if (isJavacPack &&
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    69
                (tree.sym.flags() & SYNTHETIC) == 0 &&
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    70
                tree.sym.owner.kind == Kinds.TYP) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    71
                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
    72
                        tree.getName().toString())) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    73
                    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
    74
                    boolean nonFinalStaticEnumField =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    75
                            (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
    76
                    boolean nonFinalStaticField =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    77
                            (tree.sym.flags() & STATIC) != 0 &&
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    78
                            (tree.sym.flags() & FINAL) == 0;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    79
                    if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    80
                        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
    81
                    }
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
            super.visitVarDef(tree);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    85
        }
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
    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
    90
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    91
    private static final Map<String, List<String>> classFieldsToIgnoreMap =
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    92
                new HashMap<String, List<String>>();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    93
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    94
    static {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    95
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    96
                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
    97
                    Arrays.asList("fragmentFormatter"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    98
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    99
                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
   100
                    Arrays.asList("defaultBundle", "defaultMessages"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   101
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   102
                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
   103
                    Arrays.asList("sharedInstance"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   104
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   105
                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
   106
                    Arrays.asList("versionRB"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   107
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   108
                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
   109
                    Arrays.asList("moreInfo"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   110
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   111
                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
   112
                    Arrays.asList("freelist"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   113
        classFieldsToIgnoreMap.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   114
                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
   115
                    Arrays.asList("useRawMessages"));
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   116
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   117
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   118
}