author | lmesnik |
Wed, 11 Sep 2019 16:25:12 -0700 | |
changeset 58093 | 50e1d346a126 |
parent 49158 | f62d1d1c2d9c |
permissions | -rw-r--r-- |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
1 |
/* |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
2 |
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
4 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
49158
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
f62d1d1c2d9c
8194296: Check copyright of files in make/langtools/tools
jjg
parents:
47216
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
10 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
11 |
* 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
|
12 |
* 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
|
13 |
* 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
|
14 |
* 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
|
15 |
* accompanied this code). |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
16 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
17 |
* 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
|
18 |
* 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
|
19 |
* 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
|
20 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
21 |
* 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
|
22 |
* 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
|
23 |
* questions. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
24 |
*/ |
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 |
package crules; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
27 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
28 |
import java.util.Arrays; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
29 |
import java.util.HashMap; |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
30 |
import java.util.HashSet; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
31 |
import java.util.List; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
32 |
import java.util.Map; |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
33 |
import java.util.Set; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
34 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
35 |
import com.sun.source.util.JavacTask; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
36 |
import com.sun.source.util.TaskEvent.Kind; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
37 |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
38 |
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
|
39 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
40 |
import static com.sun.tools.javac.code.Flags.ENUM; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
41 |
import static com.sun.tools.javac.code.Flags.FINAL; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
42 |
import static com.sun.tools.javac.code.Flags.STATIC; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
43 |
import static com.sun.tools.javac.code.Flags.SYNTHETIC; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25848
diff
changeset
|
44 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
45 |
|
38510
3f450a95c68f
8054562: document skip results in RunCodingRules.java
jlahoda
parents:
37848
diff
changeset
|
46 |
/**This analyzer guards against non-final static fields.*/ |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
47 |
public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
48 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
49 |
public MutableFieldsAnalyzer(JavacTask task) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
50 |
super(task); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
51 |
treeVisitor = new MutableFieldsVisitor(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
52 |
eventKind = Kind.ANALYZE; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
53 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
54 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
55 |
private boolean ignoreField(String className, String field) { |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
56 |
Set<String> fieldsToIgnore = classFieldsToIgnoreMap.get(className); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
57 |
return (fieldsToIgnore) != null && fieldsToIgnore.contains(field); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
58 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
59 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
60 |
class MutableFieldsVisitor extends TreeScanner { |
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 |
@Override |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
63 |
public void visitVarDef(JCVariableDecl tree) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
64 |
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
|
65 |
.contains(packageToCheck); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
66 |
if (isJavacPack && |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
67 |
(tree.sym.flags() & SYNTHETIC) == 0 && |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25848
diff
changeset
|
68 |
tree.sym.owner.kind == TYP) { |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
69 |
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
|
70 |
tree.getName().toString())) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
71 |
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
|
72 |
boolean nonFinalStaticEnumField = |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
73 |
(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
|
74 |
boolean nonFinalStaticField = |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
75 |
(tree.sym.flags() & STATIC) != 0 && |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
76 |
(tree.sym.flags() & FINAL) == 0; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
77 |
if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
78 |
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
|
79 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
80 |
} |
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 |
super.visitVarDef(tree); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
83 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
84 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
85 |
} |
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 |
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
|
88 |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
89 |
private static final Map<String, Set<String>> classFieldsToIgnoreMap = |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
90 |
new HashMap<>(); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
91 |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
92 |
private static void ignoreFields(String className, String... fieldNames) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
93 |
classFieldsToIgnoreMap.put(className, new HashSet<>(Arrays.asList(fieldNames))); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
94 |
}; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
95 |
|
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
96 |
static { |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
97 |
ignoreFields("com.sun.tools.javac.util.JCDiagnostic", "fragmentFormatter"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
98 |
ignoreFields("com.sun.tools.javac.util.JavacMessages", "defaultBundle", "defaultMessages"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
99 |
ignoreFields("com.sun.tools.javac.file.JRTIndex", "sharedInstance"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
100 |
ignoreFields("com.sun.tools.javac.main.JavaCompiler", "versionRB"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
101 |
ignoreFields("com.sun.tools.javac.code.Type", "moreInfo"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
102 |
ignoreFields("com.sun.tools.javac.util.SharedNameTable", "freelist"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
103 |
ignoreFields("com.sun.tools.javac.util.Log", "useRawMessages"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
104 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ModuleFinder", |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
105 |
"moduleFinderClass", "ofMethod"); |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
106 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Configuration", |
43767
9cff98a149cb
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42822
diff
changeset
|
107 |
"configurationClass", "resolveAndBindMethod"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
108 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Layer", |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
109 |
"layerClass", "bootMethod", "defineModulesWithOneLoaderMethod", "configurationMethod"); |
41033
49af2ecba616
8164742: ServiceConfigurationError on invoke of getServiceLoader method of StandardJavaFileManager
sadayapalam
parents:
40308
diff
changeset
|
110 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Module", |
49af2ecba616
8164742: ServiceConfigurationError on invoke of getServiceLoader method of StandardJavaFileManager
sadayapalam
parents:
40308
diff
changeset
|
111 |
"addExportsMethod", "addUsesMethod", "getModuleMethod", "getUnnamedModuleMethod"); |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41440
diff
changeset
|
112 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ModuleDescriptor$Version", |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41440
diff
changeset
|
113 |
"versionClass", "parseMethod"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
114 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ServiceLoaderHelper", |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
115 |
"loadMethod"); |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
38533
diff
changeset
|
116 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$VMHelper", |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
38533
diff
changeset
|
117 |
"vmClass", "getRuntimeArgumentsMethod"); |
41440 | 118 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$JmodFile", |
119 |
"jmodFileClass", "checkMagicMethod"); |
|
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
120 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
121 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
122 |
} |