Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can be real case use of Repeating annotations?

Tags:

java

java-8

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.

like image 550
Pramod S. Nikam Avatar asked Apr 23 '14 05:04

Pramod S. Nikam


People also ask

What are repeating annotations?

As of the Java SE 8 release, repeating annotations enable you to do this. For example, you are writing code to use a timer service that enables you to run a method at a given time or on a certain schedule, similar to the UNIX cron service.

How do you make annotations repeatable?

Repeatable Annotation: The Repeating Annotation has to be annotated with @Repeatable to have the capability to repeat. We have to pass the class name as a parameter to the meta-annotation @Repeatable where the compiler can store all the multiple annotations and that acts as a container for values.

Why are annotations used?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.

On which of these can annotations be used on in Java 8?

With Java 8, annotations can now also be written on any use of a type such as types in declarations, generics, and casts: @Encrypted String data; List<@NonNull String> strings; myGraph = (@Immutable Graph) tmpGraph; At first glance, type annotations aren't the sexiest feature of the new Java release.


3 Answers

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() {
}
like image 36
Ankit Bansal Avatar answered Sep 23 '22 17:09

Ankit Bansal


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.

like image 80
Nikola Yovchev Avatar answered Sep 24 '22 17:09

Nikola Yovchev


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.

like image 44
Brian Roach Avatar answered Sep 24 '22 17:09

Brian Roach