Access MVEL ASTNode info

113 Views Asked by At

After compiling an MVEL expression, I can access the top ASTNode.

ExpressionCompiler compiler = new ExpressionCompiler(<expression>, true);
ASTNode node = compiler.compile().getFirstNode();

However, I would like to have full access to the ASTNode info, including its left and right child nodes.

ASTNode left = node.getLeftNode();
ASTNode right = node.getRightNode();
...

Is there a way to do it?

1

There are 1 best solutions below

0
Mike Zhou On

If an expression has a binary operator, for example, (a + b), the corresponding ASTNode is of type BineryOperation. Therefore, the following code can be used to solve the issue:

ExpressionCompiler compiler = new ExpressionCompiler("a + b", true);
ASTNode node = compiler.compile().getFirstNode();
BinaryOperation biOptNode= (BinaryOperation)node;
ASTNode left = biOptNode.getLeftNode();
ASTNode right = biOptNode.getRightNode();