lunes, febrero 12, 2018

Repeatable Annotations in Java 8

With Java 8 you are able to repeat the same annotation to a declaration or type. For example, to register that one class should only be accessible at runtime by specific roles, you could write something like:

Notice that now @Role is repeated several times. For compatibility reasons, repeating annotations are stored in a container annotation, so instead of writing just one annotation you need to write two, so in the previous case, you need to create @Role and @Roles annotations.

Notice that you need to create two annotations, one which is the "plural" part of the annotation where you set return type of value method to be an array of the annotation that can be used multiple times. The other annotation can be used multiple time in the scope where it is defined and must be annotated with @Repeatable annotation.

This is how I did all the time since Java 8 allows to do it. But last week, during a code review my mate George Gastaldi pointed me out how they are implementing these repeatable annotations in javax.validation spec.  Of course, it is not completely different but I think that looks pretty much clear from point of view implementation since everything is implemented within the same archive and also, in my opinion, the name looks much natural. 

Notice that now everything is placed in the same archive. Since usually you only need to refer to @Role class, and not @Roles (now @Role.List) annotation you can hide this annotation as an inner annotation. Also in case of defining several annotations, this approach makes everything look more compact, instead of having of populating the hierarchy with "duplicated" classes serving the same purpose, you only create one.

Of course, I am not saying that the approach of having two classes is wrong, at the end is about preferences since both are really similar. But after implementing repeatable annotations in this way, I think that it is cleaner and compact solution having everything defined in one class.

We keep learning,
Jo sóc l'hipopòtam, i crec que el lleó, ha de refrescar-se per estar molt millor (El Lleó Vergonyós - El Pot Petit)

Follow me at

7 comentarios:

Dr. Lina dijo...

Jual obat aborsi cytotec
Jual obat aborsi makassar

Jual obat aborsi bandung
Jual obat aborsi batam
Jual obat aborsi malang
Jual obat aborsi palembang

Jual obat aborsi semarang

Jual obat aborsi jakarta

Jual obat aborsi bekasi

Jual obat aborsi surabaya

Jual obat aborsi di Semarang, Jogja, yogyakarta,Malang, Surabaya, Jakarta, Bandung, Bekasi, Batam, Palembang dan Makassar.

Obat Aborsi Manjur dijo...

Jual Obat Aborsi ,

Obat Aborsi Manjur ,

yanmaneee dijo...

calvin klein outlet
nike air max 2017
hermes handbags
kd 12
Kanye West shoes
jordan shoes
supreme new york
hermes birkin

steve dijo...

I think you will love to know about this tool called Quickbooks Install Diagnostic Tool where it's able to resolve any kind of Quickbooks error which occurs during installation of Quickbooks.

Lily dijo...

t's an Intuit-created fixation tool (the parent company of QuickBooks). The greatest QuickBooks connection diagnostic tool has been validated by users all around the world. When multi-user faults disrupt the network connection, the utility assists in rebuilding it.

searathey dijo...

click this w1o40m4r21 replica bags from china replica prada nylon bags replica bags paypal gucci replica i8g89n0l50 replica bags ru replica bags in china click this link here now t6y29z9s37 replica bags wholesale in divisoria

Unknown dijo...

j9p14v9q06 r6y99v5p30 t3f12k6w73 n9k80u9o32 o6g70w8w72 k0c75s9g38