Over a million developers have joined DZone.

Subtle Changes in Java 8: Repeatable Annotations

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Apart from the “big stuff”, related to extension methodslambda, and the streams API, Java 8 also has a couple of minor, very subtle changes. One of them is the fact that you can now annotate an object several times with the same annotation!

An example taken from the tutorial:

@Alert(role="Manager")
@Alert(role="Administrator")
public class UnauthorizedAccessException { ... }

For this to work, your @Alert annotation has to be meta-annotated with java.lang.annotation.Repeatable.

Tools, which heavily rely on annotation processing might need to review their code to get ready for Java 8. Existing methods, such as AnnotatedElement.getAnnotations(), have not been retrofitted to return repeatable annotations. Instead, other, new methods, such as AnnotatedElement.getAnnotationsByType(Class) and AnnotatedElement.getDeclaredAnnotationsByType(Class) have been added to the JDK, in order to be able to discover repeated annotations on any element.

More authoritative information can be found here:

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Lukas Eder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}