/
CollectionInappropriateCallsCheck.java
267 lines (230 loc) · 10.6 KB
/
CollectionInappropriateCallsCheck.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
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.sonar.check.Rule;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.Symbols;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;
import static org.sonar.plugins.java.api.semantic.MethodMatchers.ANY;
@Rule(key = "S2175")
public class CollectionInappropriateCallsCheck extends IssuableSubscriptionVisitor {
private static final String JAVA_UTIL_COLLECTION = "java.util.Collection";
private static final List<TypeChecker> TYPE_CHECKERS = new TypeCheckerListBuilder()
.on(JAVA_UTIL_COLLECTION)
.method("remove").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.method("removeAll").argument(1).outOf(1).shouldMatchCollectionOfParametrizedType(1).add()
.method("contains").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.on("java.util.List")
.method("indexOf").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.method("lastIndexOf").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.on("java.util.Map")
.method("containsKey").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.method("containsValue").argument(1).outOf(1).shouldMatchParametrizedType(2).add()
.method("get").argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.method("getOrDefault").argument(1).outOf(2).shouldMatchParametrizedType(1).add()
.method("remove")
.argument(1).outOf(1).shouldMatchParametrizedType(1).add()
.argument(1).outOf(2).shouldMatchParametrizedType(1).add()
.argument(2).outOf(2).shouldMatchParametrizedType(2).add()
.build();
@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Kind.METHOD_INVOCATION);
}
@Override
public void visitNode(Tree tree) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
TYPE_CHECKERS.stream()
.filter(typeChecker -> typeChecker.methodMatcher.matches(mit))
.forEach(typeChecker -> checkMethodInvocation(mit, typeChecker));
}
private void checkMethodInvocation(MethodInvocationTree tree, TypeChecker typeChecker) {
ExpressionTree argument = tree.arguments().get(typeChecker.argumentIndex);
Type argumentTypeToCheck = argument.symbolType();
if (typeChecker.argumentIsACollection) {
argumentTypeToCheck = getTypeArgumentAt(findSuperTypeMatching(argumentTypeToCheck, JAVA_UTIL_COLLECTION), 0);
}
if (argumentTypeToCheck.isUnknown()) {
// could happen with type inference.
return;
}
Type actualMethodType = getMethodOwnerType(tree);
Type checkedMethodType = findSuperTypeMatching(actualMethodType, typeChecker.methodOwnerType);
Type parameterType = getTypeArgumentAt(checkedMethodType, typeChecker.parametrizedTypeIndex);
boolean isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(argument);
if (!isCallToParametrizedOrUnknownMethod && tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(((MemberSelectExpressionTree) tree.methodSelect()).expression());
}
if (!checkedMethodType.isUnknown()
&& !parameterType.isUnknown()
&& !isCallToParametrizedOrUnknownMethod
&& !isArgumentCompatible(argumentTypeToCheck, parameterType)) {
reportIssue(ExpressionUtils.methodName(tree), message(actualMethodType, checkedMethodType, parameterType, argumentTypeToCheck));
}
}
private static String message(Type actualMethodType, Type checkedMethodType, Type parameterType, Type argumentType) {
String actualType = typeNameWithParameters(actualMethodType);
boolean actualTypeHasTheParameterType = actualMethodType.typeArguments().stream().anyMatch(typeArg -> typeArg.equals(parameterType));
boolean checkedTypeHasSeveralParameters = checkedMethodType.typeArguments().size() > 1;
String typeDescription = checkedTypeHasSeveralParameters ? (" in a \"" + parameterType + "\" type") : "";
if (actualTypeHasTheParameterType) {
return MessageFormat.format("A \"{0}\" cannot contain a \"{1}\"{2}.", actualType, argumentType.name(), typeDescription);
}
String checkedType = typeNameWithParameters(checkedMethodType);
return MessageFormat.format("\"{0}\" is a \"{1}\" which cannot contain a \"{2}\"{3}.",
actualType, checkedType, argumentType.name(), typeDescription);
}
private static String typeNameWithParameters(Type type) {
if (type.isParameterized()) {
return type.name() + type.typeArguments().stream()
.map(Type::name)
.collect(Collectors.joining(", ", "<", ">"));
}
return type.name();
}
private static boolean isCallToParametrizedOrUnknownMethod(ExpressionTree expressionTree) {
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
Symbol.MethodSymbol symbol = ((MethodInvocationTree) expressionTree).methodSymbol();
return symbol.isUnknown() || symbol.isParametrizedMethod();
}
return false;
}
private static Type getMethodOwnerType(MethodInvocationTree mit) {
if (mit.methodSelect().is(Kind.MEMBER_SELECT)) {
return ((MemberSelectExpressionTree) mit.methodSelect()).expression().symbolType();
}
return mit.methodSymbol().owner().type();
}
private static Type getTypeArgumentAt(Type type, int index) {
if (type.isParameterized()) {
List<Type> parameters = type.typeArguments();
if (index < parameters.size()) {
return parameters.get(index);
}
}
return Symbols.unknownType;
}
private static Type findSuperTypeMatching(Type type, String genericTypeName) {
if (type.is(genericTypeName)) {
return type;
}
return type.symbol().superTypes()
.stream()
.filter(superType -> superType.is(genericTypeName))
.findFirst()
.orElse(Symbols.unknownType);
}
private static boolean isArgumentCompatible(Type argumentType, Type collectionParameterType) {
return isSubtypeOf(argumentType, collectionParameterType)
|| isSubtypeOf(collectionParameterType, argumentType)
|| autoboxing(argumentType, collectionParameterType);
}
private static boolean isSubtypeOf(Type type, Type superType) {
return type.isSubtypeOf(superType.erasure());
}
private static boolean autoboxing(Type argumentType, Type collectionParameterType) {
return argumentType.isPrimitive()
&& isSubtypeOf(argumentType.primitiveWrapperType(), collectionParameterType);
}
private static class TypeChecker {
private final String methodOwnerType;
private final MethodMatchers methodMatcher;
private final int argumentIndex;
private boolean argumentIsACollection;
private final int parametrizedTypeIndex;
private TypeChecker(String methodOwnerType, MethodMatchers methodMatcher, int argumentIndex, boolean argumentIsACollection, int parametrizedTypeIndex) {
this.methodOwnerType = methodOwnerType;
this.methodMatcher = methodMatcher;
this.argumentIndex = argumentIndex;
this.argumentIsACollection = argumentIsACollection;
this.parametrizedTypeIndex = parametrizedTypeIndex;
}
}
private static class TypeCheckerListBuilder {
private final List<TypeChecker> typeCheckers = new ArrayList<>();
private String methodOwnerType;
private String methodName;
private int argumentPosition;
private boolean argumentIsACollection;
private int argumentCount;
private int parametrizedTypePosition;
private TypeCheckerListBuilder on(String methodOwnerType) {
this.methodOwnerType = methodOwnerType;
return this;
}
private TypeCheckerListBuilder method(String methodName) {
this.methodName = methodName;
return this;
}
private TypeCheckerListBuilder argument(int argumentPosition) {
this.argumentPosition = argumentPosition;
return this;
}
private TypeCheckerListBuilder outOf(int argumentCount) {
this.argumentCount = argumentCount;
return this;
}
private TypeCheckerListBuilder shouldMatchParametrizedType(int parametrizedTypePosition) {
this.parametrizedTypePosition = parametrizedTypePosition;
this.argumentIsACollection = false;
return this;
}
private TypeCheckerListBuilder shouldMatchCollectionOfParametrizedType(int parametrizedTypePosition) {
this.parametrizedTypePosition = parametrizedTypePosition;
this.argumentIsACollection = true;
return this;
}
private TypeCheckerListBuilder add() {
int argumentIndex = argumentPosition - 1;
int parametrizedTypeIndex = parametrizedTypePosition - 1;
List<String> methodMatcherParameters = new ArrayList<>();
for (int i = 0; i < argumentCount; i++) {
String parameterType = ANY;
if (i == argumentIndex) {
parameterType = argumentIsACollection ? JAVA_UTIL_COLLECTION : "java.lang.Object";
}
methodMatcherParameters.add(parameterType);
}
MethodMatchers methodMatcher = MethodMatchers.create()
.ofSubTypes(methodOwnerType)
.names(methodName)
.addParametersMatcher(methodMatcherParameters.toArray(new String[0]))
.build();
typeCheckers.add(new TypeChecker(methodOwnerType, methodMatcher, argumentIndex, argumentIsACollection, parametrizedTypeIndex));
return this;
}
private List<TypeChecker> build() {
return typeCheckers;
}
}
}