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

Multiple Repeatable Annotations Container generated if an explicit container is present and an implicit one required

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.8, 3.0.0
    • Fix Version/s: 4.0.0-alpha-1, 3.0.3
    • Component/s: None
    • Labels:
      None

      Description

      Given:

      • an annotation A with String value which is repeatable with container annotation As

      If you in Java write

      @A("a")
      @As({
        @A("c")
        @A("d")
      }) 

      this is transported 1:1 to the byte code and if you use getAnnotationsByType(A.class) you properly get all three annotations.

      If you in Java write

      @A("a")
      @A("b")
      @As({
        @A("c")
        @A("d")
      }) 

      you get a compile error saying that this does not work.

      If you in Groovy write

      @A("a")
      @As([
        @A("c")
        @A("d")
      ]) 

      this is transported 1:1 to the byte code and if you use getAnnotationsByType(A.class) you properly get all three annotations.
      All ok.

      But now comes the issue.
      If you in Groovy write

      @A("a")
      @A("b")
      @As([
        @A("c")
        @A("d")
      ]) 

      you end up with two @As annotations in the final class file.
      If you then try to read annotations for example at runtime, you get

      java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface As: @As(value=[@A(value="a"), @A(value="b")])
      	at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:125)
      	at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
      	at java.lang.reflect.Executable.declaredAnnotations(Executable.java:599)
      	at java.lang.reflect.Executable.declaredAnnotations(Executable.java:597)
      	at java.lang.reflect.Executable.getAnnotation(Executable.java:570)
      	at java.lang.reflect.Method.getAnnotation(Method.java:622)
              ...
      

      So the Groovy compiler should probably also refuse to go on if there are mutliple single instances of a repeatable annotation if the respective container annotation is already present.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                vampire Björn Kautler
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m