use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.
the class ConstraintCompiler method compileExpression.
public CompiledExpressionResult compileExpression(Expression parsedExpression) {
// Avoid processing the LHS as it's not present while compiling an expression
TypedExpression compiled = new RHSPhase(mvelCompilerContext).invoke(parsedExpression);
Expression expression = (Expression) compiled.toJavaExpression();
return new CompiledExpressionResult(expression, compiled.getType()).setUsedBindings(mvelCompilerContext.getUsedBindings());
}
use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.
the class MethodCallExprVisitor method visit.
@Override
public TypedExpression visit(MethodCallExpr n, RHSPhase.Context arg) {
Optional<TypedExpression> scope = n.getScope().map(s -> s.accept(this, arg));
TypedExpression name = n.getName().accept(this, new RHSPhase.Context(scope.orElse(null)));
final List<TypedExpression> arguments = new ArrayList<>(n.getArguments().size());
for (Expression child : n.getArguments()) {
TypedExpression a = child.accept(this, arg);
arguments.add(a);
}
Class<?>[] argumentsTypes = parametersType(arguments);
return parseMethodFromDeclaredFunction(n, arguments).orElseGet(() -> parseMethod(n, scope, name, arguments, argumentsTypes));
}
use of org.drools.mvelcompiler.ast.TypedExpression 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.TypedExpression in project drools by kiegroup.
the class LHSPhase method visit.
@Override
public TypedExpression visit(ArrayAccessExpr n, Void arg) {
if (parentIsExpressionStmt(n)) {
return rhsOrError();
}
TypedExpression name = n.getName().accept(this, arg);
Optional<Type> type = name.getType();
if (type.filter(TypeUtils::isCollection).isPresent()) {
Expression index = n.getIndex();
if (index.isStringLiteralExpr() || index.isNameExpr()) {
return new MapPutExprT(name, index, rhsOrNull(), name.getType());
} else {
return new ListAccessExprT(name, index, type.get());
}
}
return new UnalteredTypedExpression(n, type.orElse(null));
}
use of org.drools.mvelcompiler.ast.TypedExpression 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