Java Reference
In-Depth Information
In the context of JUnit, annotations can differentiate methods that should be run as a unit test and
methods that are used for setup work.
Annotations can be used for documentation. For instance, the @Deprecated annotation is used to
indicate that a method should no longer be used.
The Java compiler can also process annotations in order to detect errors, suppress warnings, or
generate code.
Annotations are popular in Java EE, where they're used to configure enterprise applications.
A.1.1. Repeated annotations
Previous versions of Java forbid more than one annotation of a given annotation type to be
specified on a declaration. For this reason, the following code is invalid:
Java EE programmers often make use of an idiom to circumvent this restriction. You declare a
new annotation, which contains an array of the annotation you want to repeat. It looks like this:
@interface Author { String name(); }
@interface Authors {
Author[] value();
}
@Authors(
{ @Author(name="Raoul"), @Author(name="Mario") , @Author(name="Alan")}
)
class Book{}
The nested annotation on the Book class is pretty ugly. This is why Java 8 essentially removes
this restriction, which tidies things a bit. You're now allowed to specify multiple annotations of
the same annotation type on a declaration, provided they stipulate that the annotation is
repeatable. It's not the default behavior; you have to explicitly ask for an annotation to be
repeatable.
 
Search WWH ::




Custom Search