I'm wondering what is the best way to retrieve nested properties in Groovy, taking a given Object and arbitrary "property" String. I would like to something like this:
someGroovyObject.getProperty("property1.property2")
I've had a hard time finding an example of others wanting to do this, so maybe I'm not understanding some basic Groovy concept. It seems like there must be some elegant way to do this.
As reference, there is a feature in Wicket that is exactly what I'm looking for, called the PropertyResolver: http://wicket.apache.org/apidocs/1.4/org/apache/wicket/util/lang/PropertyResolver.html
Any hints would be appreciated!
You can also use
propertyMissing
. This is what you might call Groovy's built-in method.Declare this in your class:
Then refer to your properties as desired:
This is automatically recursive, and you don't have to explicitly call a method. This is only a getter, but you can define a second version with parameters to make a setter as well.
The downside is that, as far as I can tell, you can only define one version of
propertyMissing
, so you have to decide if dynamic path navigation is what you want to use it for.Groovy Beans let you access fields directly. You do not have to define getter/setter methods. They get generated for you. Whenever you access a bean property the getter/setter method is called internally. You can bypass this behavior by using the .@ operator. See the following example:
If you are not dealing with collections you can simply just call the field you want to access.
If you want to access a field within a collection you have to select the element:
I don't know if Groovy has a built-in way to do this, but here are 2 solutions. Run this code in the Groovy Console to test it.
See
https://stackoverflow.com/a/15632027/2015517
It uses ${} syntax that can be used as part of GString