THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

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
analogous 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
AnnotationTypeMismatchException 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.


16.3. The Modifier Class


The Modifier class defines int constants for all non-annotation modifiers: ABSTRACT, FINAL,
INTERFACE, NATIVE, PRIVATE, PROTECTED, PUBLIC, STATIC, STRICT, SYNCHRONIZED,
TRANSIENT, and VOLATILE. For each constant there is a corresponding query method
isMod(intmodifiers) that returns TRue if modifier mod is present in the specified value. For example,
if a field is declared


public static final int OAK = 0;


the value returned by its Field object's getModifiers method would be


Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL


The strictfp modifier is reflected by the constant STRICT. If code or a class is to be evaluated in strict
floating point (see "Strict and Non-Strict Floating-Point Arithmetic" on page 203), the modifiers for the
method, class, or interface will include the STRICT flag.


The query methods can be used to ask questions in a more symbolic fashion. For example, the expression


Modifier.isPrivate(field.getModifiers())


is equivalent to the more opaque expression


(field.getModifiers() & Modifier.PRIVATE) != 0

Free download pdf