Custom Builder Using Metaprogramming

As we saw earlier, builders provide a way to create an internal DSL for specialized complex tasks that use a nested or hierarchical structure or format. When working with a specialized task in our application, we can check for a builder that can solve the problem. If we don’t find any builders, we can create our own.

We have two ways to create a custom builder: take the entire effort on our shoulders by using Groovy’s metaprogramming capabilities, as we’ll see in this section, or use the BuilderSupport (Using BuilderSupport) or FactoryBuilderSupport (Using FactoryBuilderSupport) Groovy provides.

To help understand the BuilderSupport’s benefits, let’s build a to-do list. Here’s the code that’s using ...

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.