Search in sources :

Example 21 with CastExpr

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

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

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

the class CastExprT method toJavaExpression.

@Override
public Node toJavaExpression() {
    com.github.javaparser.ast.type.Type jpType = StaticJavaParser.parseType(this.type.getCanonicalName());
    Expression expression = (Expression) innerExpr.toJavaExpression();
    return new EnclosedExpr(new CastExpr(jpType, expression));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 24 with CastExpr

use of com.github.javaparser.ast.expr.CastExpr 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)

Example 25 with CastExpr

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

the class DrlxParseUtil method findRootNodeViaScopeRec.

private static RemoveRootNodeResult findRootNodeViaScopeRec(Expression expr, LinkedList<Expression> acc) {
    if (expr.isArrayAccessExpr()) {
        throw new RuntimeException("This doesn't work on arrayAccessExpr convert them to a method call");
    }
    if (expr instanceof EnclosedExpr) {
        return findRootNodeViaScopeRec(expr.asEnclosedExpr().getInner(), acc);
    } else if (expr instanceof CastExpr) {
        return findRootNodeViaScopeRec(expr.asCastExpr().getExpression(), acc);
    } else if (expr instanceof ThisExpr) {
        return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
    } else if (expr instanceof NodeWithTraversableScope) {
        final NodeWithTraversableScope exprWithScope = (NodeWithTraversableScope) expr;
        return exprWithScope.traverseScope().flatMap((Expression scope) -> {
            if (isDslTopLevelNamespace(scope)) {
                return empty();
            }
            Expression sanitizedExpr = DrlxParseUtil.transformDrlNameExprToNameExpr(expr);
            acc.addLast(sanitizedExpr.clone());
            return of(findRootNodeViaScopeRec(scope, acc));
        }).orElse(new RemoveRootNodeResult(Optional.of(expr), expr, acc.isEmpty() ? expr : acc.getLast()));
    } else if (expr instanceof NameExpr) {
        if (!acc.isEmpty() && acc.getLast() instanceof NodeWithOptionalScope<?>) {
            ((NodeWithOptionalScope<?>) acc.getLast()).setScope(null);
            for (ListIterator<Expression> iterator = acc.listIterator(); iterator.hasNext(); ) {
                Expression e = iterator.next();
                if (e instanceof NodeWithOptionalScope) {
                    NodeWithOptionalScope<?> node = (NodeWithOptionalScope<?>) e;
                    if (iterator.hasNext()) {
                        node.setScope(acc.get(iterator.nextIndex()));
                    }
                }
            }
            return new RemoveRootNodeResult(Optional.of(expr), acc.getFirst(), acc.getLast());
        } else {
            return new RemoveRootNodeResult(Optional.of(expr), expr, expr);
        }
    }
    return new RemoveRootNodeResult(empty(), expr, expr);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) CastExpr(com.github.javaparser.ast.expr.CastExpr) NodeWithTraversableScope(com.github.javaparser.ast.nodeTypes.NodeWithTraversableScope) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) NodeWithOptionalScope(com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr)

Aggregations

CastExpr (com.github.javaparser.ast.expr.CastExpr)32 Expression (com.github.javaparser.ast.expr.Expression)21 NameExpr (com.github.javaparser.ast.expr.NameExpr)13 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)11 Test (org.junit.Test)9 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)6 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)6 Type (com.github.javaparser.ast.type.Type)6 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)6 NodeList (com.github.javaparser.ast.NodeList)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)4 UnificationTypedExpression (org.drools.modelcompiler.builder.generator.UnificationTypedExpression)4 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)4 SerializableFunction (org.kie.pmml.api.iinterfaces.SerializableFunction)4 Node (com.github.javaparser.ast.Node)3 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)3