All the examples I see of using hasProperty
or respondsTo
show it being invoked on a metaClass
. Is there a reason why these methods shouldn't be invoked on the object itself, like this:
def propertyName = 'time'
if (new Date().hasProperty(propertyName))
println 'yes'
else
println 'no'
In Groovy, as in any other Object-Oriented language, there is the concept of classes and objects to represent the objected oriented nature of the programming language. A Groovy class is a collection of data and the methods that operate on that data.
To create an object by using positional parameters, the respective class needs to declare one or more constructors. In the case of multiple constructors, each must have a unique type signature. The constructors can also added to the class using the groovy. transform.
When a Groovy class definition declares a field without an access modifier, then a public setter/getter method pair and a private instance variable field is generated which is also known as "property" according to the JavaBeans specification.
The issue is discussed in this thread. Looks like they refer to the same thing in current versions of Groovy, but this was not always the case, as only the metaClass
had that call.
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