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 ...

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.