Skip to content

Instantly share code, notes, and snippets.

@int128
Created March 26, 2016 11:54
Show Gist options
  • Save int128/810690033ca07622f4b7 to your computer and use it in GitHub Desktop.
Save int128/810690033ca07622f4b7 to your computer and use it in GitHub Desktop.
Groovy Trait with Annotation
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
@Retention(RetentionPolicy.RUNTIME)
@interface T {
Class formatter
}
trait A {
@T(formatter = {})
String foo
}
trait B {
def bar() {
this.class.declaredFields.collectEntries { field ->
[(field): field.getAnnotation(T)]
}.findAll { field, annotation ->
annotation != null
}
}
}
class C implements A, B {
@T()
String baz
}
def c = new C(foo: 'FOO')
c.bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment