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,
Alex.
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)
Music: https://www.youtube.com/watch?v=lYriMzzMsUw

Follow me at https://twitter.com/alexsotob




9 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...

yeezy shoes
longchamp
golden goose
caterpillar shoes
cheap nfl jerseys
nike shox
moncler sale
lebron 13
louboutin
curry shoes

norhan dijo...


تنظيف منازل بالدمام تنظيف منازل بالدمام
تنظيف منازل بالاحساء تنظيف منازل بالاحساء
تنظيف منازل بمكة تنظيف منازل بمكة
تنظيف منازل بجدة تنظيف منازل بجدة
تنظيف منازل بالمدينة المنورة تنظيف منازل بالمدينة المنورة

yanmaneee dijo...

calvin klein outlet
nike air max 2017
hermes handbags
kd 12
louboutin
Kanye West shoes
jordan shoes
timberland
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.

Zafran Yoda dijo...

I’d have to check with you here. Which is not something I usually do! I enjoy reading a post that will make people think. Also, thanks for allowing me to comment!

Information
Click Here
Visit Web
Godryshop.it
Information

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.

phethashough dijo...

important source replica louis vuitton bags pop over to this website replica louis vuitton visit here best replica designer