In my previous posts, we saw examples of method calls, or, how you can call a method on an object to either extract a value or perform some action that will produce a result we can use. In this post, I'll discuss how methods are called, the potential implications to what you're trying to accomplish, and how to deal with static methods in Java.
An important thing to understand about OGNL is that it's an interpreted language - at runtime, the OGNL processor or interpreter parses through your expression and tries to generate the proper Java code. As you can guess, this interpretation will have an impact on the runtime performance, so that is something to keep in mind as you write expressions.
See the complete list of articles in my OGNL series at the end of this post.
When it comes to method calls, OGNL tries to figure out which method to call by matching the most specific method based on the type information of the arguments provided. If more than one method is equally specific for the given arguments, then it will arbitrarily choose one.
Also, whenever you supply a null as an argument, OGNL matches the type of all non-primitive types, which may lead to unpredictable results. So, you should always try to provide values for all arguments unless you know that a null for a particular argument is allowed for a method call.
In those OGNL method calls, we've seen that the comma (,) is used to separate arguments. It's also the code separator. If a comma is inside parentheses, then it will be accepted as the separator for arguments; otherwise, it will be assumed to be a code block or line separator.
Up to this point, we have been calling methods on objects that we have instantiated by either taking it from the context (#this) or by using the new operator, for example:
#today = new java.util.Date()
In my previous post, I noted that we can then call a method such as toString:
#is = #today.toString()
(Of course, we could combine it all into one expression.)
You can use static methods in Java, but you must use a special syntax so that the OGNL interpreter understands that the method in question is static. In order for a static method to be recognized as such, you must prefix it with an at symbol (@) before the package name and the method, as shown below: