Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.
The ability to examine and manipulate a Java class from within itself may not sound like very much, but in other programming languages this feature simply doesn't exist. For example, there is no way in a Pascal, C, or C++ program to obtain information about the functions defined within that program.
One tangible use of reflection is in JavaBeans, where software components can be manipulated visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically loaded.
A Simple Example
To see how reflection works, consider this simple example:
import java.lang.reflect.*; public class DumpMethods { public static void main(String args[]) { try { Class c = Class.forName(args[0]); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); } catch (Throwable e) { System.err.println(e); } } } |
For an invocation of:
java DumpMethods java.util.Stack
the output is:
public java.lang.Object java.util.Stack.push( java.lang.Object) public synchronized java.lang.Object java.util.Stack.pop() public synchronized java.lang.Object java.util.Stack.peek() public boolean java.util.Stack.empty() public synchronized int java.util.Stack.search(java.lang.Object) |
That is, the method names of class
java.util.Stack
are listed, along with their fully qualified parameter and return types.
This program loads the specified class using
class.forName
, and then calls getDeclaredMethods
to retrieve the list of methods defined in the class. java.lang.reflect.Method
is a class representing a single class method.Setting Up to Use Reflection
The reflection classes, such as
Method
, are found in java.lang.reflect. There are three steps that must be followed to use these classes. The first step is to obtain a java.lang.Class
object for the class that you want to manipulate. java.lang.Class
is used to represent classes and interfaces in a running Java program.
One way of obtaining a Class object is to say:
Class c = Class.forName("java.lang.String");to get the Class object for
String
. Another approach is to use:Class c = int.class;or
Class c = Integer.TYPE;to obtain Class information on fundamental types. The latter approach accesses the predefined
TYPE
field of the wrapper (such as Integer
) for the fundamental type.
The second step is to call a method such as
getDeclaredMethods
, to get a list of all the methods declared by the class.
Once this information is in hand, then the third step is to use the reflection API to manipulate the information. For example, the sequence:
Class c = Class.forName("java.lang.String"); Method m[] = c.getDeclaredMethods(); System.out.println(m[0].toString());will display a textual representation of the first method declared in
String
.
In the examples below, the three steps are combined to present self contained illustrations of how to tackle specific applications using reflection.
Reflection tutorial
Reflection class
Reflection get class object
Reflection get super class object
Reflection get implemented interfaces
Reflection get all public methods
Reflection get all public constructors
Reflection get all public fields
Reflection get all annotations
Reflection get package name
Reflection get class modifiers
Reflection get parameter type
Reflection get public field
Reflection get field declaring class
Reflection get field type
Reflection get set public field value
Reflection get set private field value
Reflection get public method
Reflection get method parameter names
Reflection get method parameter types
Reflection get method return value
Reflection get method modifier value
Reflection call or invoke public method
Reflection call or invoke private method
Reflection get public constructor
Reflection get private constructor
Reflection instantiate an object
Reflection class
Reflection get class object
Reflection get super class object
Reflection get implemented interfaces
Reflection get all public methods
Reflection get all public constructors
Reflection get all public fields
Reflection get all annotations
Reflection get package name
Reflection get class modifiers
Reflection get parameter type
Reflection get public field
Reflection get field declaring class
Reflection get field type
Reflection get set public field value
Reflection get set private field value
Reflection get public method
Reflection get method parameter names
Reflection get method parameter types
Reflection get method return value
Reflection get method modifier value
Reflection call or invoke public method
Reflection call or invoke private method
Reflection get public constructor
Reflection get private constructor
Reflection instantiate an object
No comments:
Post a Comment