Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ void method(BigDecimal a, BigDecimal b, Object o, String s) {

// Compliant
res = com.google.common.base.Objects.equal(s, "hello");
res = com.google.common.base.Objects.equal(null, s);
res = com.google.common.base.Objects.equal(s, null);
res = com.google.common.base.Objects.equal(null, a);
res = com.google.common.base.Objects.equal(a, null);
res = com.google.common.base.Objects.equal(a.setScale(2), b.setScale(2));
}

static class AccountWithGuavaEquals {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

class BigDecimalEqualsCheckSample {

void method(BigDecimal a, BigDecimal b, Object o, String s) {
private static final int SCALE = 2;

void method(BigDecimal a, BigDecimal b, Object o, String s, int runtimeScaleA, int runtimeScaleB) {
boolean res;

res = a.equals(b); // Noncompliant [["BigDecimal.equals()" compares scale as well as value; use "compareTo() == 0" for numerical comparison.]]
Expand All @@ -18,6 +20,11 @@ void method(BigDecimal a, BigDecimal b, Object o, String s) {
// ^^^^^^
res = Objects.equals(a, o); // Noncompliant
res = Objects.equals(o, a); // Noncompliant
res = a.setScale(2).equals(b); // Noncompliant
res = a.equals(b.setScale(2)); // Noncompliant
res = a.setScale(2).equals(b.setScale(3)); // Noncompliant
res = a.setScale(runtimeScaleA).equals(b.setScale(runtimeScaleB)); // Noncompliant
res = Objects.equals(a.setScale(2), b); // Noncompliant

// Compliant
res = a.compareTo(b) == 0;
Expand All @@ -26,6 +33,16 @@ void method(BigDecimal a, BigDecimal b, Object o, String s) {
res = s.equals(a);
res = s.equals("hello");
res = Objects.equals(s, "hello");
res = Objects.equals(null, s);
res = Objects.equals(s, null);
res = Objects.equals(null, a);
res = Objects.equals(a, null);
res = Objects.equals((null), s);
res = a.setScale(2).equals(b.setScale(2));
res = a.setScale(2, java.math.RoundingMode.UP).equals(b.setScale(2, java.math.RoundingMode.DOWN));
res = a.setScale(SCALE, java.math.RoundingMode.HALF_UP).equals(b.setScale(SCALE, java.math.RoundingMode.HALF_UP));
res = (a.setScale(2)).equals((b.setScale(2)));
res = Objects.equals(a.setScale(2), b.setScale(2));
}

static class Account {
Expand Down Expand Up @@ -67,6 +84,7 @@ public MyBigDecimal(String val) {

void testCustom(MyBigDecimal other) {
boolean r = this.equals(other); // Noncompliant
r = this.setScale(2).equals(other.setScale(2));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.sonar.check.Rule;
import org.sonar.java.checks.helpers.MethodTreeUtils;
import org.sonar.java.model.ExpressionUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.Arguments;
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.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
Expand All @@ -48,6 +51,14 @@ public class BigDecimalEqualsCheck extends IssuableSubscriptionVisitor {
.addParametersMatcher(JAVA_LANG_OBJECT, JAVA_LANG_OBJECT)
.build();

private static final MethodMatchers SET_SCALE = MethodMatchers.create()
.ofTypes(BIG_DECIMAL)
.names("setScale")
.addParametersMatcher("int")
.addParametersMatcher("int", "int")
.addParametersMatcher("int", "java.math.RoundingMode")
.build();
Comment thread
nathsou marked this conversation as resolved.

@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.METHOD_INVOCATION);
Expand All @@ -60,17 +71,45 @@ public void visitNode(Tree tree) {
return;
}
if (INSTANCE_EQUALS.matches(mit)) {
reportIssue(ExpressionUtils.methodName(mit), MESSAGE);
if (!hasSameKnownScale(mit)) {
reportIssue(ExpressionUtils.methodName(mit), MESSAGE);
}
} else if (STATIC_EQUALS.matches(mit)) {
Arguments arguments = mit.arguments();
Type firstType = arguments.get(0).symbolType();
Type secondType = arguments.get(1).symbolType();
if (isBigDecimal(firstType) || isBigDecimal(secondType)) {
if (!hasNullArgument(arguments)
&& (isBigDecimal(firstType) || isBigDecimal(secondType))
&& !hasSameKnownScale(arguments.get(0), arguments.get(1))) {
Comment thread
nathsou marked this conversation as resolved.
reportIssue(ExpressionUtils.methodName(mit), MESSAGE);
}
}
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.

private static boolean hasNullArgument(Arguments arguments) {
return arguments.stream().anyMatch(ExpressionUtils::isNullLiteral);
}

private static boolean hasSameKnownScale(MethodInvocationTree invocation) {
if (invocation.methodSelect() instanceof MemberSelectExpressionTree memberSelect) {
return hasSameKnownScale(memberSelect.expression(), invocation.arguments().get(0));
}
return false;
}

private static boolean hasSameKnownScale(ExpressionTree first, ExpressionTree second) {
Optional<Integer> firstScale = setScale(first);
return firstScale.isPresent() && firstScale.equals(setScale(second));
}

private static Optional<Integer> setScale(ExpressionTree expression) {
ExpressionTree unwrapped = ExpressionUtils.skipParentheses(expression);
if (unwrapped instanceof MethodInvocationTree invocation && SET_SCALE.matches(invocation.methodSymbol())) {
return invocation.arguments().get(0).asConstant(Integer.class);
}
return Optional.empty();
}

private static boolean isBigDecimal(Type type) {
return !type.isUnknown() && type.isSubtypeOf(BIG_DECIMAL);
}
Expand Down
Loading