A class may be declared with one or more modifiers which affect its runtime behavior:Not all modifiers are allowed on all classes, for example an interface cannot be
- Access modifiers:
public
,protected
, andprivate
- Modifier requiring override:
abstract
- Modifier restricting to one instance:
static
- Modifier prohibiting value modification:
final
- Modifier forcing strict floating point behavior:
strictfp
- Annotations
final
and an enum cannot beabstract
.java.lang.reflect.Modifier
contains declarations for all possible modifiers. It also contains methods which may be used to decode the set of modifiers returned byClass.getModifiers()
.The
example shows how to obtain the declaration components of a class including the modifiers, generic type parameters, implemented interfaces, and the inheritance path. Since
ClassDeclarationSpy
Class
implements thejava.lang.reflect.AnnotatedElement
interface it is also possible to query the runtime annotations.A few samples of the output follows. User input is in italics.import java.lang.annotation.Annotation; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import static java.lang.System.out; public class ClassDeclarationSpy { public static void main(String... args) { try { Class<?> c = Class.forName(args[0]); out.format("Class:%n %s%n%n", c.getCanonicalName()); out.format("Modifiers:%n %s%n%n", Modifier.toString(c.getModifiers())); out.format("Type Parameters:%n"); TypeVariable[] tv = c.getTypeParameters(); if (tv.length != 0) { out.format(" "); for (TypeVariable t : tv) out.format("%s ", t.getName()); out.format("%n%n"); } else { out.format(" -- No Type Parameters --%n%n"); } out.format("Implemented Interfaces:%n"); Type[] intfs = c.getGenericInterfaces(); if (intfs.length != 0) { for (Type intf : intfs) out.format(" %s%n", intf.toString()); out.format("%n"); } else { out.format(" -- No Implemented Interfaces --%n%n"); } out.format("Inheritance Path:%n"); List<Class> l = new ArrayList<Class>(); printAncestor(c, l); if (l.size() != 0) { for (Class<?> cl : l) out.format(" %s%n", cl.getCanonicalName()); out.format("%n"); } else { out.format(" -- No Super Classes --%n%n"); } out.format("Annotations:%n"); Annotation[] ann = c.getAnnotations(); if (ann.length != 0) { for (Annotation a : ann) out.format(" %s%n", a.toString()); out.format("%n"); } else { out.format(" -- No Annotations --%n%n"); } // production code should handle this exception more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } } private static void printAncestor(Class<?> c, List<Class> l) { Class<?> ancestor = c.getSuperclass(); if (ancestor != null) { l.add(ancestor); printAncestor(ancestor, l); } } }This is the actual declaration for$ java ClassDeclarationSpy java.util.concurrent.ConcurrentNavigableMap Class: java.util.concurrent.ConcurrentNavigableMap Modifiers: public abstract interface Type Parameters: K V Implemented Interfaces: java.util.concurrent.ConcurrentMap<K, V> java.util.NavigableMap<K, V> Inheritance Path: -- No Super Classes -- Annotations: -- No Annotations --java.util.concurrent.ConcurrentNavigableMap
in the source code:Note that since this is an interface, it is implicitlypublic interface ConcurrentNavigableMap<K,V> extends ConcurrentMap, NavigableMap<K,V> abstract
. The compiler adds this modifier for every interface. Also, this declaration contains two generic type parameters,K
andV
. The example code simply prints the names of these parameters, but is it possible to retrieve additional information about them using methods injava.lang.reflect.TypeVariable
. Interfaces may also implement other interfaces as shown above.Since arrays are runtime objects, all of the type information is defined by the Java virtual machine. In particular, arrays implement$ java ClassDeclarationSpy "[Ljava.lang.String;" Class: java.lang.String[] Modifiers: public abstract final Type Parameters: -- No Type Parameters -- Implemented Interfaces: interface java.lang.Cloneable interface java.io.Serializable Inheritance Path: java.lang.Object Annotations: -- No Annotations --Cloneable
andjava.io.Serializable
and their direct superclass is alwaysObject
.From the inheritance path, it may be deduced that$ java ClassDeclarationSpy java.io.InterruptedIOException Class: java.io.InterruptedIOException Modifiers: public Type Parameters: -- No Type Parameters -- Implemented Interfaces: -- No Implemented Interfaces -- Inheritance Path: java.io.IOException java.lang.Exception java.lang.Throwable java.lang.Object Annotations: -- No Annotations --java.io.InterruptedIOException
is a checked exception becauseRuntimeException
is not present.This output shows that$ java ClassDeclarationSpy java.security.Identity Class: java.security.Identity Modifiers: public abstract Type Parameters: -- No Type Parameters -- Implemented Interfaces: interface java.security.Principal interface java.io.Serializable Inheritance Path: java.lang.Object Annotations: @java.lang.Deprecated()java.security.Identity
, a deprecated API, possesses the annotationjava.lang.Deprecated
. This may be used by reflective code to detect deprecated APIs.
Note: Not all annotations are available via reflection. Only those which have ajava.lang.annotation.RetentionPolicy
ofRUNTIME
are accessible. Of the three annotations pre-defined in the language@Deprecated
,@Override
, and@SuppressWarnings
only@Deprecated
is available at runtime.