Search in sources :

Example 1 with TypedExpression

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));
}
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 2 with TypedExpression

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));
    });
}
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 3 with TypedExpression

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;
}
Also used : UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 4 with 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()));
}
Also used : UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) ObjectCreationExpressionT(org.drools.mvelcompiler.ast.ObjectCreationExpressionT) ArrayList(java.util.ArrayList) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Example 5 with TypedExpression

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));
}
Also used : NullType(org.drools.core.util.MethodUtils.NullType) Type(java.lang.reflect.Type) TypeUtils.classFromType(org.drools.mvelcompiler.util.TypeUtils.classFromType) ListAccessExprT(org.drools.mvelcompiler.ast.ListAccessExprT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Aggregations

TypedExpression (org.drools.mvelcompiler.ast.TypedExpression)21 UnalteredTypedExpression (org.drools.mvelcompiler.ast.UnalteredTypedExpression)18 Expression (com.github.javaparser.ast.expr.Expression)8 Node (com.github.javaparser.ast.Node)4 Type (java.lang.reflect.Type)4 Optional (java.util.Optional)4 FieldToAccessorTExpr (org.drools.mvelcompiler.ast.FieldToAccessorTExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 Optional.ofNullable (java.util.Optional.ofNullable)3 DrlGenericVisitor (org.drools.mvel.parser.ast.visitor.DrlGenericVisitor)3 DoStmtT (org.drools.mvelcompiler.ast.DoStmtT)3 ForEachStmtT (org.drools.mvelcompiler.ast.ForEachStmtT)3 MapPutExprT (org.drools.mvelcompiler.ast.MapPutExprT)3 WhileStmtT (org.drools.mvelcompiler.ast.WhileStmtT)3 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 DoStmt (com.github.javaparser.ast.stmt.DoStmt)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2