Groovy Metaprogramming

Groovy metaprogramming tutorials. Runtime and compile-time techniques including MetaClass, ExpandoMetaClass, categories, mixins, and AST transforms.

Groovy ExpandoMetaClass – Add Methods at Runtime with 10+ Examples

Groovy ExpandoMetaClass lets you add instance methods, static methods, constructors, and operators to any class at runtime. 10+ tested examples included. “ExpandoMetaClass is Groovy’s answer to open classes. If you wish a Java class had a method, just add it.” Last Updated: July 2026 | Tested on: Groovy 5.0.6, Java 17+ | Difficulty: Intermediate to Advanced | Reading Time: 18 minutes Ever wished String had a method to check for palindromes, or that Integer could compute factorials? In Java, you’d ... Read More

Groovy MOP (Meta-Object Protocol) – How It Works with 10+ Examples

Groovy MOP (Meta-Object Protocol) explained with 10+ tested examples. Learn method resolution, MetaClass hierarchy, and how Groovy dispatches every call. “The MOP is the beating heart of Groovy’s dynamism. Every method call, every property access, every operator – they all flow through the Meta-Object Protocol.” Last Updated: July 2026 | Tested on: Groovy 5.0.6, Java 17+ | Difficulty: Intermediate to Advanced | Reading Time: 18 minutes Unlike Java, where method calls are resolved at compile time and baked into bytecode, ... Read More

Groovy Categories and Mixins – Extend Existing Classes with 10+ Examples

You patch String with ExpandoMetaClass for one report, and a week later that method is showing up in every corner of the app. Scoped extensions solve this. Groovy categories add methods that exist only inside a use block and vanish when it ends, while mixins compose behavior from several classes. This guide covers both, with tested Groovy 5 examples. “Categories let you try on new methods like clothes – wear them when you need them, take them off when you’re ... Read More