Java Reference
In-Depth Information
the code
Class<Foo> cls = Foo.class;
BugsFixed bugsFixed =
(BugsFixed) cls.getAnnotation(BugsFixed.class);
String[] bugIds = bugsFixed.value();
for (String id : bugIds)
System.out.println(id);
would print
457605
532456
If an annotation method represents an annotation element with a Class
type, and that class can not be found at runtime, then you will get a
TypeNotPresentException (which is an unchecked exception that is analog-
ous to ClassNotFoundException ).
Since the annotation type that is available at runtime could be different
from the annotation type used to annotate the class being inspected, it
is possible that the two uses of the type are incompatible. If this occurs,
then trying to access an element of the annotation may throw an Annota-
tionTypeMismatchException or IncompleteAnnotationException . If an element
type is an enum and the enum constant in the annotation is no longer
present in the enum, then an EnumConstantNotPresentException is thrown.
Exercise 16.4 : Write a program that prints all the available annotations
applied to a given type. (Only annotations with a retention policy of
RUNTIME will be available.)
Exercise 16.5 : Expand ClassContents to include the available annotation
information for each member.
 
Search WWH ::




Custom Search