langtools/test/tools/javac/T8003967/DetectMutableStaticFields.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27995 ba5b3966b883
child 30846 2b3f379840f0
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27995
diff changeset
     2
 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
14801
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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27995
diff changeset
    30
 * @modules jdk.compiler/com.sun.tools.classfile
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27995
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.util
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    32
 * @run main DetectMutableStaticFields
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    33
 */
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    34
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    35
import java.io.File;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    36
import java.io.IOException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    37
import java.net.URI;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    38
import java.net.URISyntaxException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    39
import java.util.ArrayList;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    40
import java.util.Arrays;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    41
import java.util.EnumSet;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    42
import java.util.HashMap;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    43
import java.util.List;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    44
import java.util.Map;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    45
import javax.tools.JavaCompiler;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    46
import javax.tools.JavaFileManager;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    47
import javax.tools.JavaFileObject;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    48
import javax.tools.StandardJavaFileManager;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    49
import javax.tools.StandardLocation;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    50
import javax.tools.ToolProvider;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    51
import com.sun.tools.classfile.ClassFile;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    52
import com.sun.tools.classfile.ConstantPoolException;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    53
import com.sun.tools.classfile.Descriptor;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    54
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    55
import com.sun.tools.classfile.Field;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    56
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    57
import static javax.tools.JavaFileObject.Kind.CLASS;
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_ENUM;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    59
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
    60
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
    61
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    62
public class DetectMutableStaticFields {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    63
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    64
    private static final String keyResource =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    65
            "com/sun/tools/javac/tree/JCTree.class";
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    66
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    67
    private String[] packagesToSeekFor = new String[] {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    68
        "javax.tools",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    69
        "javax.lang.model",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    70
        "com.sun.javadoc",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    71
        "com.sun.source",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    72
        "com.sun.tools.classfile",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    73
        "com.sun.tools.doclets",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    74
        "com.sun.tools.javac",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    75
        "com.sun.tools.javadoc",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    76
        "com.sun.tools.javah",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    77
        "com.sun.tools.javap",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    78
    };
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
    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
    81
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    82
    static {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    83
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    84
                put("javax/tools/ToolProvider",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    85
                    Arrays.asList("instance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    86
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    87
                put("com/sun/tools/javah/JavahTask",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    88
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    89
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    90
                put("com/sun/tools/classfile/Dependencies$DefaultFilter",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    91
                    Arrays.asList("instance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    92
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    93
                put("com/sun/tools/javap/JavapTask",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    94
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    95
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    96
                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
    97
                    Arrays.asList("docletToStart"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    98
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
    99
                put("com/sun/tools/javac/util/JCDiagnostic",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   100
                    Arrays.asList("fragmentFormatter"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   101
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   102
                put("com/sun/tools/javac/util/JavacMessages",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   103
                    Arrays.asList("defaultBundle", "defaultMessages"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   104
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   105
                put("com/sun/tools/javac/file/ZipFileIndexCache",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   106
                    Arrays.asList("sharedInstance"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   107
        classFieldsToIgnoreMap.
27995
ba5b3966b883 8067001: DetectMutableStaticFields fails after modular images push
mcimadamore
parents: 27319
diff changeset
   108
                put("com/sun/tools/javac/file/JRTIndex",
ba5b3966b883 8067001: DetectMutableStaticFields fails after modular images push
mcimadamore
parents: 27319
diff changeset
   109
                    Arrays.asList("sharedInstance"));
ba5b3966b883 8067001: DetectMutableStaticFields fails after modular images push
mcimadamore
parents: 27319
diff changeset
   110
        classFieldsToIgnoreMap.
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   111
                put("com/sun/tools/javac/main/JavaCompiler",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   112
                    Arrays.asList("versionRB"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   113
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   114
                put("com/sun/tools/javac/code/Type",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   115
                    Arrays.asList("moreInfo"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   116
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   117
                put("com/sun/tools/javac/util/SharedNameTable",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   118
                    Arrays.asList("freelist"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   119
        classFieldsToIgnoreMap.
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   120
                put("com/sun/tools/javac/util/Log",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   121
                    Arrays.asList("useRawMessages"));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   122
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   123
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   124
    private List<String> errors = new ArrayList<>();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   125
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   126
    public static void main(String[] args) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   127
        try {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   128
            new DetectMutableStaticFields().run();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   129
        } catch (Exception ex) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   130
            throw new AssertionError(
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   131
                    "Exception during test execution with cause ",
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   132
                    ex.getCause());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   133
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   134
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   135
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   136
    private void run()
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   137
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   138
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   139
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   140
            InvalidDescriptor,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   141
            URISyntaxException {
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
        URI resource = findResource(keyResource);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   144
        if (resource == null) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   145
            throw new AssertionError("Resource " + keyResource +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   146
                "not found in the class path");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   147
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   148
        analyzeResource(resource);
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   149
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   150
        if (errors.size() > 0) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   151
            for (String error: errors) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   152
                System.err.println(error);
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
            throw new AssertionError("There are mutable fields, "
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   155
                + "please check output");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   156
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   157
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   158
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   159
    URI findResource(String className) throws URISyntaxException {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   160
        URI uri = getClass().getClassLoader().getResource(className).toURI();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   161
        if (uri.getScheme().equals("jar")) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   162
            String ssp = uri.getRawSchemeSpecificPart();
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   163
            int sep = ssp.lastIndexOf("!");
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   164
            uri = new URI(ssp.substring(0, sep));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   165
        } else if (uri.getScheme().equals("file")) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   166
            uri = new URI(uri.getPath().substring(0,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   167
                    uri.getPath().length() - keyResource.length()));
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   168
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   169
        return uri;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   170
    }
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
    boolean shouldAnalyzePackage(String packageName) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   173
        for (String aPackage: packagesToSeekFor) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   174
            if (packageName.contains(aPackage)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   175
                return true;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   176
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   177
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   178
        return false;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   179
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   180
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   181
    void analyzeResource(URI resource)
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   182
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   183
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   184
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   185
            InvalidDescriptor {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   186
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   187
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   188
            JavaFileManager.Location location =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   189
                    StandardLocation.locationFor(resource.getPath());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   190
            fm.setLocation(location, com.sun.tools.javac.util.List.of(
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   191
                    new File(resource.getPath())));
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   192
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   193
            for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   194
                String className = fm.inferBinaryName(location, file);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   195
                int index = className.lastIndexOf('.');
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   196
                String pckName = index == -1 ? "" : className.substring(0, index);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   197
                if (shouldAnalyzePackage(pckName)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   198
                    analyzeClassFile(ClassFile.read(file.openInputStream()));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 14801
diff changeset
   199
                }
14801
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   200
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   201
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   202
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   203
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   204
    List<String> currentFieldsToIgnore;
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
    boolean ignoreField(String field) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   207
        if (currentFieldsToIgnore != null) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   208
            for (String fieldToIgnore : currentFieldsToIgnore) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   209
                if (field.equals(fieldToIgnore)) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   210
                    return true;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   211
                }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   212
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   213
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   214
        return false;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   215
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   216
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   217
    void analyzeClassFile(ClassFile classFileToCheck)
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   218
        throws
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   219
            IOException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   220
            ConstantPoolException,
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   221
            Descriptor.InvalidDescriptor {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   222
        boolean enumClass =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   223
                (classFileToCheck.access_flags.flags & ACC_ENUM) != 0;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   224
        boolean nonFinalStaticEnumField;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   225
        boolean nonFinalStaticField;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   226
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   227
        currentFieldsToIgnore =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   228
                classFieldsToIgnoreMap.get(classFileToCheck.getName());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   229
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   230
        for (Field field : classFileToCheck.fields) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   231
            if (ignoreField(field.getName(classFileToCheck.constant_pool))) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   232
                continue;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   233
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   234
            nonFinalStaticEnumField =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   235
                    (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
   236
            nonFinalStaticField =
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   237
                    (field.access_flags.flags & ACC_STATIC) != 0 &&
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   238
                    (field.access_flags.flags & ACC_FINAL) == 0;
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   239
            if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) {
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   240
                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
   241
                        field.getName(classFileToCheck.constant_pool) +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   242
                        ", at class " +
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   243
                        classFileToCheck.getName());
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   244
            }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   245
        }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   246
    }
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   247
d66cab4ef397 8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
diff changeset
   248
}