circumstances when the type you want to cast to is represented by a type
parameter and so you can't use an actual language-level cast.
public booleanisAssignableFrom(Class<?> type)
Returns true if this Class object represents a supertype of type or type
itself; otherwise returns false.
16.2. Annotation Queries
You can ask about the annotations applied to a class or interface using methods that are similar to those used
for asking about members, but which differ slightly in the details. These methods are all part of the
AnnotatedElement interface. AnnotedElement is implemented by all the reflection classes that
represent program elements: Class, Field, Method, Constructor, and Package. The discussion here
applies to all these AnnotatedElement instances.
The annotation queries can only provide information on those annotations that are available at runtimethat is,
those annotations with a retention policy of RetentionPolicy.RUNTIME (see "Retention Policies" on
page 395).
You can ask for all the annotations present on an element, whether declared directly or inherited, by using
getAnnotations, which returns an array of Annotation instances. Or you can ask for only the directly
applied annotations using geTDeclaredAnnotations, which also returns an array of Annotation
instances. You can ask for a specific annotation using getAnnotation, which takes the type of the
annotation as an argument and returns the Annotation object or null if it is not present. You can ask
whether a specific annotation type is present on an element using the booleanisAnnotationPresent
method. In contrast to the similarly named methods for querying members, there is no notion of public versus
non-public annotations, and also no security checks.
The annotation instances that are returned are proxy objects that implement the interface defined by a given
annotation type. The methods of the annotation type can be invoked to obtain the annotation values for each
annotation element. For example, assuming our BugsFixed annotation type (see page 392) had a runtime
retention policy, then given
@BugsFixed( { "457605", "532456"} )
class Foo { / ... / }
the code
Class
BugsFixed bugsFixed =
(BugsFixed) cls.getAnnotation(BugsFixed.class);
String[] bugIds = bugsFixed.value();
for (String id : bugIds)
System.out.println(id);
would print
457605
532456