Method Expressions
Method expressions can be "invoked". A javax.el.MethodExpression is invoked by calling its invoke(ELContext, Object<?>[]) method. The specification also allows to treat literal text as a method expression.
A method expression is called a literal method expression if its underlying expression is literal text (that is, isLiteralText() returns true). All other method expressions are called non-literal method expressions. Non-literal method expressions share the same syntax as lvalue expressions.
For literal method expressions
- invoke(ELContext, Object<?>[]) simply returns the expression string, optionally coerced to the expected return type specified at creation time.
- getMethodInfo(ELContext) always returns null.
On the other hand, non-literal method expressions refer to a java.lang.reflext.Method which can be invoked or used to create a javax.el.MethodInfo instance. For non-literal method expressions
- invoke(ELContext, Object<?>[]) evaluates the expression to a java.lang.reflext.Method and invokes that method, passing over the given actual parameters.
- the found method must match the expected return type (if it is not null) and the argument types given at creation time; otherwise an exception is thrown.
Tree Method Expressions
Class de.odysseus.el.TreeMethodExpression is a subclass of javax.el.MethodExpression, which is used by JUEL to represent method expressions. It is the return type of
ExpressionFactoryImpl.createMethodExpression(ELContext, String, Class<?>, Class<?>[])
In addition to the methods declared by javax.el.MethodExpression, it provides
- void dump(java.io.PrintWriter writer) – dump parse tree
- boolean isDeferred() – true if expression is deferred (contains eval expressions #{...})
import java.io.*; import de.odysseus.el.*; import de.odysseus.el.util.*; ... ExpressionFactoryImpl factory = new ExpressionFactoryImpl(); SimpleContext context = new SimpleContext(); // more on this here... TreeMethodExpression e = factory.createMethodExpression(context, "#{x.toString}", String.class, new Class[]{}); PrintWriter out = new PrintWriter(System.out); e.dump(out); // +- #{...} // | // +- . toString // | // +- x out.flush(); System.out.println(e.isDeferred()); // true ...