/
ImmutableSetForContains.java
246 lines (226 loc) · 10.4 KB
/
ImmutableSetForContains.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
/*
* 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.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.hasAnnotationWithSimpleName;
import static com.google.errorprone.matchers.Matchers.hasModifier;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.isSameType;
import static com.google.errorprone.matchers.Matchers.isStatic;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.shouldKeep;
import static com.google.errorprone.util.ASTHelpers.streamReceivers;
import static java.util.stream.Collectors.toMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
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.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import javax.lang.model.element.Modifier;
/**
* Refactoring to suggest using {@code private static final} {@link ImmutableSet} over {@link
* ImmutableList} when using only contains, containsAll and isEmpty.
*/
@BugPattern(
summary =
"This private static ImmutableList is only used for contains, containsAll or isEmpty"
+ " checks; prefer ImmutableSet.",
severity = SUGGESTION)
public final class ImmutableSetForContains extends BugChecker implements ClassTreeMatcher {
private static final Matcher<Tree> PRIVATE_STATIC_IMMUTABLE_LIST_MATCHER =
allOf(
isStatic(),
hasModifier(Modifier.PRIVATE),
hasModifier(Modifier.FINAL),
isSameType(ImmutableList.class));
private static final Matcher<Tree> EXCLUSIONS =
anyOf(
(t, s) -> shouldKeep(t),
hasAnnotationWithSimpleName("Bind"),
hasAnnotationWithSimpleName("Inject"));
private static final Matcher<ExpressionTree> IMMUTABLE_LIST_FACTORIES =
staticMethod().onClass(ImmutableList.class.getName()).namedAnyOf("of", "copyOf");
private static final Matcher<ExpressionTree> IMMUTABLE_LIST_BUILD =
instanceMethod().onExactClass(ImmutableList.Builder.class.getName()).namedAnyOf("build");
private static final Matcher<ExpressionTree> IMMUTABLE_COLLECTION =
instanceMethod().onExactClass(Stream.class.getName()).named("collect");
private static final Matcher<ExpressionTree> IMMUTABLE_BUILDER_METHODS =
instanceMethod()
.onExactClass(ImmutableList.Builder.class.getName())
.namedAnyOf("add", "addAll");
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
ImmutableSet<VariableTree> immutableListVar =
tree.getMembers().stream()
.filter(member -> member.getKind().equals(Kind.VARIABLE))
.map(VariableTree.class::cast)
// TODO(ashishkedia) : Expand to non-static vars with simple init in constructors.
.filter(
member ->
PRIVATE_STATIC_IMMUTABLE_LIST_MATCHER.matches(member, state)
&& !EXCLUSIONS.matches(member, state))
.collect(toImmutableSet());
if (immutableListVar.isEmpty()) {
return Description.NO_MATCH;
}
ImmutableVarUsageScanner usageScanner = new ImmutableVarUsageScanner(immutableListVar);
// Scan entire compilation unit since private static vars of nested classes may be referred in
// parent class.
TreePath cuPath = state.findPathToEnclosing(CompilationUnitTree.class);
usageScanner.scan(cuPath.getLeaf(), state.withPath(cuPath));
SuggestedFix.Builder fix = SuggestedFix.builder();
Optional<VariableTree> firstReplacement = Optional.empty();
for (VariableTree var : immutableListVar) {
if (isSuppressed(var, state)) {
continue;
}
if (!usageScanner.disallowedVarUsages.get(getSymbol(var))) {
firstReplacement = Optional.of(var);
fix.merge(convertListToSetInit(var, state));
}
}
if (!firstReplacement.isPresent()) {
return Description.NO_MATCH;
}
return describeMatch(firstReplacement.get(), fix.build());
}
private static SuggestedFix convertListToSetInit(VariableTree var, VisitorState state) {
SuggestedFix.Builder fix =
SuggestedFix.builder()
.addImport(ImmutableSet.class.getName())
.replace(stripParameters(var.getType()), "ImmutableSet");
if (IMMUTABLE_LIST_FACTORIES.matches(var.getInitializer(), state)) {
fix.replace(getReceiver(var.getInitializer()), "ImmutableSet");
return fix.build();
}
if (IMMUTABLE_COLLECTION.matches(var.getInitializer(), state)) {
fix.addStaticImport("com.google.common.collect.ImmutableSet.toImmutableSet")
.replace(
getOnlyElement(((MethodInvocationTree) var.getInitializer()).getArguments()),
"toImmutableSet()");
return fix.build();
}
if (IMMUTABLE_LIST_BUILD.matches(var.getInitializer(), state)) {
Optional<ExpressionTree> rootExpr =
getRootMethod((MethodInvocationTree) var.getInitializer(), state);
if (rootExpr.isPresent()) {
if (rootExpr.get().getKind().equals(Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) rootExpr.get();
fix.replace(getReceiver(methodTree), "ImmutableSet");
return fix.build();
}
if (rootExpr.get().getKind().equals(Kind.NEW_CLASS)) {
NewClassTree ctorTree = (NewClassTree) rootExpr.get();
fix.replace(stripParameters(ctorTree.getIdentifier()), "ImmutableSet.Builder");
}
return fix.build();
}
}
return fix.replace(
var.getInitializer(),
"ImmutableSet.copyOf(" + state.getSourceForNode(var.getInitializer()) + ")")
.build();
}
private static Tree stripParameters(Tree tree) {
return tree.getKind().equals(Kind.PARAMETERIZED_TYPE)
? ((ParameterizedTypeTree) tree).getType()
: tree;
}
private static Optional<ExpressionTree> getRootMethod(
MethodInvocationTree methodInvocationTree, VisitorState state) {
return streamReceivers(methodInvocationTree)
.filter(t -> !IMMUTABLE_BUILDER_METHODS.matches(t, state))
.findFirst();
}
// Scans the tree for all usages of immutable list vars and determines if any usage will prevent
// us from turning ImmutableList into ImmutableSet.
private static final class ImmutableVarUsageScanner extends TreeScanner<Void, VisitorState> {
private static final Matcher<ExpressionTree> ALLOWED_FUNCTIONS_ON_LIST =
instanceMethod()
.onExactClass(ImmutableList.class.getName())
.namedAnyOf("contains", "containsAll", "isEmpty");
// For each ImmutableList usage var, we will keep a map indicating if the var has been used in a
// way that would prevent us from making it a set.
private final Map<Symbol, Boolean> disallowedVarUsages;
private ImmutableVarUsageScanner(ImmutableSet<VariableTree> immutableListVar) {
this.disallowedVarUsages =
immutableListVar.stream()
.map(ASTHelpers::getSymbol)
.collect(toMap(x -> x, x -> Boolean.FALSE));
}
@Override
public Void visitMethodInvocation(
MethodInvocationTree methodInvocationTree, VisitorState state) {
methodInvocationTree.getArguments().forEach(tree -> scan(tree, state));
methodInvocationTree.getTypeArguments().forEach(tree -> scan(tree, state));
if (!allowedFuncOnImmutableVar(methodInvocationTree, state)) {
scan(methodInvocationTree.getMethodSelect(), state);
}
return null;
}
private boolean allowedFuncOnImmutableVar(MethodInvocationTree methodTree, VisitorState state) {
ExpressionTree receiver = getReceiver(methodTree);
if (receiver == null) {
return false;
}
if (!disallowedVarUsages.containsKey(getSymbol(receiver))) {
// Not a function invocation on any of the candidate immutable list vars.
return false;
}
return ALLOWED_FUNCTIONS_ON_LIST.matches(methodTree, state);
}
@Override
public Void visitIdentifier(IdentifierTree identifierTree, VisitorState visitorState) {
recordDisallowedUsage(getSymbol(identifierTree));
return super.visitIdentifier(identifierTree, visitorState);
}
@Override
public Void visitMemberSelect(MemberSelectTree memberSelectTree, VisitorState visitorState) {
recordDisallowedUsage(getSymbol(memberSelectTree));
return super.visitMemberSelect(memberSelectTree, visitorState);
}
private void recordDisallowedUsage(Symbol symbol) {
disallowedVarUsages.computeIfPresent(symbol, (sym, oldVal) -> Boolean.TRUE);
}
}
}