Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5929

Support meta-annotations for AST transformation annotations

    XMLWordPrintableJSON

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 {
        ...
      }

      [1] http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/#tx-custom-attributes

      Attachments

        Issue Links

          Activity

            People

              guillaume Guillaume Sauthier
              sdanig Daniel Gredler
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: