author | mgronlun |
Tue, 02 Aug 2016 18:36:47 +0200 | |
changeset 40203 | 6ecf77479dcf |
parent 38533 | 2bd347dde526 |
child 40308 | 274367a99f98 |
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 |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
8 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
13 |
* accompanied this code). |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
14 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
18 |
* |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
21 |
* questions. |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
22 |
*/ |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
23 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
24 |
package crules; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
25 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
26 |
import java.util.Arrays; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
27 |
import java.util.HashMap; |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
28 |
import java.util.HashSet; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
29 |
import java.util.List; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
30 |
import java.util.Map; |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
31 |
import java.util.Set; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
32 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
33 |
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
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
38 |
import static com.sun.tools.javac.code.Flags.ENUM; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
39 |
import static com.sun.tools.javac.code.Flags.FINAL; |
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.STATIC; |
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.SYNTHETIC; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25848
diff
changeset
|
42 |
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
|
43 |
|
38510
3f450a95c68f
8054562: document skip results in RunCodingRules.java
jlahoda
parents:
37848
diff
changeset
|
44 |
/**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
|
45 |
public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
46 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
47 |
public MutableFieldsAnalyzer(JavacTask task) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
48 |
super(task); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
49 |
treeVisitor = new MutableFieldsVisitor(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
50 |
eventKind = Kind.ANALYZE; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
51 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
52 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
53 |
private boolean ignoreField(String className, String field) { |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
54 |
Set<String> fieldsToIgnore = classFieldsToIgnoreMap.get(className); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
55 |
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
|
56 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
57 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
58 |
class MutableFieldsVisitor extends TreeScanner { |
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 |
@Override |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
61 |
public void visitVarDef(JCVariableDecl tree) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
62 |
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
|
63 |
.contains(packageToCheck); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
64 |
if (isJavacPack && |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
65 |
(tree.sym.flags() & SYNTHETIC) == 0 && |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25848
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
tree.getName().toString())) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
69 |
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
|
70 |
boolean nonFinalStaticEnumField = |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
71 |
(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
|
72 |
boolean nonFinalStaticField = |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
73 |
(tree.sym.flags() & STATIC) != 0 && |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
74 |
(tree.sym.flags() & FINAL) == 0; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
75 |
if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
76 |
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
|
77 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
78 |
} |
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 |
super.visitVarDef(tree); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
81 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
82 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
83 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
84 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
85 |
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
|
86 |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
87 |
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
|
88 |
new HashMap<>(); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
89 |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
90 |
private static void ignoreFields(String className, String... fieldNames) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
91 |
classFieldsToIgnoreMap.put(className, new HashSet<>(Arrays.asList(fieldNames))); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
92 |
}; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
93 |
|
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
94 |
static { |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
95 |
ignoreFields("com.sun.tools.javac.util.JCDiagnostic", "fragmentFormatter"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
96 |
ignoreFields("com.sun.tools.javac.util.JavacMessages", "defaultBundle", "defaultMessages"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
97 |
ignoreFields("com.sun.tools.javac.file.JRTIndex", "sharedInstance"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
98 |
ignoreFields("com.sun.tools.javac.main.JavaCompiler", "versionRB"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
99 |
ignoreFields("com.sun.tools.javac.code.Type", "moreInfo"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
100 |
ignoreFields("com.sun.tools.javac.util.SharedNameTable", "freelist"); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
101 |
ignoreFields("com.sun.tools.javac.util.Log", "useRawMessages"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
102 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ModuleFinder", |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
103 |
"moduleFinderClass", "ofMethod"); |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
104 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Configuration", |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
105 |
"configurationClass", "resolveRequiresAndUsesMethod"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
106 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$Layer", |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
107 |
"layerClass", "bootMethod", "defineModulesWithOneLoaderMethod", "configurationMethod"); |
38533
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
108 |
ignoreFields("com.sun.tools.javac.util.JDK9Wrappers$ServiceLoaderHelper", |
2bd347dde526
8157474: clean up/simplify/rename ModuleWrappers class
jjg
parents:
38510
diff
changeset
|
109 |
"loadMethod"); |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
110 |
ignoreFields("com.sun.tools.javac.util.ModuleHelper", |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
27579
diff
changeset
|
111 |
"addExportsMethod", "getUnnamedModuleMethod", "getModuleMethod"); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
112 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
113 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
114 |
} |