Skip to content

Commit defc8ee

Browse files
quentin-jaquier-sonarsourceWohops
authored andcommitted
SONARJAVA-3455 FN in S2111 for boxed Double and Float
1 parent 924c63d commit defc8ee

2 files changed

Lines changed: 38 additions & 17 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
package checks;
22

33
import java.math.BigDecimal;
4+
import java.math.BigInteger;
45
import java.math.MathContext;
56

67
class BigDecimalDoubleConstructorCheck {
78
MathContext mc;
89
BigDecimal bd1 = new BigDecimal("1");
10+
BigDecimal bd1_1 = new BigDecimal("2", mc);
11+
BigDecimal bd1_2 = new BigDecimal(new BigInteger("10"), mc);
12+
BigDecimal bd1_3 = new BigDecimal(1);
913
BigDecimal bd2 = new BigDecimal(2.0); // Noncompliant [[sc=20;ec=39]] {{Use "BigDecimal.valueOf" instead.}}
1014
BigDecimal bd4 = new BigDecimal(2.0, mc); // Noncompliant {{Use "BigDecimal.valueOf" instead.}}
1115
BigDecimal bd5 = new BigDecimal(2.0f); // Noncompliant {{Use "BigDecimal.valueOf" instead.}}
1216
BigDecimal bd6 = new BigDecimal(2.0f, mc); // Noncompliant {{Use "BigDecimal.valueOf" instead.}}
1317
BigDecimal bd3 = BigDecimal.valueOf(2.0);
1418

19+
Double d = 0.1;
20+
BigDecimal bd7 = new BigDecimal(d); // Noncompliant
21+
BigDecimal bd8 = new BigDecimal((Double) 0.1); // Noncompliant
1522

23+
double d2 = 0.1;
24+
BigDecimal bd7_2 = new BigDecimal(d2); // Noncompliant
25+
BigDecimal bd8_2 = new BigDecimal((double) 0.1); // Noncompliant
1626

27+
Float f = 0.1f;
28+
BigDecimal bd9 = new BigDecimal(f); // Noncompliant
29+
BigDecimal bd10 = new BigDecimal((Float) 0.1f); // Noncompliant
30+
31+
float f2 = 0.1f;
32+
BigDecimal bd9_2 = new BigDecimal(f2); // Noncompliant
33+
BigDecimal bd10_2 = new BigDecimal((float) 0.1); // Noncompliant
34+
35+
Object myDoubleObject = 23.5d;
36+
BigDecimal bd11 = new BigDecimal((Double) myDoubleObject); // Noncompliant
37+
38+
Object myIntObject = 12;
39+
BigDecimal bd12 = new BigDecimal((Integer) myIntObject);
40+
BigDecimal bd13 = new BigDecimal((Integer) 1);
1741
}

java-checks/src/main/java/org/sonar/java/checks/BigDecimalDoubleConstructorCheck.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,35 @@
1919
*/
2020
package org.sonar.java.checks;
2121

22+
import java.util.Collections;
23+
import java.util.List;
2224
import org.sonar.check.Rule;
2325
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
24-
import org.sonar.plugins.java.api.semantic.Type;
26+
import org.sonar.plugins.java.api.semantic.MethodMatchers;
2527
import org.sonar.plugins.java.api.tree.NewClassTree;
2628
import org.sonar.plugins.java.api.tree.Tree;
2729

28-
import java.util.Collections;
29-
import java.util.List;
30-
3130
@Rule(key = "S2111")
3231
public class BigDecimalDoubleConstructorCheck extends IssuableSubscriptionVisitor {
3332

33+
private static final MethodMatchers BIG_DECIMAL_DOUBLE_FLOAT =
34+
MethodMatchers.create().ofTypes("java.math.BigDecimal")
35+
.constructor()
36+
.addParametersMatcher("double")
37+
.addParametersMatcher("float")
38+
.addParametersMatcher("double", MethodMatchers.ANY)
39+
.addParametersMatcher("float", MethodMatchers.ANY)
40+
.build();
41+
3442
@Override
3543
public List<Tree.Kind> nodesToVisit() {
3644
return Collections.singletonList(Tree.Kind.NEW_CLASS);
3745
}
3846

3947
@Override
4048
public void visitNode(Tree tree) {
41-
if (hasSemantic()) {
42-
NewClassTree nct = (NewClassTree) tree;
43-
if (nct.symbolType().is("java.math.BigDecimal") && isDoubleConstructor(nct)) {
44-
reportIssue(tree, "Use \"BigDecimal.valueOf\" instead.");
45-
}
46-
}
47-
}
48-
49-
private static boolean isDoubleConstructor(NewClassTree nct) {
50-
if (!nct.arguments().isEmpty() && nct.arguments().size() <= 2) {
51-
Type argumentType = nct.arguments().get(0).symbolType();
52-
return argumentType.is("double") || argumentType.is("float");
49+
if (BIG_DECIMAL_DOUBLE_FLOAT.matches((NewClassTree) tree)) {
50+
reportIssue(tree, "Use \"BigDecimal.valueOf\" instead.");
5351
}
54-
return false;
5552
}
5653
}

0 commit comments

Comments
 (0)