use of org.drools.mvelcompiler.ast.BinaryExprT in project drools by kiegroup.
the class LHSPhase method compoundOperator.
/**
* Conversion of the compound operator applied to number literals
* $p.age += 50;
* $p.setAge($p.getAge() + 50));
*/
private FieldToAccessorTExpr compoundOperator(TypedExpression fieldAccessScope, String accessorName, Class<?> scopeType, AssignExpr.Operator parentOperator, Method setter) {
BinaryExpr.Operator operator = BinaryExprT.compoundToArithmeticOperation(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();
BinaryExprT arithmeticExprT = new BinaryExprT(getterExpression, argument, operator);
return new FieldToAccessorTExpr(fieldAccessScope, setter, singletonList(arithmeticExprT));
}
use of org.drools.mvelcompiler.ast.BinaryExprT 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