The java.lang.reflect
package contains the classes and interfaces that, along with
java.lang.Class, comprise the Java
Reflection API.
The
Constructor, Field, and
Method classes represent the constructors, fields,
and methods of a class. Because these types all represent members of
a class, they each implement the Member interface,
which defines a simple set of methods that can be invoked for any
class member. These classes allow information about the class members
to be obtained, methods and constructors to be invoked, and fields to
be queried and set.
Class member modifiers are represented
as integers that specify a number of bit flags. The
Modifier class defines static methods that help
interpret the meanings of these flags. The Array
class defines static methods for creating arrays and reading and
writing array elements.
As of Java 1.3, the
Proxy class allows the dynamic creation of new
Java classes that implement a specified set of interfaces. When an
interface method is invoked on an instance of such a proxy class, the
invocation is delegated to an InvocationHandler
object.
There have been a number of changes to this package to support the
new language features of Java 5.0. The most important changes are
support for querying the generic signature of classes, methods,
constructors, and fields. Class,
Method and Constructor
implement the new
GenericDeclaration interface, which provides access to
the TypeVariable declarations of generic classes,
methods, and constructors. In general, the package has been modified
to add new generic versions of methods like Field.getType(
) and Method.getParameterTypes( ).
Instead of returning Class objects, the new
generic methods, like Field.getGenericType(
)
and
Method.getGenericParameterTypes( ), return
Type objects. The
Type interface is new in Java 5.0, and
represents any kind of generic or nongeneric type.
Class implements Type, so a
Type object may simply be an ordinary
Class. Type is also the
super-interface for four other new interfaces:
ParameterizedType,
TypeVariable, WildcardType and
GenericArrayType. A Type object
that is not a Class should be an instance of one
of these other interfaces, representing a generic type of some sort.
Support for reflection on
annotations is provided by the
AnnotatedElement interface which is implemented by
Class, Package,
Method, Constructor and
Field. Method and
Constructor also have new
getParameterAnnotations( ) for querying
annotations on method parameters. Other, more minor changes in Java
5.0 include the isEnumConstant( ) method of
Field and the isVarArgs( )
method of Method and
Constructor.