What does the Groovy equivalent of Python dir () mean?

In Python, I see which methods and fields objects have:

print dir(my_object) 

What is equivalent to what in Groovy (provided that it is)?

+7
source share
3 answers

It looks especially nice in Groovy (untested, taken from this link , so the credit code should go there):

 // Introspection, know all the details about classes : // List all constructors of a class String.constructors.each{println it} // List all interfaces implemented by a class String.interfaces.each{println it} // List all methods offered by a class String.methods.each{println it} // Just list the methods names String.methods.name // Get the fields of an object (with their values) d = new Date() d.properties.each{println it} 

The general term you are looking for is introspection .

+8
source

As described here , to find all the methods defined for a String object:

  "foo".metaClass.methods*.name.sort().unique() 

It is not as simple as the Python version, maybe someone else can show a better way.

+4
source

Besides using the regular Java reflection API, there are:

http://docs.codehaus.org/display/GROOVY/JN3535-Reflection

You can also play games with metaclasses.

0
source

All Articles