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);
}
}
}
Aggregations