-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathCollectionIsEmptyCheck.java
More file actions
202 lines (179 loc) · 8.08 KB
/
Copy pathCollectionIsEmptyCheck.java
File metadata and controls
202 lines (179 loc) · 8.08 KB
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
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.java.model.LiteralUtils;
import org.sonar.java.reporting.AnalyzerMessage;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.BinaryExpressionTree;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonarsource.analyzer.commons.collections.ListUtils;
import static org.sonar.java.reporting.AnalyzerMessage.textSpanBetween;
@Rule(key = "S1155")
public class CollectionIsEmptyCheck extends IssuableSubscriptionVisitor {
private enum EmptyComparisonType {
EMPTY, NOT_EMPTY
}
private static final String JAVA_UTIL_COLLECTION = "java.util.Collection";
private static final MethodMatchers SIZE_METHOD = MethodMatchers.create()
.ofSubTypes(JAVA_UTIL_COLLECTION)
.names("size")
.addWithoutParametersMatcher()
.build();
private static final Tree.Kind[] TARGETED_BINARY_OPERATOR_TREES = {
Tree.Kind.EQUAL_TO,
Tree.Kind.NOT_EQUAL_TO,
Tree.Kind.LESS_THAN,
Tree.Kind.LESS_THAN_OR_EQUAL_TO,
Tree.Kind.GREATER_THAN,
Tree.Kind.GREATER_THAN_OR_EQUAL_TO
};
private static final Tree.Kind[] CLASS_TREES = Tree.Kind.CLASS_KINDS.toArray(new Tree.Kind[0]);
private static final Deque<Boolean> IS_COLLECTION_ENCLOSING_TYPES_STACK = new LinkedList<>();
@Override
public List<Tree.Kind> nodesToVisit() {
return ListUtils.concat(Arrays.asList(CLASS_TREES), Arrays.asList(TARGETED_BINARY_OPERATOR_TREES));
}
@Override
public void leaveFile(JavaFileScannerContext context) {
// in case of polluted state
IS_COLLECTION_ENCLOSING_TYPES_STACK.clear();
}
@Override
public void visitNode(Tree tree) {
if (tree.is(CLASS_TREES)) {
handleClassTree((ClassTree) tree);
} else {
// Necessarily a BinaryExpressionTree - the rule only raises issues on size() comparisons
// are "poorly" used from something which is NOT a Collection
handleBinaryExpressionTree((BinaryExpressionTree) tree);
}
}
private static void handleClassTree(ClassTree tree) {
Symbol.TypeSymbol symbol = tree.symbol();
boolean isCollection = symbol.type().isSubtypeOf(JAVA_UTIL_COLLECTION);
if (isInnerClassOfCollection(symbol)) {
// inner classes which might be related to its parent collection
isCollection = true;
}
IS_COLLECTION_ENCLOSING_TYPES_STACK.push(isCollection);
}
private static boolean isInnerClassOfCollection(Symbol.TypeSymbol symbol) {
return Boolean.TRUE.equals(IS_COLLECTION_ENCLOSING_TYPES_STACK.peek()) && !symbol.isStatic();
}
private void handleBinaryExpressionTree(BinaryExpressionTree tree) {
if (isInCollectionType()) {
return;
}
getCallToSizeInvocation(tree).ifPresent(callToSizeInvocation ->
getEmptyComparisonType(tree).ifPresent(comparisonType -> QuickFixHelper.newIssue(context)
.forRule(this)
.onTree(tree)
.withMessage("Use isEmpty() to check whether the collection is empty or not.")
.withQuickFix(() -> getQuickFix(tree, callToSizeInvocation, comparisonType))
.report()));
}
private static boolean isInCollectionType() {
return Boolean.TRUE.equals(IS_COLLECTION_ENCLOSING_TYPES_STACK.peek());
}
@Override
public void leaveNode(Tree tree) {
if (tree.is(CLASS_TREES)) {
IS_COLLECTION_ENCLOSING_TYPES_STACK.pop();
}
}
private static JavaQuickFix getQuickFix(BinaryExpressionTree tree, MethodInvocationTree callToSizeInvocation, EmptyComparisonType emptyComparisonType) {
IdentifierTree sizeCallIdentifier = ExpressionUtils.methodName(callToSizeInvocation);
// We want to keep the object on which "size" is called, we therefore replace everything before with ! (if needed) and after with "isEmpty()".
JavaQuickFix.Builder builder = JavaQuickFix.newQuickFix("Use \"isEmpty()\"");
AnalyzerMessage.TextSpan textSpan = textSpanBetween(tree.firstToken(), true, callToSizeInvocation, false);
String replacement = emptyComparisonType == EmptyComparisonType.EMPTY ? "" : "!";
if (!(textSpan.isEmpty() && replacement.isEmpty())) {
builder.addTextEdit(JavaTextEdit.replaceTextSpan(textSpan, replacement));
}
builder.addTextEdit(JavaTextEdit.replaceTextSpan(textSpanBetween(sizeCallIdentifier, true, tree.lastToken(), true), "isEmpty()"));
return builder.build();
}
private static Optional<MethodInvocationTree> getCallToSizeInvocation(BinaryExpressionTree tree) {
return getCallToSizeInvocation(tree.leftOperand())
// we stop at the first match
.or(() -> getCallToSizeInvocation(tree.rightOperand()));
}
private static Optional<MethodInvocationTree> getCallToSizeInvocation(ExpressionTree tree) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree invocationTree = (MethodInvocationTree) tree;
if (SIZE_METHOD.matches(invocationTree)) {
return Optional.of(invocationTree);
}
}
return Optional.empty();
}
private static Optional<EmptyComparisonType> getEmptyComparisonType(BinaryExpressionTree tree) {
boolean leftIsZero = LiteralUtils.isZero(tree.leftOperand());
boolean leftIsOne = LiteralUtils.isOne(tree.leftOperand());
boolean rightIsZero = LiteralUtils.isZero(tree.rightOperand());
boolean rightIsOne = LiteralUtils.isOne(tree.rightOperand());
boolean anyZero = leftIsZero || rightIsZero;
if (isEmptyComparison(tree, leftIsZero, leftIsOne, rightIsZero, rightIsOne, anyZero)) {
return Optional.of(EmptyComparisonType.EMPTY);
}
if (isNotEmptyComparison(tree, leftIsZero, leftIsOne, rightIsZero, rightIsOne, anyZero)) {
return Optional.of(EmptyComparisonType.NOT_EMPTY);
}
return Optional.empty();
}
private static boolean isEmptyComparison(BinaryExpressionTree tree, boolean leftIsZero, boolean leftIsOne, boolean rightIsZero, boolean rightIsOne, boolean anyZero) {
// size == 0, 0 == size
return (tree.is(Tree.Kind.EQUAL_TO) && anyZero)
// size > 1
|| (tree.is(Tree.Kind.LESS_THAN) && rightIsOne)
// size <= 0
|| (tree.is(Tree.Kind.LESS_THAN_OR_EQUAL_TO) && rightIsZero)
// 1 > size
|| (tree.is(Tree.Kind.GREATER_THAN) && leftIsOne)
// 0 >= size
|| (tree.is(Tree.Kind.GREATER_THAN_OR_EQUAL_TO) && leftIsZero);
}
private static boolean isNotEmptyComparison(BinaryExpressionTree tree, boolean leftIsZero, boolean leftIsOne, boolean rightIsZero, boolean rightIsOne, boolean anyZero) {
// size != 0, 0 != size
return (tree.is(Tree.Kind.NOT_EQUAL_TO) && anyZero)
// size > 0
|| (tree.is(Tree.Kind.GREATER_THAN) && rightIsZero)
// size >= 1
|| (tree.is(Tree.Kind.GREATER_THAN_OR_EQUAL_TO) && rightIsOne)
// 0 < size
|| (tree.is(Tree.Kind.LESS_THAN) && leftIsZero)
// 1 <= size
|| (tree.is(Tree.Kind.LESS_THAN_OR_EQUAL_TO) && leftIsOne);
}
}