/
MethodCanBeStatic.java
299 lines (274 loc) · 11.1 KB
/
MethodCanBeStatic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2016 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.bugpatterns;
import static com.google.common.collect.Sets.immutableEnumSet;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.fixes.SuggestedFixes.addModifiers;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.SERIALIZATION_METHODS;
import static com.google.errorprone.util.ASTHelpers.getStartPosition;
import static java.util.Collections.disjoint;
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.NATIVE;
import static javax.lang.model.element.Modifier.SYNCHRONIZED;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher;
import com.google.errorprone.bugpatterns.CanBeStaticAnalyzer.CanBeStaticResult;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.Modifier;
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@BugPattern(
altNames = "static-method",
summary = "A private method that does not reference the enclosing instance can be static",
severity = SUGGESTION)
public class MethodCanBeStatic extends BugChecker implements CompilationUnitTreeMatcher {
private final FindingOutputStyle findingOutputStyle;
public MethodCanBeStatic(ErrorProneFlags flags) {
boolean findingPerSite = flags.getBoolean("MethodCanBeStatic:FindingPerSite").orElse(false);
this.findingOutputStyle =
findingPerSite ? FindingOutputStyle.FINDING_PER_SITE : FindingOutputStyle.ONE_FINDING;
}
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
Map<MethodSymbol, MethodDetails> nodes = new HashMap<>();
new TreePathScanner<Void, Void>() {
private int suppressions = 0;
@Override
public Void visitClass(ClassTree classTree, Void unused) {
if (isSuppressed(classTree, state)) {
suppressions++;
super.visitClass(classTree, null);
suppressions--;
} else {
super.visitClass(classTree, null);
}
return null;
}
@Override
public Void visitMethod(MethodTree tree, Void unused) {
if (isSuppressed(tree, state)) {
suppressions++;
matchMethod(tree);
super.visitMethod(tree, null);
suppressions--;
} else {
matchMethod(tree);
super.visitMethod(tree, null);
}
return null;
}
@Override
public Void visitVariable(VariableTree variableTree, Void unused) {
if (isSuppressed(variableTree, state)) {
suppressions++;
super.visitVariable(variableTree, null);
suppressions--;
} else {
super.visitVariable(variableTree, null);
}
return null;
}
private void matchMethod(MethodTree tree) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym.isStatic()) {
nodes.put(sym, new MethodDetails(tree, true, ImmutableSet.of()));
} else {
CanBeStaticResult result = CanBeStaticAnalyzer.canBeStaticResult(tree, sym, state);
boolean isExcluded = isExcluded(tree, state);
nodes.put(
sym,
new MethodDetails(
tree,
result.canPossiblyBeStatic() && !isExcluded && suppressions == 0,
result.methodsReferenced()));
}
}
}.scan(state.getPath(), null);
propagateNonStaticness(nodes);
nodes
.entrySet()
.removeIf(
entry -> entry.getValue().tree.getModifiers().getFlags().contains(Modifier.STATIC));
return generateDescription(nodes, state);
}
private static void propagateNonStaticness(Map<MethodSymbol, MethodDetails> nodes) {
for (Map.Entry<MethodSymbol, MethodDetails> entry : nodes.entrySet()) {
MethodSymbol sym = entry.getKey();
MethodDetails methodDetails = entry.getValue();
for (MethodSymbol use : methodDetails.methodsReferenced) {
if (nodes.containsKey(use)) {
nodes.get(use).referencedBy.add(sym);
}
}
if (referencesExternalMethods(methodDetails, nodes.keySet())) {
methodDetails.couldPossiblyBeStatic = false;
}
}
Set<MethodSymbol> toVisit = new HashSet<>(nodes.keySet());
while (!toVisit.isEmpty()) {
Set<MethodSymbol> nextVisit = new HashSet<>();
for (MethodSymbol sym : toVisit) {
MethodDetails methodDetails = nodes.get(sym);
if (methodDetails.couldPossiblyBeStatic) {
continue;
}
for (MethodSymbol user : methodDetails.referencedBy) {
if (!nodes.get(user).couldPossiblyBeStatic) {
continue;
}
nodes.get(user).couldPossiblyBeStatic = false;
nodes.get(user).methodsReferenced.remove(sym);
nextVisit.add(user);
}
}
toVisit = nextVisit;
}
}
private Description generateDescription(
Map<MethodSymbol, MethodDetails> nodes, VisitorState state) {
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
fixBuilder.setShortDescription("Make static");
Set<MethodTree> affectedTrees = new HashSet<>();
for (Map.Entry<MethodSymbol, MethodDetails> entry : nodes.entrySet()) {
MethodSymbol sym = entry.getKey();
MethodDetails methodDetails = entry.getValue();
boolean noExternalMethods = !referencesExternalMethods(methodDetails, nodes.keySet());
if (methodDetails.couldPossiblyBeStatic && noExternalMethods) {
addModifiers(methodDetails.tree, state, Modifier.STATIC)
.map(f -> fixQualifiers(state, sym, f))
.ifPresent(fixBuilder::merge);
affectedTrees.add(methodDetails.tree);
}
}
return findingOutputStyle.report(affectedTrees, fixBuilder.build(), state, this);
}
private static boolean referencesExternalMethods(
MethodDetails methodDetails, Set<MethodSymbol> localMethods) {
return !Sets.difference(methodDetails.methodsReferenced, localMethods).isEmpty();
}
/**
* Replace instance references to the method with static access (e.g. `this.foo(...)` ->
* `EnclosingClass.foo(...)` and `this::foo` to `EnclosingClass::foo`).
*/
private SuggestedFix fixQualifiers(VisitorState state, MethodSymbol sym, SuggestedFix f) {
SuggestedFix.Builder builder = SuggestedFix.builder().merge(f);
new TreeScanner<Void, Void>() {
@Override
public Void visitMemberSelect(MemberSelectTree tree, Void unused) {
fixQualifier(tree, tree.getExpression());
return super.visitMemberSelect(tree, unused);
}
@Override
public Void visitMemberReference(MemberReferenceTree tree, Void unused) {
fixQualifier(tree, tree.getQualifierExpression());
return super.visitMemberReference(tree, unused);
}
private void fixQualifier(Tree tree, ExpressionTree qualifierExpression) {
if (sym.equals(ASTHelpers.getSymbol(tree))) {
builder.replace(qualifierExpression, sym.owner.enclClass().getSimpleName().toString());
}
}
}.scan(state.getPath().getCompilationUnit(), null);
return builder.build();
}
private static boolean isExcluded(MethodTree tree, VisitorState state) {
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (sym.isConstructor() || !disjoint(EXCLUDED_MODIFIERS, sym.getModifiers())) {
return true;
}
if (!ASTHelpers.canBeRemoved(sym, state) || ASTHelpers.shouldKeep(tree)) {
return true;
}
switch (sym.owner.enclClass().getNestingKind()) {
case TOP_LEVEL:
break;
case MEMBER:
if (sym.owner.enclClass().hasOuterInstance()) {
return true;
}
break;
case LOCAL:
case ANONYMOUS:
return true;
}
return SERIALIZATION_METHODS.matches(tree, state);
}
private static final ImmutableSet<Modifier> EXCLUDED_MODIFIERS =
immutableEnumSet(NATIVE, SYNCHRONIZED, ABSTRACT);
/** Information about a {@link MethodSymbol} and whether it can be made static. */
private static final class MethodDetails {
private final MethodTree tree;
private boolean couldPossiblyBeStatic;
private final Set<MethodSymbol> methodsReferenced;
private final Set<MethodSymbol> referencedBy = new HashSet<>();
private MethodDetails(
MethodTree tree, boolean couldPossiblyBeStatic, Set<MethodSymbol> methodsReferenced) {
this.tree = tree;
this.couldPossiblyBeStatic = couldPossiblyBeStatic;
this.methodsReferenced = new HashSet<>(methodsReferenced);
}
}
/**
* Encapsulates how we should report findings. We support reporting a finding on either every
* affected (can be static) method, or just the first one in the file.
*/
private enum FindingOutputStyle {
ONE_FINDING {
@Override
public Description report(
Set<MethodTree> affectedTrees, SuggestedFix fix, VisitorState state, BugChecker checker) {
return affectedTrees.stream()
.min(Comparator.comparingInt(t -> getStartPosition(t)))
.map(t -> checker.describeMatch(t.getModifiers(), fix))
.orElse(NO_MATCH);
}
},
FINDING_PER_SITE {
@Override
public Description report(
Set<MethodTree> affectedTrees, SuggestedFix fix, VisitorState state, BugChecker checker) {
for (MethodTree tree : affectedTrees) {
state.reportMatch(checker.describeMatch(tree.getModifiers(), fix));
}
return NO_MATCH;
}
};
public abstract Description report(
Set<MethodTree> affectedTrees, SuggestedFix fix, VisitorState state, BugChecker checker);
}
}