Chapter 9. Reflection
The Reflection API allows a Java program
to inspect and manipulate itself; it comprises the
java.lang.Class class and the
java.lang.reflect package, which represents the
members of a class with Method,
Constructor, and Field objects.Reflection can obtain information about a
class and its members. This is the technique that the
JavaBeans™ (see Chapter 15)
introspection mechanism uses to determine the properties, events, and
methods that are supported by a bean, for example. Reflection can
also manipulate objects in Java. You can use the
Field class to query and set the values of fields,
the Method class to invoke methods, and the
Constructor class to create new objects. The
examples in this chapter demonstrate both object inspection and
manipulation using the Reflection API.In addition to the examples in this chapter, the Java Reflection API
also features prominently in Examples Example 15-10
and Example 18-3.