Search in sources :

Example 1 with BigDecimalConvertedExprT

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));
}
Also used : ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BinaryExprT(org.drools.mvelcompiler.ast.BinaryExprT) ExpressionStmtT(org.drools.mvelcompiler.ast.ExpressionStmtT) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) LoggerFactory(org.slf4j.LoggerFactory) ClassUtils.getAccessor(org.drools.core.util.ClassUtils.getAccessor) Collections.singletonList(java.util.Collections.singletonList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) BigDecimal(java.math.BigDecimal) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr) Declaration(org.drools.mvelcompiler.context.Declaration) Map(java.util.Map) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ClassUtils.getSetter(org.drools.core.util.ClassUtils.getSetter) Method(java.lang.reflect.Method) ListAccessExprT(org.drools.mvelcompiler.ast.ListAccessExprT) Node(com.github.javaparser.ast.Node) VariableDeclaratorTExpr(org.drools.mvelcompiler.ast.VariableDeclaratorTExpr) SimpleNameTExpr(org.drools.mvelcompiler.ast.SimpleNameTExpr) Logger(org.slf4j.Logger) AssignExprT(org.drools.mvelcompiler.ast.AssignExprT) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) List(java.util.List) Type(java.lang.reflect.Type) BigDecimalConvertedExprT(org.drools.mvelcompiler.ast.BigDecimalConvertedExprT) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) DrlGenericVisitor(org.drools.mvel.parser.ast.visitor.DrlGenericVisitor) MapPutExprT(org.drools.mvelcompiler.ast.MapPutExprT) Optional(java.util.Optional) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) Collections(java.util.Collections) TypeUtils(org.drools.mvelcompiler.util.TypeUtils) BigDecimalConvertedExprT(org.drools.mvelcompiler.ast.BigDecimalConvertedExprT) BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) 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 BigDecimalConvertedExprT

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);
}
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

Type (java.lang.reflect.Type)2 BigDecimal (java.math.BigDecimal)2 BigDecimalArithmeticExprT (org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT)2 BigDecimalConvertedExprT (org.drools.mvelcompiler.ast.BigDecimalConvertedExprT)2 BinaryExprT (org.drools.mvelcompiler.ast.BinaryExprT)2 Node (com.github.javaparser.ast.Node)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ArrayAccessExpr (com.github.javaparser.ast.expr.ArrayAccessExpr)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 Method (java.lang.reflect.Method)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1