Macro support in MVEL is a basic facility to allow the replacement of a token with an expanded source output. The functionality was introduced as a way of creating specialized keywords that enclose an Interceptor.
A macro can consist of of any legal identifier for example: modify
Consider the following code:
This is not a legal MVEL expression. There is, after all, no modify keyword in MVEL. However, in JBoss Drools this functionality is used to implement a form of change listener by replacing the modify identifier with the string: @Modify with.
The org.mvel.Macro Interface
The interface is very simple. Upon execution, the doMacro() method returns a String to replace the representative token. For example:
Using the MacroProcessor
The MacroProcessor is a lightweight and fast text parser that uses the provided macros to replace all the matching identifiers.
The defined macros are passed via a Map with the key representing the identifier to match, and the value being an instance of the Macro. For example:
The returned String can be simply passed to the MVEL compiler.
| Using Macros with the Interpreter If performance is an important consideration while using MVEL in interpreted mode, you should avoid using macros or instead consider switching to using pre-compiled expressions which only have a one-time overhead to parse and compile. |