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

Custom Methods Using the Extension Modules

Groovy 2.x helps us move quickly beyond the privilege of using methods added to the GDK. Using the extension-modules feature, we can add our own instance or static methods to existing classes at compile time and use them throughout the application at runtime. Let’s take a look at the simple steps we have to follow for this using an example.

We need two things for this feature to work: First, a method we want to add must be defined in an extension-module class. Second, we need a descriptor file in the manifest file to tell the Groovy compiler what extension-module classes to look for.

Let’s create two extension methods—an instance method and a static method—both on the String class, to get the price ...

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