Search in sources :

Example 1 with NodeWithTraversableScope

use of com.github.javaparser.ast.nodeTypes.NodeWithTraversableScope 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)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)1 NodeWithOptionalScope (com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope)1 NodeWithTraversableScope (com.github.javaparser.ast.nodeTypes.NodeWithTraversableScope)1 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)1 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)1 ListCreationLiteralExpression (org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression)1 MapCreationLiteralExpression (org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression)1