Search code examples
javainterceptormvel

Intercept input and output of each function in an MVEL expression


I am quite new to MVEL. What I am trying to achieve is to log input and outputs of every function in an expression.

For example, having the following expression

h.function1('value1') != h.function2('value2') 

what I am trying to log is the input and output values of function1 and function2. I read about Interceptors. If I do undestand right, to add and interceptor I need to rewrite the above expression as the following.

@Intercept h.function1('value1') != @Intercept h.function2('value2')

However, I cannot force my users to add the @Intercept annotation.

Which is the best way to accomplish this task? Have I to resume Spring AOP? :(


Solution

  • It's not @Intercept but the name of intercept you put in map. Bellow is @test annotation. Please check detail here https://github.com/mvel/mvel/blob/master/src/test/java/org/mvel2/tests/core/CoreConfidenceTests.java

    interceptors.put("test",
            testInterceptor);
    
     executeExpression(compileExpression("@test System.out.println('MIDDLE');",
            null,
            interceptors));