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