Java Programming/Reflection/Overview

Reflection is the mechanism by which Java exposes the features of a class during runtime, allowing Java programs to enumerate and access a class' methods, fields, and constructors as objects. In other words, there are object-based mirrors that reflect the Java object model, and you can use these objects to access an object's features using runtime API constructs instead of compile-time language constructs. Each object instance has a  method, inherited from, which returns an object with the runtime representation of that object's class; this object is an instance of the , which in turn has methods that return the fields, methods, constructors, superclass, and other properties of that class. You can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile-time dependencies on those features. The Java runtime provides the corresponding classes for reflection. Most of the Java classes that support reflection are in the. Reflection is most useful for performing dynamic operations with Java &mdash; operations that are not hard-coded into a source program, but that are determined at run time. One of the most important aspects of reflection is dynamic class loading.

Example: Invoking a method
One way to understand how reflection works is to use reflection to model how the Java Runtime Environment (JRE) loads and executes a class. When you invoke a Java program

and pass it command line arguments, the JRE must Steps 2, 3, and 4 can be accomplished with Java reflection. Below is an example of loading the  class, locating the   method, (see Understanding a Java Program) and invoking it via reflection.
 * 1) put the command line arguments arg0 ... argn into a [] array
 * 2) dynamically load the target class named by fully-qualified-class-name
 * 3) access the      method
 * 4) invoke the   method, passing the string array main.

This code is obviously more complicated than simply calling

However, the main Java runtime does not know about the  class. The name of the class to execute is a runtime value. Reflection allows a Java program to work with classes even though the classes are not known when the program was written. Let's explore what the  method is doing. The first statement at line 9 is an example of dynamic class loading. The  method will load a Java class and return an instance of  that results from loading the class. In this case, we are loading the class from the default package. We store the class object in the local variable ; its type is &lt;?&gt;. The second statement at line 10 simply creates a array with the four command line arguments we wish to pass to the   method of the   class. The third statement at line 11 performs a reflection operation on the  class. The  method is defined for the  class. It takes a variable number of parameters: the method name is the first parameter and the remaining parameters are the types of each of 's parameters. The method name is trivial: we want to invoke the  method, so we pass in the name. We then add a  variable for each of the method parameters. accepts one parameter ( so we add a single   element representing the String[]. The   method has a return type of  ; we store the result in a local variable named  . Finally, we invoke the method by calling the   method of the   instance. This method's first parameter is the instance to invoke on, and the remaining parameters are for the invokee's parameters. Since we are invoking a static method and not an instance method, we pass  as the instance argument. Since we only have a single parameter we pass it as the second argument. However, we must cast the parameter to Object to indicate that the array is the parameter, and not that the parameters are in the array. See varargs for more details on this.

The  method returns an   that will contain the result that the reflected method returns. In this case, our  method is a  method, so we ignore the return type. Most of the methods in this short  method may throw various exceptions. The method declares all of them in its signatures. Here is a brief rundown of what might throw an exception: In addition to these exceptions, there are also errors and runtime exceptions that these methods may throw.
 * will throw, if the named class cannot be located.
 * will throw, if the class could not be loaded due to the static initializer throwing an exception or a static field's initialization throwing an exception.
 * will throw
 * , if a matching method is not found, or is not public (use  to get a non-public method).
 * , if a security manager is installed and calling the method would result in an access violation (for example, the method is in the  package designed for internal use only).
 * may throw:
 * , if this method is invoked in a manner that violates its access modifiers.
 * for various reasons, including
 * passing an instance that does not implement this method.
 * the actual arguments do not match the method's arguments
 * , if the underlying method ( in this case) throws an exception.