Search in sources :

Example 91 with Node

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

the class PreprocessPhase method withPreprocessor.

private PreprocessPhaseResult withPreprocessor(WithStatement withStatement) {
    PreprocessPhaseResult result = new StatementResult();
    Deque<Statement> allNewStatements = new ArrayDeque<>();
    Optional<Expression> initScope = addTypeToInitialization(withStatement, allNewStatements);
    final Expression scope = initScope.orElse(withStatement.getWithObject());
    withStatement.findAll(AssignExpr.class).replaceAll(assignExpr -> assignToFieldAccess(result, scope, assignExpr));
    // Do not use findAll as we should only process top level expressions
    withStatement.getExpressions().replaceAll(e -> addScopeToMethodCallExpr(result, scope, e));
    NodeList<Statement> bodyStatements = wrapToExpressionStmt(withStatement.getExpressions());
    allNewStatements.addAll(bodyStatements);
    // delete modify statement and add the new statements to its children
    Node parentNode = withStatement.getParentNode().orElseThrow(() -> new MvelCompilerException("A parent node is expected here"));
    // See RuleChainingTest.testRuleChainingWithLogicalInserts
    if (parentNode instanceof BlockStmt) {
        BlockStmt parentBlock = (BlockStmt) parentNode;
        Iterator<Statement> newStatementsReversed = allNewStatements.descendingIterator();
        while (newStatementsReversed.hasNext()) {
            parentBlock.getStatements().addAfter(newStatementsReversed.next(), withStatement);
        }
        withStatement.remove();
    } else {
        throw new MvelCompilerException("Expecting a BlockStmt as a parent");
    }
    return result;
}
Also used : WithStatement(org.drools.mvel.parser.ast.expr.WithStatement) ModifyStatement(org.drools.mvel.parser.ast.expr.ModifyStatement) Statement(com.github.javaparser.ast.stmt.Statement) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayDeque(java.util.ArrayDeque) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression)

Example 92 with Node

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

the class ObjectCreationExpressionT method toJavaExpression.

@Override
public Node toJavaExpression() {
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(type);
    List<Expression> arguments = this.constructorArguments.stream().map(typedExpression -> (Expression) typedExpression.toJavaExpression()).collect(Collectors.toList());
    objectCreationExpr.setArguments(NodeList.nodeList(arguments));
    return objectCreationExpr;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) Collectors(java.util.stream.Collectors) Node(com.github.javaparser.ast.Node) Expression(com.github.javaparser.ast.expr.Expression)

Example 93 with Node

use of com.github.javaparser.ast.Node 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 94 with Node

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

the class MethodCallExprT method toJavaExpression.

@Override
public Node toJavaExpression() {
    Node scopeE = scope.map(TypedExpression::toJavaExpression).orElse(null);
    List<Expression> methodArguments;
    // MVEL forces a to string on each String value in map
    if (PUT_CALL.equals(name) && arguments.size() == 2) {
        methodArguments = coercedMapArguments();
    } else {
        methodArguments = toJavaExpressionArgument();
    }
    return new MethodCallExpr((Expression) scopeE, name, nodeList(methodArguments));
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 95 with Node

use of com.github.javaparser.ast.Node in project zaproxy by zaproxy.

the class JFlexToRstaTokenMaker method removeMethod.

private static void removeMethod(TypeDeclaration<?> type, String... names) {
    if (names == null || names.length == 0) {
        return;
    }
    for (String name : names) {
        List<MethodDeclaration> methods = type.getMethodsByName(name);
        MethodDeclaration method = methods.get(methods.size() - 1);
        method.getJavadocComment().ifPresent(Node::remove);
        method.remove();
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Node(com.github.javaparser.ast.Node)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6