Search in sources :

Example 71 with Expression

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

the class QueryDefImplGenerator method callMethod.

private void callMethod(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration method = clazz.addMethod("call", Modifier.Keyword.PUBLIC);
    addOverride(method);
    method.addParameter("boolean", "open");
    method.setType(toClassOrInterfaceType(QueryCallViewItem.class));
    BlockStmt statements = new BlockStmt();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = toClassOrInterfaceType(QueryCallViewItemImpl.class);
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    statements.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument("this");
    objCreationExpr.addArgument("open");
    rangeArity().forEach(i -> {
        String varWithIndex = stringWithIndex("var", i);
        String genericTypeName = genericTypeName(i);
        method.addParameter(genericType("Argument", genericTypeName), varWithIndex);
        objCreationExpr.addArgument(varWithIndex);
    });
    method.setBody(statements);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 72 with Expression

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

the class QueryDefGenerator method callMethod.

private void callMethod(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration method = clazz.addMethod("call", Modifier.Keyword.DEFAULT);
    method.setType(toClassOrInterfaceType(QueryCallViewItem.class));
    BlockStmt stmts = new BlockStmt();
    NodeList<Expression> arguments = nodeList();
    MethodCallExpr objCreationExpr = new MethodCallExpr(null, "call", arguments);
    stmts.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument(new BooleanLiteralExpr(true));
    rangeArity().forEach(i -> {
        String varWithIndex = stringWithIndex("var", i);
        String genericTypeName = stringWithIndex("T", i);
        method.addParameter(genericType("Argument", genericTypeName), varWithIndex);
        objCreationExpr.addArgument(varWithIndex);
    });
    method.setBody(stmts);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) QueryCallViewItem(org.drools.model.view.QueryCallViewItem) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 73 with Expression

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

the class FromVisitor method createSingleFrom.

private Optional<Expression> createSingleFrom(String expression) {
    final Expression parsedExpression = DrlxParseUtil.parseExpression(expression).getExpr();
    if (parsedExpression instanceof FieldAccessExpr || parsedExpression instanceof NameExpr || parsedExpression instanceof DrlNameExpr) {
        return fromFieldOrName(expression);
    }
    if (parsedExpression instanceof ObjectCreationExpr) {
        return fromConstructorExpr(expression, (ObjectCreationExpr) parsedExpression);
    }
    if (parsedExpression instanceof LiteralExpr) {
        MethodCallExpr fromCall = createDslTopLevelMethod(FROM_CALL);
        fromCall.addArgument(parsedExpression);
        return of(fromCall);
    }
    return fromExpression(expression, parsedExpression);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 74 with Expression

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

the class EvaluatorGenerator method lastStatementIsGetter.

private void lastStatementIsGetter(BlockStmt mvelBlock, Expression expression) {
    MethodCallExpr methodCallExprClone = expression.asMethodCallExpr().clone();
    String getterName = methodCallExprClone.getName().asString().replace("set", "get");
    MethodCallExpr getter = new MethodCallExpr(getterName);
    methodCallExprClone.getScope().ifPresent(getter::setScope);
    final Expression assignExpr = new AssignExpr(new NameExpr(RESULT_VALUE_REFERENCE_NAME), getter, AssignExpr.Operator.ASSIGN);
    mvelBlock.addStatement(assignExpr);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Example 75 with Expression

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

the class EvaluatorGenerator method defineLastStatement.

// Simulate "Last expression is a return statement"
private void defineLastStatement(BlockStmt mvelBlock) {
    if (!lastMVELStatement.isExpressionStmt()) {
        return;
    }
    Expression expression = lastMVELStatement.asExpressionStmt().getExpression();
    addResultReference();
    if (expression.isMethodCallExpr() && expression.asMethodCallExpr().getNameAsString().startsWith("set")) {
        lastStatementIsGetter(mvelBlock, expression);
    } else {
        final Expression assignExpr = new AssignExpr(new NameExpr(RESULT_VALUE_REFERENCE_NAME), expression, AssignExpr.Operator.ASSIGN);
        mvelBlock.replace(lastMVELStatement, new ExpressionStmt(assignExpr));
    }
    returnResult();
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) 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