Evaluating the MetaClass runtime
Since 1.1, Groovy supports a much richer set of APIs for evaluating the MetaClass runtime. Using these APIs in combination with ExpandoMetaClass makes Groovy an extremely powerful language for meta-programming
Finding out methods and properties
To obtain a list of methods ( or MetaMethod instances in Groovy speak) for a particular Groovy class use can inspect its MetaClass:
The same can be done for properties:
Using respondsTo and hasProperty
Obtaining a list of methods sometimes is a little more than what you want. It is quite common in meta-programming scenarios to want to find out if an object supports a particular method.
Since 1.1, you can use respondsTo and hasProperty to achieve this:
The respondsTo method actually returns a List of MetaMethod instances so you can use it to both query and evaluate the resulting list.
respondsTo only works for "real" methods and those added via ExpandoMetaClass and not for cases where you override invokeMethod or methodMissing. It is impossible in these cases to tell if an object responds to a method without actually invoking the method.