Created
March 26, 2016 11:54
-
-
Save int128/810690033ca07622f4b7 to your computer and use it in GitHub Desktop.
Groovy Trait with Annotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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