You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/06/20 15:29:00 UTC

[jira] [Commented] (GROOVY-8234) @Repeatable java8 annotation not supported by groovy

    [ https://issues.apache.org/jira/browse/GROOVY-8234?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16055940#comment-16055940 ] 

Paul King commented on GROOVY-8234:
-----------------------------------

Yes, Groovy has supported "repeatable" annotations for a long time even in Java 5 so long as retention policy was only SOURCE. This is what allows multiple @Grab statements for instance without the outer @Grapes container annotation. We don't currently support @Repeatable at all but plan to in a future version.

> @Repeatable java8 annotation not supported by groovy
> ----------------------------------------------------
>
>                 Key: GROOVY-8234
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8234
>             Project: Groovy
>          Issue Type: Bug
>         Environment: groovy 2.4.11
>            Reporter: Dmitry Lukyanov
>
> raised on stackoverflow: https://stackoverflow.com/questions/44532632/is-the-repeatable-annotation-not-supported-by-groovy/44628119#44628119
> Problem: the following code in groovy 2.4.11 / java8
> {code}
>    @MyAnnotation(value = "val1")
>    @MyAnnotation(value = "val2")
>    void annotatedMethod() { println("annotated method called") }
> {code}
> should be compiled to this:
> {code}
>    @MyAnnotationArray({@MyAnnotation("val1"), @MyAnnotation("val2")})
>    void annotatedMethod() { println("annotated method called") }
> {code}
> but actually compiled to this:
> {code}
>    @MyAnnotation(value = "val1")
>    @MyAnnotation(value = "val2")
>    void annotatedMethod() { println("annotated method called") }
> {code}
> The full groovy script to reproduce problem is below.
> It throws exception: 
> {color:red}java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface MyAnnotation: @MyAnnotation(value=val2){color}
> at line `List annos = m.getAnnotations()`
> {code}
> import java.lang.annotation.*
> class MyClass 
> {
>     @MyAnnotation(value = "val1")
>     @MyAnnotation(value = "val2")
>     //change annotation to next line and the code will work
>     //@MyAnnotationArray( [@MyAnnotation("val1"), @MyAnnotation("val2")] )
>     public void annotatedMethod()
>     {
>       System.out.println("annotated method called");
>     }
>    public static void main(String... args)
>    {
>       MyClass ob = new MyClass()
>       ob.annotatedMethod()
>       java.lang.reflect.Method m = ob.getClass().getMethod("annotatedMethod")
>       List annos = m.getAnnotations()
>       println("annos = $annos")
>    }
> }
> @Target(ElementType.METHOD)
> @Retention(RetentionPolicy.RUNTIME)
> @Repeatable(MyAnnotationArray) 
> public @interface MyAnnotation
> {
>     String value() default "val0";
> }
> @Retention(RetentionPolicy.RUNTIME)
> public @interface MyAnnotationArray 
> {
>    MyAnnotation[] value()
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)