langtools/test/tools/javac/T8003967/DetectMutableStaticFields.java
author vromero
Mon, 10 Dec 2012 16:21:26 +0000
changeset 14801 d66cab4ef397
child 27319 030080f03e4f
permissions -rw-r--r--
8003967: detect and remove all mutable implicit static enum fields in langtools Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     1
/*
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     2
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     4
 *
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    10
 *
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    15
 * accompanied this code).
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    16
 *
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    20
 *
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    23
 * questions.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    24
 */
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    25
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    26
/*
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    27
 * @test
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    28
 * @bug 8003967
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    29
 * @summary detect and remove all mutable implicit static enum fields in langtools
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    30
 * @run main DetectMutableStaticFields
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    31
 */
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    32
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    33
import java.io.File;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    34
import java.io.IOException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    35
import java.net.URI;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    36
import java.net.URISyntaxException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    37
import java.util.ArrayList;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    38
import java.util.Arrays;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    39
import java.util.EnumSet;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    40
import java.util.HashMap;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    41
import java.util.List;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    42
import java.util.Map;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    43
import javax.tools.JavaCompiler;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    44
import javax.tools.JavaFileManager;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    45
import javax.tools.JavaFileObject;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    46
import javax.tools.StandardJavaFileManager;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    47
import javax.tools.StandardLocation;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    48
import javax.tools.ToolProvider;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    49
import com.sun.tools.classfile.ClassFile;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    50
import com.sun.tools.classfile.ConstantPoolException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    51
import com.sun.tools.classfile.Descriptor;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    52
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    53
import com.sun.tools.classfile.Field;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    54
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    55
import static javax.tools.JavaFileObject.Kind.CLASS;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    56
import static com.sun.tools.classfile.AccessFlags.ACC_ENUM;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    57
import static com.sun.tools.classfile.AccessFlags.ACC_FINAL;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    58
import static com.sun.tools.classfile.AccessFlags.ACC_STATIC;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    59
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    60
public class DetectMutableStaticFields {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    61
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    62
    private static final String keyResource =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    63
            "com/sun/tools/javac/tree/JCTree.class";
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    64
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    65
    private String[] packagesToSeekFor = new String[] {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    66
        "javax.tools",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    67
        "javax.lang.model",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    68
        "com.sun.javadoc",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    69
        "com.sun.source",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    70
        "com.sun.tools.classfile",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    71
        "com.sun.tools.doclets",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    72
        "com.sun.tools.javac",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    73
        "com.sun.tools.javadoc",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    74
        "com.sun.tools.javah",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    75
        "com.sun.tools.javap",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    76
    };
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    77
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    78
    private static final Map<String, List<String>> classFieldsToIgnoreMap = new HashMap<>();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    79
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    80
    static {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    81
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    82
                put("javax/tools/ToolProvider",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    83
                    Arrays.asList("instance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    84
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    85
                put("com/sun/tools/javah/JavahTask",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    86
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    87
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    88
                put("com/sun/tools/classfile/Dependencies$DefaultFilter",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    89
                    Arrays.asList("instance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    90
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    91
                put("com/sun/tools/javap/JavapTask",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    92
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    93
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    94
                put("com/sun/tools/doclets/formats/html/HtmlDoclet",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    95
                    Arrays.asList("docletToStart"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    96
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    97
                put("com/sun/tools/javac/util/JCDiagnostic",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    98
                    Arrays.asList("fragmentFormatter"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    99
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   100
                put("com/sun/tools/javac/util/JavacMessages",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   101
                    Arrays.asList("defaultBundle", "defaultMessages"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   102
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   103
                put("com/sun/tools/javac/file/ZipFileIndexCache",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   104
                    Arrays.asList("sharedInstance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   105
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   106
                put("com/sun/tools/javac/main/JavaCompiler",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   107
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   108
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   109
                put("com/sun/tools/javac/code/Type",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   110
                    Arrays.asList("moreInfo"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   111
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   112
                put("com/sun/tools/javac/util/SharedNameTable",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   113
                    Arrays.asList("freelist"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   114
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   115
                put("com/sun/tools/javac/util/Log",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   116
                    Arrays.asList("useRawMessages"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   117
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   118
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   119
    private List<String> errors = new ArrayList<>();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   120
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   121
    public static void main(String[] args) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   122
        try {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   123
            new DetectMutableStaticFields().run();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   124
        } catch (Exception ex) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   125
            throw new AssertionError(
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   126
                    "Exception during test execution with cause ",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   127
                    ex.getCause());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   128
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   129
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   130
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   131
    private void run()
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   132
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   133
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   134
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   135
            InvalidDescriptor,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   136
            URISyntaxException {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   137
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   138
        URI resource = findResource(keyResource);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   139
        if (resource == null) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   140
            throw new AssertionError("Resource " + keyResource +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   141
                "not found in the class path");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   142
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   143
        analyzeResource(resource);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   144
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   145
        if (errors.size() > 0) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   146
            for (String error: errors) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   147
                System.err.println(error);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   148
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   149
            throw new AssertionError("There are mutable fields, "
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   150
                + "please check output");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   151
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   152
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   153
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   154
    URI findResource(String className) throws URISyntaxException {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   155
        URI uri = getClass().getClassLoader().getResource(className).toURI();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   156
        if (uri.getScheme().equals("jar")) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   157
            String ssp = uri.getRawSchemeSpecificPart();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   158
            int sep = ssp.lastIndexOf("!");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   159
            uri = new URI(ssp.substring(0, sep));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   160
        } else if (uri.getScheme().equals("file")) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   161
            uri = new URI(uri.getPath().substring(0,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   162
                    uri.getPath().length() - keyResource.length()));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   163
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   164
        return uri;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   165
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   166
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   167
    boolean shouldAnalyzePackage(String packageName) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   168
        for (String aPackage: packagesToSeekFor) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   169
            if (packageName.contains(aPackage)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   170
                return true;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   171
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   172
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   173
        return false;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   174
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   175
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   176
    void analyzeResource(URI resource)
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   177
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   178
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   179
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   180
            InvalidDescriptor {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   181
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   182
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   183
        JavaFileManager.Location location =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   184
                StandardLocation.locationFor(resource.getPath());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   185
        fm.setLocation(location, com.sun.tools.javac.util.List.of(
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   186
                new File(resource.getPath())));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   187
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   188
        for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   189
            String className = fm.inferBinaryName(location, file);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   190
            int index = className.lastIndexOf('.');
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   191
            String pckName = index == -1 ? "" : className.substring(0, index);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   192
            if (shouldAnalyzePackage(pckName)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   193
                analyzeClassFile(ClassFile.read(file.openInputStream()));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   194
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   195
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   196
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   197
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   198
    List<String> currentFieldsToIgnore;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   199
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   200
    boolean ignoreField(String field) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   201
        if (currentFieldsToIgnore != null) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   202
            for (String fieldToIgnore : currentFieldsToIgnore) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   203
                if (field.equals(fieldToIgnore)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   204
                    return true;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   205
                }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   206
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   207
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   208
        return false;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   209
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   210
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   211
    void analyzeClassFile(ClassFile classFileToCheck)
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   212
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   213
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   214
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   215
            Descriptor.InvalidDescriptor {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   216
        boolean enumClass =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   217
                (classFileToCheck.access_flags.flags & ACC_ENUM) != 0;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   218
        boolean nonFinalStaticEnumField;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   219
        boolean nonFinalStaticField;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   220
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   221
        currentFieldsToIgnore =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   222
                classFieldsToIgnoreMap.get(classFileToCheck.getName());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   223
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   224
        for (Field field : classFileToCheck.fields) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   225
            if (ignoreField(field.getName(classFileToCheck.constant_pool))) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   226
                continue;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   227
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   228
            nonFinalStaticEnumField =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   229
                    (field.access_flags.flags & (ACC_ENUM | ACC_FINAL)) == 0;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   230
            nonFinalStaticField =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   231
                    (field.access_flags.flags & ACC_STATIC) != 0 &&
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   232
                    (field.access_flags.flags & ACC_FINAL) == 0;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   233
            if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   234
                errors.add("There is a mutable field named " +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   235
                        field.getName(classFileToCheck.constant_pool) +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   236
                        ", at class " +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   237
                        classFileToCheck.getName());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   238
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   239
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   240
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   241
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   242
}