Search in sources :

Example 1 with FieldToAccessorTExpr

use of org.drools.mvelcompiler.ast.FieldToAccessorTExpr in project drools by kiegroup.

the class LHSPhase method withBigDecimalConversion.

// Conversion of AssignExpr to BigDecimal Arithmetic operation when LHS is is a BigDecimal variable
public Optional<TypedExpression> withBigDecimalConversion(AssignExpr assignExpr, TypedExpression target, TypedExpression value) {
    Optional<Type> optRHSType = value.getType();
    if (!optRHSType.isPresent()) {
        return Optional.empty();
    }
    AssignExpr.Operator operator = assignExpr.getOperator();
    if (operator == AssignExpr.Operator.ASSIGN) {
        return Optional.empty();
    }
    // handled previously in FieldAccessExpr visitor
    boolean assigningToFieldAccess = target instanceof FieldToAccessorTExpr;
    if (!assigningToFieldAccess && target.getType().filter(t -> t == BigDecimal.class).isPresent()) {
        String bigDecimalMethod = BigDecimalArithmeticExprT.toBigDecimalMethod(operator);
        BigDecimalArithmeticExprT convertedBigDecimalExpr = new BigDecimalArithmeticExprT(bigDecimalMethod, target, value);
        return Optional.of(new AssignExprT(AssignExpr.Operator.ASSIGN, target, convertedBigDecimalExpr));
    }
    return Optional.empty();
}
Also used : BigDecimalArithmeticExprT(org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT) Type(java.lang.reflect.Type) AssignExprT(org.drools.mvelcompiler.ast.AssignExprT) BigDecimal(java.math.BigDecimal) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Example 2 with FieldToAccessorTExpr

use of org.drools.mvelcompiler.ast.FieldToAccessorTExpr 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 3 with FieldToAccessorTExpr

use of org.drools.mvelcompiler.ast.FieldToAccessorTExpr in project drools by kiegroup.

the class LHSPhase method tryParseItAsSetter.

private Optional<TypedExpression> tryParseItAsSetter(FieldAccessExpr n, TypedExpression scope, Class<?> setterArgumentType) {
    return scope.getType().flatMap(scopeType -> {
        String setterName = printNode(n.getName());
        Optional<Method> optAccessor = ofNullable(getSetter((Class<?>) scopeType, setterName, setterArgumentType)).map(Optional::of).orElse(ofNullable(getSetter((Class<?>) scopeType, setterName, String.class)));
        List<TypedExpression> arguments = rhs.map(Collections::singletonList).orElse(emptyList());
        return optAccessor.map(accessor -> new FieldToAccessorTExpr(scope, accessor, arguments));
    });
}
Also used : Method(java.lang.reflect.Method) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Example 4 with FieldToAccessorTExpr

use of org.drools.mvelcompiler.ast.FieldToAccessorTExpr in project drools by kiegroup.

the class LHSPhase method visit.

@Override
public TypedExpression visit(AssignExpr n, Void arg) {
    logPhase("AssignExpr {}", n);
    TypedExpression target = n.getTarget().accept(this, arg);
    Optional<TypedExpression> bigDecimalConversion = withBigDecimalConversion(n, target, rhsOrError());
    if (bigDecimalConversion.isPresent()) {
        return bigDecimalConversion.get();
    }
    if (target instanceof FieldToAccessorTExpr || target instanceof VariableDeclaratorTExpr || target instanceof MapPutExprT) {
        return target;
    }
    return new AssignExprT(n.getOperator(), target, rhsOrNull());
}
Also used : MapPutExprT(org.drools.mvelcompiler.ast.MapPutExprT) AssignExprT(org.drools.mvelcompiler.ast.AssignExprT) VariableDeclaratorTExpr(org.drools.mvelcompiler.ast.VariableDeclaratorTExpr) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) FieldToAccessorTExpr(org.drools.mvelcompiler.ast.FieldToAccessorTExpr)

Example 5 with FieldToAccessorTExpr

use of org.drools.mvelcompiler.ast.FieldToAccessorTExpr 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)

Aggregations

FieldToAccessorTExpr (org.drools.mvelcompiler.ast.FieldToAccessorTExpr)5 TypedExpression (org.drools.mvelcompiler.ast.TypedExpression)4 UnalteredTypedExpression (org.drools.mvelcompiler.ast.UnalteredTypedExpression)4 Method (java.lang.reflect.Method)3 AssignExprT (org.drools.mvelcompiler.ast.AssignExprT)3 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 Type (java.lang.reflect.Type)2 BigDecimal (java.math.BigDecimal)2 BigDecimalArithmeticExprT (org.drools.mvelcompiler.ast.BigDecimalArithmeticExprT)2 BinaryExprT (org.drools.mvelcompiler.ast.BinaryExprT)2 MapPutExprT (org.drools.mvelcompiler.ast.MapPutExprT)2 VariableDeclaratorTExpr (org.drools.mvelcompiler.ast.VariableDeclaratorTExpr)2 Node (com.github.javaparser.ast.Node)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ArrayAccessExpr (com.github.javaparser.ast.expr.ArrayAccessExpr)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