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:
#isAnotherWay = @java.lang.String@format("%1$tY/%1$tm/%1$td", #today)
Taking all the code above (which by the way shows you how to reformat a date) and combining it into a final expression will give us:
#today = new java.util.Date(),
#is = #today.toString(),
#isAnotherWay = @java.lang.String@format("%1$tY/%1$tm/%1$td",#today),
"Default: " + #is + " and our format: " + #isAnotherWay
The last line of code concatenates it all together to be the final value we want assigned to our attribute.
Here are the results when received by the application:
Stay tuned for more about OGNL. In the meantime please leave a comment on this post and let me know what topics you would like to see. Follow me on Twitter: @jdasilvaPI
OGNL Blog Series:
- Introduction to OGNL
- A simple OGNL expression
- Declaring variables in OGNL
- Method calls in OGNL
- Arrays in OGNL
- OGNL: What about those curly braces?
- Looping in OGNL
- Looping in OGNL take 2
- So what exactly is #this in OGNL?
- A continuing look at #this variable in OGNL
- Functions in OGNL
- Misc Topics in OGNL
John DaSilva develops training and solutions at Ping Identity.