O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Injecting Methods Using ExpandoMetaClass

To create domain-specific languages (DSLs), we need to be able to add arbitrary methods to different classes and even hierarchies of classes. We need to inject instance methods and static methods, manipulate constructors, and convert a method to a property for the sake of fluency. We’ll want these capabilities for creating mock objects to stand in for collaborators. In this section, we’ll discuss the techniques to alter and enhance a class’s structure.

We can inject methods into a class by adding methods to its MetaClass. The methods we inject are available globally. We’re not restricted to a block like in categories. We discussed ExpandoMetaClass in Intercepting Methods Using MetaClass. Using ExpandoMetaClass ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required