I am the founder and CEO of Data Geekery GmbH, located in Zurich, Switzerland. With our company, we have been selling database products and services around Java and SQL since 2013. Ever since my Master's studies at EPFL in 2006, I have been fascinated by the interaction of Java and SQL. Most of this experience I have obtained in the Swiss E-Banking field through various variants (JDBC, Hibernate, mostly with Oracle). I am happy to share this knowledge at various conferences, JUGs, in-house presentations and on our blog. Lukas is a DZone MVB and is not an employee of DZone and has posted 241 posts at DZone. You can read more from them at their website. View Full User Profile

Subtle Changes in Java 8: Repeatable Annotations

05.14.2013
| 8412 views |
  • submit to reddit

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:

About these ads
Published at DZone with permission of Lukas Eder, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: