Nesting categories in Groovy

Recently I discovered that Groovy categories can be nested. It means, you can inject a category into another one:

@Category(GroovyObject)
class FirstCategory {
    def method1() { }
}

@Category(GroovyObject)
@Mixin(FirstCategory)
class SecondCategory {
    def method2() {
        method1()
    }
}

@Mixin(SecondCategory)
class Mixee {
    def yetAnotherMethod() {
        method2()
    }
}

Before that ‘discovery’ I was injecting all categories into the final class. My code was not expressing the explicit dependency between the second and the first category:

@Mixin([FirstCategory SecondCategory)
class Mixee

Happy mixin’!

Advertisements