use of org.drools.mvelcompiler.ast.TypedExpression 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.TypedExpression 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.TypedExpression in project drools by kiegroup.
the class LHSPhase method invoke.
public TypedExpression invoke(Node n) {
logPhase("LHS phase on: {}", n);
TypedExpression typedExpression = n.accept(this, null);
if (typedExpression == null) {
throw new MvelCompilerException("Type check of " + printNode(n) + " failed.");
}
logger.debug("LHS phase completed");
return typedExpression;
}
use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.
the class RHSPhase method visit.
@Override
public TypedExpression visit(ObjectCreationExpr n, Context arg) {
List<TypedExpression> constructorArguments = new ArrayList<>();
for (Expression e : n.getArguments()) {
TypedExpression compiledArgument = e.accept(this, arg);
constructorArguments.add(compiledArgument);
}
return new ObjectCreationExpressionT(constructorArguments, resolveType(n.getType()));
}
use of org.drools.mvelcompiler.ast.TypedExpression in project drools by kiegroup.
the class RHSPhase method visit.
@Override
public TypedExpression visit(ArrayAccessExpr n, Context arg) {
TypedExpression name = n.getName().accept(this, arg);
Optional<Type> type = name.getType();
if (type.filter(TypeUtils::isCollection).isPresent()) {
return new ListAccessExprT(name, n.getIndex(), type.get());
}
return new UnalteredTypedExpression(n, type.orElse(null));
}
Aggregations