Querying Methods and Properties

At runtime, we can query an object’s methods and properties to find out if the object supports a certain behavior. This is especially useful for behavior we add dynamically at runtime. We can add behavior not only to classes, but also to select instances of a class.

We can use MetaObjectProtocol’s getMetaMethod (MetaClass extends MetaObjectProtocol) to get a metamethod. We can use getStaticMetaMethod if we’re looking for a static method. To get a list of overloaded methods, we use the plural forms of these methods—getMetaMethods and getStaticMetaMethods. Similarly, we can use getMetaProperty and getStaticMetaProperty for a metaproperty. If we want simply to check for existence and not get the metamethod or metaproperty, ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.