In Python I can see what methods and fields an object has with:
print dir(my_object)
What's the equivalent of that in Groovy (assuming it has one)?
Python dir() Function The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.
Help() and dir(), are the two functions that are reachable from the python interpreter. Both functions are utilized for observing the combine dump of build-in-function. These created functions in python are truly helpful for the efficient observation of the built-in system.
Looks particulary nice in Groovy (untested, taken from this link so code credit 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.
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