I want to invoke the static method of a class without putting any object in the context of JEXL.
For instance methods, we put an object to the MapContext and use the key to call the method. but In my case, I don't have anything in context. ${person.howYouDoing()} I don't want to use person object to put in the context.
There's a good discussion of this as a JEXL improvement at https://issues.apache.org/jira/browse/JEXL-140.
The guy who requested this proposed a solution that puts the burden on the JEXL programmer. It creates a String only to get the String class, which is only used to look up the desired class.
${''.class.forName('person').howYouDoing()}
...assuming that "person" is in the default package. This can be used to call static constructors for classes like Pattern for which there is no public constructor:
${''.class.forName('java.util.regex.Pattern').compile('\\d{2}-(\\w{3})-\\d{2}')}
By the way, the JEXL developers suggested subclassing JexlContext to always return any class which exists. This is more elegant than requiring your template writers do the ''.class.forName() hack, but since you didn't want to modify your MapContext, it might not satisfy your question. It also pollutes your context with all classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With