Search in sources :

Example 1 with NodeWithArguments

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

the class DrlxParseUtil method rescopeNamesToNewScope.

/**
 * Mutates expression
 * such that, if it contains a NameExpr for any of the <code>names</code>,
 * it is replaced with a FieldAccessExpr having <code>newScope</code> as the scope.
 */
public static void rescopeNamesToNewScope(Expression newScope, List<String> names, Node e) {
    if (e instanceof NodeWithArguments) {
        NodeWithArguments<?> arguments = (NodeWithArguments) e;
        for (Expression argument : arguments.getArguments()) {
            rescopeNamesToNewScope(newScope, names, argument);
        }
    }
    if (e instanceof AssignExpr) {
        AssignExpr assignExpr = (AssignExpr) e;
        rescopeNamesToNewScope(newScope, names, assignExpr.getTarget());
        rescopeNamesToNewScope(newScope, names, assignExpr.getValue());
    } else if (e instanceof BinaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getLeft());
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getRight());
    } else if (e instanceof UnaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((UnaryExpr) e).getExpression());
    } else if (e instanceof EnclosedExpr) {
        rescopeNamesToNewScope(newScope, names, ((EnclosedExpr) e).getInner());
    } else if (e instanceof Expression) {
        Optional<Expression> rootNode = DrlxParseUtil.findRootNodeViaScope((Expression) e);
        if (rootNode.isPresent() && rootNode.get() instanceof NameExpr) {
            NameExpr nameExpr = (NameExpr) rootNode.get();
            if (names.contains(nameExpr.getNameAsString())) {
                Expression prepend = new FieldAccessExpr(newScope, nameExpr.getNameAsString());
                if (e instanceof NameExpr) {
                    Optional<Node> parentNode = e.getParentNode();
                    if (parentNode.isPresent()) {
                        // actually `e` was not composite, it was already the NameExpr node I was looking to replace.
                        parentNode.get().replace(nameExpr, prepend);
                    } else {
                        throw new IllegalStateException("Cannot find parent node for " + ((NameExpr) e).getNameAsString() + "!");
                    }
                } else {
                    e.replace(nameExpr, prepend);
                }
            }
        }
    } else {
        for (Node child : e.getChildNodes()) {
            rescopeNamesToNewScope(newScope, names, child);
        }
    }
}
Also used : BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Node(com.github.javaparser.ast.Node) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) 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) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) NodeWithArguments(com.github.javaparser.ast.nodeTypes.NodeWithArguments)

Aggregations

Node (com.github.javaparser.ast.Node)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)1 NodeWithArguments (com.github.javaparser.ast.nodeTypes.NodeWithArguments)1 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)1 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)1 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)1 ListCreationLiteralExpression (org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression)1 MapCreationLiteralExpression (org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression)1