use of org.drools.mvelcompiler.ast.BigDecimalConvertedExprT in project drools by kiegroup.
the class LHSPhase method bigDecimalCompoundOperator.
/**
* Conversion of the compound operator applied to BigDecimal
* $p.salary += 50000B;
* $p.setSalary($p.getSalary().add(new BigDecimal(\"50000\")));
*/
private FieldToAccessorTExpr bigDecimalCompoundOperator(TypedExpression fieldAccessScope, String accessorName, Class<?> scopeType, AssignExpr.Operator parentOperator, Method setter) {
String bigDecimalArithmeticMethod = BigDecimalArithmeticExprT.toBigDecimalMethod(parentOperator);
Method optGetter = ofNullable(getAccessor(scopeType, accessorName)).orElseThrow(() -> new MvelCompilerException("No getter found but setter is present for accessor: " + accessorName));
FieldToAccessorTExpr getterExpression = new FieldToAccessorTExpr(fieldAccessScope, optGetter, emptyList());
TypedExpression argument = rhsOrError();
if (argument.getType().filter(t -> t != BigDecimal.class).isPresent()) {
argument = new BigDecimalConvertedExprT(argument);
}
BigDecimalArithmeticExprT bigDecimalArithmeticExprT = new BigDecimalArithmeticExprT(bigDecimalArithmeticMethod, getterExpression, argument);
return new FieldToAccessorTExpr(fieldAccessScope, setter, singletonList(bigDecimalArithmeticExprT));
}
use of org.drools.mvelcompiler.ast.BigDecimalConvertedExprT in project drools by kiegroup.
the class RHSPhase method withPossiblyBigDecimalConversion.
private TypedExpression withPossiblyBigDecimalConversion(TypedExpression left, TypedExpression right, BinaryExpr.Operator operator) {
Optional<Type> optTypeLeft = left.getType();
Optional<Type> optTypeRight = right.getType();
if (!optTypeLeft.isPresent() || !optTypeRight.isPresent()) {
// coerce only when types are known
return new BinaryExprT(left, right, operator);
}
Type typeLeft = optTypeLeft.get();
Type typeRight = optTypeRight.get();
boolean binaryOperatorNeedBigDecimalConversion = asList(BinaryExpr.Operator.PLUS, BinaryExpr.Operator.DIVIDE, BinaryExpr.Operator.MINUS, BinaryExpr.Operator.MULTIPLY, BinaryExpr.Operator.REMAINDER, BinaryExpr.Operator.EQUALS, BinaryExpr.Operator.NOT_EQUALS).contains(operator);
boolean isStringConcatenation = operator == BinaryExpr.Operator.PLUS && (typeLeft == String.class || typeRight == String.class);
if (binaryOperatorNeedBigDecimalConversion && !isStringConcatenation) {
boolean shouldNegate = operator == BinaryExpr.Operator.NOT_EQUALS;
if (typeLeft == BigDecimal.class && typeRight == BigDecimal.class) {
// do not convert
return new BigDecimalArithmeticExprT(toBigDecimalMethod(operator), left, right, shouldNegate);
} else if (typeLeft != BigDecimal.class && typeRight == BigDecimal.class) {
// convert left
return new BigDecimalArithmeticExprT(toBigDecimalMethod(operator), new BigDecimalConvertedExprT(left), right, shouldNegate);
} else if (typeLeft == BigDecimal.class && typeRight != BigDecimal.class) {
// convert right
return new BigDecimalArithmeticExprT(toBigDecimalMethod(operator), left, new BigDecimalConvertedExprT(right), shouldNegate);
}
}
return new BinaryExprT(left, right, operator);
}
Aggregations