5

Actually, I am going through new things in Java 8. There are many new stuff has come i.e Lambda expressions , Default methods and so many..

One of the feature is Repeating Annotations,

" Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use "

Here I can Understand that I can use same Annotation many times.

I am trying understand real / practical usage of this addition. and trying to understand why they missed it until java 7.

Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62

3 Answers3

10

If you have, for example, some role-based security, you'd be able to write something like:

@Authorize(role="USER")
@Authorize(role="ADMIN")
public void doStuff(){
...codez
}

In previous Java versions, to achieve the same, you'd have to do something like:

@Authorize(roles={"USER", "ADMIN"})
public void doStuff(){
...codez
}

or

@Authorize(role="USER, ADMIN")
public void doStuff(){
..moar codez
}

The difference, as you can see for yourself, is minimal, and the @Repeatable annotation is more syntactic sugar than a huge breakthrough. The benefit is not obvious unless you consider some more verbose annotations, say bunch of Quartz cron expressions. Prior java 8, you'd have to do something like:

@Cron(values={"* * * * * 10", "12 * * * * 0", "4 * 5 * 6 * 7"})
public void doCron(){

}

But in java 8 you could have a separate cron at each line, which makes the expression more readable.

Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
  • We can also write these as @Authorize(roles="ADMIN , User") public void doStuff(){ ...codez } – Pramod S. Nikam Apr 23 '14 at 05:54
  • We can write everything in more than one way. Java 8 just added a new way of writing it. Imagine if instead of roles, you had a bunch of quartz cron expressions. It would be nicer if each was on a separate lines. But once again, what Repeatable adds to java8, is more a syntactic sugar than something to ponder on from design perspective. – Nikola Yovchev Apr 23 '14 at 05:57
6

It is much helpful when you have multiple fields declared in an annotation. For example:

@Repeatable
public @interface Role {
   String type;
   String[] allowed;
}

In this case, it is much easier to use this kind of annotation for multiple roles:

@Role(type="readonly", allowed={"view"})
@Role(type="admin", allowed={"view,add,update,delete"})
public boolean checkAllowed() {
}
Ankit Bansal
  • 4,962
  • 1
  • 23
  • 40
2

The answer is the obvious: You want to annotate something with multiple values.

Prior to this feature you'd need to parse the single allowed argument in your annotation processing code:

@SomeAnnotation(value="this,that")
String foo;

Or (from comments):

@SomeAnnotation(value={"author1","author2"})
String foo;

The annotation processing code would need to have logic to deal with splitting the value and dealing with it, or getting multiple values. Allowing multiple annotations makes this simpler from a logic perspective. It's just a convenience feature.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • http://stackoverflow.com/questions/20632940/how-to-set-string-array-in-java-annotation – Nikola Yovchev Apr 23 '14 at 05:55
  • @baba Same deal. It's simpler code to process multiple annotations of the same type. I edited to make more clear this is simply a convenience feature. – Brian Roach Apr 23 '14 at 05:56