Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Duplicate
-
2.0.6
-
None
-
None
-
None
Description
I've been using Spring's meta-annotation support lately [1], and I think a similar feature would be a good fit for Groovy's AST transformation annotations: @Bindable, @Canonical, @ToString, @EqualsAndHashCode, @TupleConstructor, @Immutable, etc.
The idea is that in order to avoid repetition and boilerplate code, you can annotate a custom annotation, and the custom annotation carries its metadata around with it.
So for example if I have 20 DTOs, and they all look like this:
@Bindable @Canonical(includes = "some,list,of,fields") @ToString(includeNames = true, includePackage = false) class MyDto { ... }
With meta-annotation support, I can instead write a custom annotation:
@Bindable @ToString(includeNames = true, includePackage = false) @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Dto { }
And then use that annotation in my DTOs:
@Dto @Canonical(includes = "some,list,of,fields") class MyDto { ... }
In the example above, we weren't able to move the @Canonical annotation into the meta-annotation because the list of "includes" fields varies by DTO. However, you could theoretically tell the meta-annotation to pull the @Canonical "includes" value from a parameter in your custom annotation, essentially allowing for parameterized meta-annotations:
@Bindable @ToString(includeNames = true, includePackage = false) @Canonical(includes = "businessKey") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Dto { String[] businessKey() default {}; }
Which would leave you with a beautifully terse, parameterized meta-annotation in those 20 DTOs:
@Dto(businessKey = "some,list,of,fields") class MyDto { ... }
Attachments
Issue Links
- duplicates
-
GROOVY-5821 Provide a way to assemble an annoation consisting of several other
- Closed