Search in sources :

Example 86 with Expression

use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.

the class FlattenScopeTest method expr.

private Expression expr(String inputExpr) {
    Expression expr = DrlxParseUtil.parseExpression(inputExpr).getExpr();
    // This is because parsing doesn't set type arguments.
    expr.ifMethodCallExpr(m -> m.setTypeArguments(nodeList()));
    expr.getChildNodes().forEach((Node e) -> {
        if (e instanceof Expression) {
            ((Expression) e).ifMethodCallExpr(m -> m.setTypeArguments(nodeList()));
        }
    });
    return expr;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node)

Example 87 with Expression

use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.

the class DrlxCompilerTest method testWhitespace.

@Test
public void testWhitespace() {
    ParseStart<Expression> context = ParseStart.EXPRESSION;
    MvelParser mvelParser = new MvelParser(new ParserConfiguration(), false);
    ParseResult<Expression> result = mvelParser.parse(context, provider("1\n+1"));
    assertEquals("1 + 1", result.getResult().get().toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MvelParser(org.drools.mvel.parser.MvelParser) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.Test)

Example 88 with Expression

use of com.github.javaparser.ast.expr.Expression 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 89 with Expression

use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.

the class StatementVisitor method visit.

@Override
public TypedExpression visit(ForEachStmt n, Void arg) {
    Expression iterable = n.getIterable();
    Optional<TypedExpression> convertedToDowncastStmt = iterable.toNameExpr().map(PrintUtil::printNode).flatMap(mvelCompilerContext::findDeclarations).filter(this::isDeclarationIterable).map(d -> toForEachDowncastStmtT(n, arg));
    if (convertedToDowncastStmt.isPresent()) {
        return convertedToDowncastStmt.get();
    }
    TypedExpression variableDeclarationExpr = new LHSPhase(mvelCompilerContext, Optional.empty()).invoke(n.getVariable());
    TypedExpression typedIterable = new RHSPhase(mvelCompilerContext).invoke(n.getIterable());
    TypedExpression body = n.getBody().accept(this, arg);
    return new ForEachStmtT(variableDeclarationExpr, typedIterable, body);
}
Also used : UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) ForEachStmtT(org.drools.mvelcompiler.ast.ForEachStmtT)

Example 90 with Expression

use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.

the class PreprocessPhase method addTypeToInitialization.

private Optional<Expression> addTypeToInitialization(WithStatement withStatement, Deque<Statement> allNewStatements) {
    if (withStatement.getWithObject().isAssignExpr()) {
        AssignExpr assignExpr = withStatement.getWithObject().asAssignExpr();
        Expression assignExprValue = assignExpr.getValue();
        Expression assignExprTarget = assignExpr.getTarget();
        if (assignExprValue.isObjectCreationExpr() && assignExprTarget instanceof DrlNameExpr) {
            ObjectCreationExpr constructor = assignExprValue.asObjectCreationExpr();
            ClassOrInterfaceType ctorType = constructor.getType();
            String targetVariableName = ((DrlNameExpr) assignExprTarget).getNameAsString();
            VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(ctorType, targetVariableName);
            AssignExpr withTypeAssignmentExpr = new AssignExpr(variableDeclarationExpr, assignExprValue, assignExpr.getOperator());
            allNewStatements.add(new ExpressionStmt(withTypeAssignmentExpr));
            return of(new DrlNameExpr(targetVariableName));
        }
    }
    return empty();
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43