Multiple annotations of the same type on one element?

前端 未结 8 1282
再見小時候
再見小時候 2020-11-30 21:39

I\'m attempting to slap two or more annotations of the same type on a single element, in this case, a method. Here\'s the approximate code that I\'m working with:

         


        
8条回答
  •  攒了一身酷
    2020-11-30 22:32

    http://docs.oracle.com/javase/tutorial/java/annotations/repeating.html

    Starting from Java8 you can describe repeatable annotations:

    @Repeatable(FooValues.class)
    public @interface Foo {
        String bar();
    }
    
    public @interface FooValues {
        Foo[] value();
    }
    

    Note, value is required field for values list.

    Now you can use annotations repeating them instead of filling the array:

    @Foo(bar="one")
    @Foo(bar="two")
    public void haha() {}
    

提交回复
热议问题