Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy hasProperty/respondsTo

Tags:

groovy

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'
like image 352
Dónal Avatar asked Jul 15 '11 09:07

Dónal


People also ask

Does Groovy have objects?

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.

How do I create an object in Groovy?

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.

What are Groovy properties?

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.


1 Answers

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.

like image 161
Igor Avatar answered Sep 25 '22 16:09

Igor