Search in sources :

Example 26 with VariableDeclarationExpr

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

the class VariableDeclaratorTExpr method toJavaExpression.

@Override
public Node toJavaExpression() {
    Optional<Type> optInitType = initExpression.flatMap(TypedExpression::getType);
    com.github.javaparser.ast.type.Type jpType = toJPType(this.type);
    return initExpression.map(ie -> {
        Expression initializer = (Expression) ie.toJavaExpression();
        // Used to downcast map.get see testAddCastToMapGetOfDeclaration
        if (!optInitType.isPresent() || optInitType.get().equals(Object.class)) {
            initializer = new CastExpr(jpType, new EnclosedExpr(initializer));
        }
        return (Node) new VariableDeclarationExpr(new VariableDeclarator(jpType, name, initializer));
    }).orElse(new VariableDeclarationExpr(jpType, name));
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Type(java.lang.reflect.Type) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) TypeUtils.toJPType(org.drools.mvelcompiler.util.TypeUtils.toJPType) CastExpr(com.github.javaparser.ast.expr.CastExpr) Node(com.github.javaparser.ast.Node) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Type(java.lang.reflect.Type) TypeUtils.toJPType(org.drools.mvelcompiler.util.TypeUtils.toJPType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 27 with VariableDeclarationExpr

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

the class ForEachDowncastStmtT method toJavaExpression.

@Override
public Node toJavaExpression() {
    ForEachStmt newForEachStmt = new ForEachStmt();
    BlockStmt body = new BlockStmt();
    NodeList<VariableDeclarator> variables = nodeList();
    for (VariableDeclarator v : variableDeclarationExpr.getVariables()) {
        VariableDeclarator newVariable = v.clone();
        String newIteratorVariable = "_" + v.getNameAsString();
        VariableDeclarationExpr castAssign = new VariableDeclarationExpr(new VariableDeclarator(v.getType(), v.getName(), new CastExpr(v.getType(), new NameExpr(newIteratorVariable))));
        body.addStatement(0, castAssign);
        newVariable.setType(Object.class);
        newVariable.setName(newIteratorVariable);
        variables.add(newVariable);
    }
    body.addStatement((BlockStmt) child.toJavaExpression());
    newForEachStmt.setBody(body);
    VariableDeclarationExpr newVariables = new VariableDeclarationExpr(variables);
    newForEachStmt.setVariable(newVariables);
    return new ForEachStmt(newVariables, new NameExpr(iterable), body);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CastExpr(com.github.javaparser.ast.expr.CastExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 28 with VariableDeclarationExpr

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

the class AccumulateInline method parseInitBlock.

private void parseInitBlock() {
    MethodDeclaration initMethod = getMethodFromTemplateClass("init");
    String mvelBlock = addCurlyBracesToBlock(addSemicolon(accumulateDescr.getInitCode()));
    CompiledBlockResult initCodeCompilationResult = mvelCompiler.compileStatement(mvelBlock);
    BlockStmt initBlock = initCodeCompilationResult.statementResults();
    for (Statement stmt : initBlock.getStatements()) {
        final BlockStmt initMethodBody = initMethod.getBody().orElseThrow(InvalidInlineTemplateException::new);
        if (stmt.isExpressionStmt() && stmt.asExpressionStmt().getExpression().isVariableDeclarationExpr()) {
            VariableDeclarationExpr vdExpr = stmt.asExpressionStmt().getExpression().asVariableDeclarationExpr();
            for (VariableDeclarator vd : vdExpr.getVariables()) {
                final String variableName = vd.getNameAsString();
                contextFieldNames.add(variableName);
                contextData.addField(vd.getType(), variableName, Modifier.publicModifier().getKeyword());
                Optional<Expression> optInitializer = vd.getInitializer();
                optInitializer.ifPresent(initializer -> {
                    Expression target = new FieldAccessExpr(getDataNameExpr(), variableName);
                    Statement initStmt = new ExpressionStmt(new AssignExpr(target, initializer, AssignExpr.Operator.ASSIGN));
                    initMethodBody.addStatement(initStmt);
                    initStmt.findAll(NameExpr.class).stream().map(Node::toString).filter(context::hasDeclaration).forEach(usedExternalDeclarations::add);
                });
                accumulateDeclarations.add(new DeclarationSpec(variableName, DrlxParseUtil.getClassFromContext(context.getTypeResolver(), vd.getType().asString())));
            }
        }
    }
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(com.github.javaparser.ast.expr.Expression) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) CompiledBlockResult(org.drools.mvelcompiler.CompiledBlockResult)

Example 29 with VariableDeclarationExpr

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

the class MaterializedLambdaPredicate method createPredicateInformationMethod.

private void createPredicateInformationMethod(EnumDeclaration classDeclaration) {
    MethodDeclaration methodDeclaration = classDeclaration.addMethod("predicateInformation", Modifier.Keyword.PUBLIC);
    methodDeclaration.addAnnotation("Override");
    ClassOrInterfaceType predicateInformationType = toClassOrInterfaceType(PredicateInformation.class);
    methodDeclaration.setType(predicateInformationType);
    BlockStmt block = new BlockStmt();
    NameExpr infoExpr = new NameExpr("info");
    VariableDeclarationExpr infoVar = new VariableDeclarationExpr(toClassOrInterfaceType(PredicateInformation.class), "info");
    NodeList<Expression> newPredicateInformationArguments = NodeList.nodeList(toStringLiteral(StringEscapeUtils.escapeJava(predicateInformation.getStringConstraint())));
    ObjectCreationExpr newPredicateInformation = new ObjectCreationExpr(null, predicateInformationType, newPredicateInformationArguments);
    block.addStatement(new AssignExpr(infoVar, newPredicateInformation, AssignExpr.Operator.ASSIGN));
    int i = 0;
    NodeList<Expression> addRuleNamesArguments = null;
    for (PredicateInformation.RuleDef ruleDef : predicateInformation.getRuleDefs()) {
        if (i++ % 125 == 0) {
            addRuleNamesArguments = NodeList.nodeList();
            block.addStatement(new MethodCallExpr(infoExpr, "addRuleNames", addRuleNamesArguments));
        }
        addRuleNamesArguments.add(toStringLiteral(ruleDef.getRuleName()));
        addRuleNamesArguments.add(toStringLiteral(ruleDef.getFileName()));
    }
    if (predicateInformation.isMoreThanMaxRuleDefs()) {
        block.addStatement(new MethodCallExpr(infoExpr, "setMoreThanMaxRuleDefs", NodeList.nodeList(new BooleanLiteralExpr(true))));
    }
    block.addStatement(new ReturnStmt(infoExpr));
    methodDeclaration.setBody(block);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) PredicateInformation(org.drools.model.functions.PredicateInformation) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 30 with VariableDeclarationExpr

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

the class EvaluatorGenerator method createContextVariableAssignments.

private void createContextVariableAssignments(Map.Entry<String, Object> entry) {
    String binding = entry.getKey();
    Object contextVar = entry.getValue();
    if (contextVar != null) {
        Class<?> contextVarClass = contextVar instanceof Class ? (Class<? extends Object>) contextVar : contextVar.getClass();
        if (contextVarClass != null && contextVarClass.getCanonicalName() != null) {
            Type type = StaticJavaParser.parseType(contextVarClass.getCanonicalName());
            VariableDeclarationExpr variable = new VariableDeclarationExpr(type, binding);
            Expression indexMethodExpression = new CastExpr(type, new MethodCallExpr(new NameExpr("map"), "get", NodeList.nodeList(new StringLiteralExpr(binding))));
            methodBody.addStatement(0, variable);
            final Expression expr = new AssignExpr(new NameExpr(binding), indexMethodExpression, AssignExpr.Operator.ASSIGN);
            bindingAssignmentBlock.addStatement(expr);
            MethodCallExpr putExpr = new MethodCallExpr(new NameExpr("map"), "put", NodeList.nodeList(new StringLiteralExpr(binding), new NameExpr(binding)));
            repopulateMapBlock.addStatement(putExpr);
        }
    }
}
Also used : Type(com.github.javaparser.ast.type.Type) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)38 Test (org.junit.Test)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)16 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 Expression (com.github.javaparser.ast.expr.Expression)11 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Type (com.github.javaparser.ast.type.Type)4 Node (com.github.javaparser.ast.Node)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)3 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2