Skip to content

Commit

Permalink
[kie-issues#649] Use appropriate BigDecimal method to convert to prim…
Browse files Browse the repository at this point in the history
…itive type (apache#5567)

* Tests

* All tests passing

* Add integration tests.

* Revert "Add integration tests."

This reverts commit 98b9637.

---------

Co-authored-by: Tibor Zimányi <tiborzimanyi@ibm.com>
Co-authored-by: Mario Fusco <mario.fusco@gmail.com>
  • Loading branch information
3 people authored Oct 27, 2023
1 parent 75438b3 commit a1bed65
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand All @@ -30,6 +31,7 @@
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import org.drools.mvelcompiler.util.BigDecimalArgumentCoercion;
import org.drools.util.MethodUtils;

import static com.github.javaparser.ast.NodeList.nodeList;
Expand Down Expand Up @@ -72,7 +74,17 @@ public Node toJavaExpression() {
.map(this::convertToStringIfNeeded)
.collect(Collectors.toList());

return new MethodCallExpr((Expression) scope.toJavaExpression(), accessor.getName(), nodeList(expressionArguments));
Optional<Type> rhsType = this.arguments.stream()
.findFirst()
.flatMap(TypedExpression::getType);

// Right is BigDecimal, left is other, coerce
if(rhsType.isPresent() && rhsType.get().equals(BigDecimal.class) && !type.equals(BigDecimal.class)) {
Expression coercedExpression = new BigDecimalArgumentCoercion().coercedArgument(BigDecimal.class, (Class<?>)type, expressionArguments.get(0));
return new MethodCallExpr((Expression) scope.toJavaExpression(), accessor.getName(), nodeList(coercedExpression));
} else {
return new MethodCallExpr((Expression) scope.toJavaExpression(), accessor.getName(), nodeList(expressionArguments));
}
}

private Expression convertToStringIfNeeded(TypedExpression argumentExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public class Person {

private String name;
private int age;
private long longValue;
private short shortValue;
private double doubleValue;
private float floatValue;

private Integer integerBoxed;
private Long longBoxed;
private Short shortBoxed;
private Double doubleBoxed;
private Float floatBoxed;

private Person parent;
private Address address;
Expand Down Expand Up @@ -122,4 +132,76 @@ public void setAddresses(List<Address> addresses) {
public boolean isEven(int value) {
return true;
}

public long getLongValue() {
return longValue;
}

public void setLongValue(long longValue) {
this.longValue = longValue;
}

public short getShortValue() {
return shortValue;
}

public void setShortValue(short shortValue) {
this.shortValue = shortValue;
}

public double getDoubleValue() {
return doubleValue;
}

public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}

public float getFloatValue() {
return floatValue;
}

public void setFloatValue(float floatValue) {
this.floatValue = floatValue;
}

public Integer getIntegerBoxed() {
return integerBoxed;
}

public void setIntegerBoxed(Integer integerBoxed) {
this.integerBoxed = integerBoxed;
}

public Long getLongBoxed() {
return longBoxed;
}

public void setLongBoxed(Long longBoxed) {
this.longBoxed = longBoxed;
}

public Short getShortBoxed() {
return shortBoxed;
}

public void setShortBoxed(Short shortBoxed) {
this.shortBoxed = shortBoxed;
}

public Double getDoubleBoxed() {
return doubleBoxed;
}

public void setDoubleBoxed(Double doubleBoxed) {
this.doubleBoxed = doubleBoxed;
}

public Float getFloatBoxed() {
return floatBoxed;
}

public void setFloatBoxed(Float floatBoxed) {
this.floatBoxed = floatBoxed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,116 @@ public void testBigDecimalArithmeticWithConversionFromInteger() {
"}");
}

@Test
public void testBigDecimalAssignmentToInt() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.age = $p.salary;\n" +
"}",
"{ " +
" $p.setAge($p.getSalary().intValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToIntegerBoxed() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.integerBoxed = $p.salary;\n" +
"}",
"{ " +
" $p.setIntegerBoxed($p.getSalary().intValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToLong() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.longValue = $p.salary;\n" +
"}",
"{ " +
" $p.setLongValue($p.getSalary().longValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToLongBoxed() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.longBoxed = $p.salary;\n" +
"}",
"{ " +
" $p.setLongBoxed($p.getSalary().longValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToShort() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.shortValue = $p.salary;\n" +
"}",
"{ " +
" $p.setShortValue($p.getSalary().shortValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToShortBoxed() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.shortBoxed = $p.salary;\n" +
"}",
"{ " +
" $p.setShortBoxed($p.getSalary().shortValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToDouble() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.doubleValue = $p.salary;\n" +
"}",
"{ " +
" $p.setDoubleValue($p.getSalary().doubleValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToDoubleBoxed() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.doubleBoxed = $p.salary;\n" +
"}",
"{ " +
" $p.setDoubleBoxed($p.getSalary().doubleValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToFloat() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.floatValue = $p.salary;\n" +
"}",
"{ " +
" $p.setFloatValue($p.getSalary().floatValue());\n" +
"}");
}

@Test
public void testBigDecimalAssignmentToFloatBoxed() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ " +
" $p.floatBoxed = $p.salary;\n" +
"}",
"{ " +
" $p.setFloatBoxed($p.getSalary().floatValue());\n" +
"}");
}

@Test
public void testBigDecimalPromotionAllFourOperations() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
Expand Down

0 comments on commit a1bed65

Please sign in to comment.