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:
36526
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 |
|
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
28 |
import java.util.ArrayList; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
29 |
import java.util.HashMap; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
30 |
import java.util.List; |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
31 |
import java.util.Map; |
36526 | 32 |
import java.util.Optional; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
33 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
34 |
import javax.lang.model.element.TypeElement; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
35 |
import javax.tools.JavaFileObject; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
36 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
37 |
import com.sun.source.tree.Tree; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
38 |
import com.sun.source.util.JavacTask; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
39 |
import com.sun.source.util.Plugin; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
40 |
import com.sun.source.util.TaskEvent; |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
41 |
import com.sun.source.util.TaskEvent.Kind; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
42 |
import com.sun.source.util.TaskListener; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
43 |
import com.sun.source.util.Trees; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
44 |
import com.sun.tools.javac.api.BasicJavacTask; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
45 |
import com.sun.tools.javac.tree.JCTree; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
46 |
import com.sun.tools.javac.util.Context; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
47 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
48 |
import com.sun.tools.javac.util.DefinedBy.Api; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
49 |
import com.sun.tools.javac.util.Log; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
50 |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
51 |
/* |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
52 |
* This code must be run in a context that provides |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
53 |
* access to the following javac internal packages: |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
54 |
* com.sun.tools.javac.api |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
55 |
* com.sun.tools.javac.tree |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
56 |
* com.sun.tools.javac.util |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
57 |
*/ |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
58 |
public class CodingRulesAnalyzerPlugin implements Plugin { |
17283
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 |
protected Log log; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
61 |
protected Trees trees; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
62 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
63 |
@DefinedBy(Api.COMPILER_TREE) |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
64 |
public void init(JavacTask task, String... args) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
65 |
BasicJavacTask impl = (BasicJavacTask)task; |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
66 |
Context context = impl.getContext(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
67 |
log = Log.instance(context); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
68 |
trees = Trees.instance(task); |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
69 |
task.addTaskListener(new PostAnalyzeTaskListener( |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
70 |
new MutableFieldsAnalyzer(task), |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
71 |
new AssertCheckAnalyzer(task), |
45504
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44573
diff
changeset
|
72 |
new DefinedByAnalyzer(task), |
ea7475564d07
8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents:
44573
diff
changeset
|
73 |
new LegacyLogMethodAnalyzer(task) |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
74 |
)); |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
75 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
76 |
|
36526 | 77 |
private void addExports(String moduleName, String... packageNames) { |
78 |
for (String packageName : packageNames) { |
|
79 |
try { |
|
44573
245bb4e6f983
8177530: Module system implementation refresh (4/2017)
alanb
parents:
37848
diff
changeset
|
80 |
ModuleLayer layer = ModuleLayer.boot(); |
36526 | 81 |
Optional<Module> m = layer.findModule(moduleName); |
82 |
if (!m.isPresent()) |
|
83 |
throw new Error("module not found: " + moduleName); |
|
84 |
m.get().addExports(packageName, getClass().getModule()); |
|
85 |
} catch (Exception e) { |
|
86 |
throw new Error("failed to add exports for " + moduleName + "/" + packageName); |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
||
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
91 |
public class PostAnalyzeTaskListener implements TaskListener { |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
92 |
private final Map<Kind, List<AbstractCodingRulesAnalyzer>> analyzers = new HashMap<>(); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
93 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
94 |
public PostAnalyzeTaskListener(AbstractCodingRulesAnalyzer... analyzers) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
95 |
for (AbstractCodingRulesAnalyzer analyzer : analyzers) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
96 |
List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(analyzer.eventKind); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
97 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
98 |
if (currentAnalyzers == null) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
99 |
this.analyzers.put(analyzer.eventKind, currentAnalyzers = new ArrayList<>()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
100 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
101 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
102 |
currentAnalyzers.add(analyzer); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
103 |
} |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
104 |
} |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
105 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
106 |
@Override @DefinedBy(Api.COMPILER_TREE) |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
107 |
public void started(TaskEvent taskEvent) {} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
108 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
109 |
@Override @DefinedBy(Api.COMPILER_TREE) |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
110 |
public void finished(TaskEvent taskEvent) { |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
111 |
List<AbstractCodingRulesAnalyzer> currentAnalyzers = this.analyzers.get(taskEvent.getKind()); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
112 |
|
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
113 |
if (currentAnalyzers != null) { |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
114 |
TypeElement typeElem = taskEvent.getTypeElement(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
115 |
Tree tree = trees.getTree(typeElem); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
116 |
if (tree != null) { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
117 |
JavaFileObject prevSource = log.currentSourceFile(); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
118 |
try { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
119 |
log.useSource(taskEvent.getCompilationUnit().getSourceFile()); |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
120 |
for (AbstractCodingRulesAnalyzer analyzer : currentAnalyzers) { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
121 |
analyzer.treeVisitor.scan((JCTree)tree); |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
122 |
} |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
123 |
} finally { |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
124 |
log.useSource(prevSource); |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
125 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
126 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
127 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
128 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
129 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
130 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25848
diff
changeset
|
131 |
@Override @DefinedBy(Api.COMPILER_TREE) |
25848
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
132 |
public String getName() { |
3bc09f4676a9
8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents:
17283
diff
changeset
|
133 |
return "coding_rules"; |
17283
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
134 |
} |
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
135 |
|
cddfdf252f4a
8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff
changeset
|
136 |
} |