Search in sources :

Example 1 with BinaryExprT

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));
}
Also used : BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) BinaryExprT(org.drools.mvelcompiler.ast.BinaryExprT) Method(java.lang.reflect.Method) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Example 2 with BinaryExprT

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);
}
Also used : BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) BigDecimalConvertedExprT(org.drools.mvelcompiler.ast.BigDecimalConvertedExprT) NullType(org.drools.core.util.MethodUtils.NullType) Type(java.lang.reflect.Type) TypeUtils.classFromType(org.drools.mvelcompiler.util.TypeUtils.classFromType) BinaryExprT(org.drools.mvelcompiler.ast.BinaryExprT) BigDecimal(java.math.BigDecimal)

Aggregations

BinaryExprT (org.drools.mvelcompiler.ast.BinaryExprT)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 BigDecimal (java.math.BigDecimal)1 NullType (org.drools.core.util.MethodUtils.NullType)1 BigDecimalArithmeticExprT (org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT)1 BigDecimalConvertedExprT (org.drools.mvelcompiler.ast.BigDecimalConvertedExprT)1 FieldToAccessorTExpr (org.drools.mvelcompiler.ast.FieldToAccessorTExpr)1 TypedExpression (org.drools.mvelcompiler.ast.TypedExpression)1 UnalteredTypedExpression (org.drools.mvelcompiler.ast.UnalteredTypedExpression)1 TypeUtils.classFromType (org.drools.mvelcompiler.util.TypeUtils.classFromType)1