Java provides a set of mechanisms to dynamically execute methods, constructor methods, and array operations. This mechanism is called reflection. Reflection mechanisms are the basis for implementation of many popular frameworks today, including Spring and Hibernate. The principle issue is not the focus of this Article. Next, let's learn about this wonderful mechanism in the instance.
1. Get the attributes of an object
1 public Object getProperty (Object owner, String fieldName) throws Exception {
2 Class ownerClass = owner. getClass ();
3
4 Field field = ownerClass. getField (fieldName );
5
6 Object property = field. get (owner );
7
8 return property;
9}
Class ownerClass = owner. getClass (): obtains the Class of the object.
Field field = ownerClass. getField (fieldName): obtains the attribute declared by the Class through the Class.
Object property = field. get (owner): obtains the instance of this attribute through the Object. If this attribute is not public, IllegalAccessException is reported here.
2. Obtain the static attributes of a class.
1 public Object getStaticProperty (String className, String fieldName)
2 throws Exception {
3 Class ownerClass = Class. forName (className );
4
5 Field field = ownerClass. getField (fieldName );
6
7 Object property = field. get (ownerClass );
8
9 return property;
10}
Class ownerClass = Class. forName (className): first obtain the Class of this Class.
Field field = ownerClass. getField (fieldName): Same as above, get the attribute declared by Class through Class.
Object property = field. get (ownerClass): This is somewhat different from the above. Because this property is static, it is directly retrieved from the Class of the Class.
3. Execute the method of an object
1 public Object invokeMethod (Object owner, String methodName, Object [] args) throws Exception {
2
3 Class ownerClass = owner. getClass ();
4
5 Class [] argsClass = new Class [args. length];
6
7 for (int I = 0, j = args. length; I <j; I ++ ){
8 argsClass [I] = args [I]. getClass ();
9}
10
11 Method method = ownerClass. getMethod (methodName, argsClass );
12
13 return method. invoke (owner, args );
14}
Class owner_class = owner. getClass (): first, you must obtain the Class of this object.
5 ~ Row 9: configure the Class array of the parameter as the condition for finding the Method.
Method method = ownerClass. getMethod (methodName, argsClass): Obtain the Method to be executed through the Method name and the Class array of the parameter.
Method. invoke (owner, args): executes this Method. The parameters of the invoke method are the objects that execute this Method, and the parameter arrays. The returned value is an Object and the return value of this method.
4. Execute the static method of a class
1 public Object invokeStaticMethod (String className, String methodName,
2 Object [] args) throws Exception {
3 Class ownerClass = Class. forName (className );
4
5 Class [] argsClass = new Class [args. length];
6
7 for (int I = 0, j = args. length; I <j; I ++ ){
8 argsClass [I] = args [I]. getClass ();
9}
10
11 Method method = ownerClass. getMethod (methodName, argsClass );
12
13 return method. invoke (null, args );
14}
The basic principle is the same as that of instance 3. The difference is that the last line, one of the parameters of invoke is null, because this is a static method and does not need to be run by the instance.
5. Create an instance
1
2 public Object newInstance (String className, Object [] args) throws Exception {
3 Class newoneClass = Class. forName (className );
4
5 Class [] argsClass = new Class [args. length];
6
7 for (int I = 0, j = args. length; I <j; I ++ ){
8 argsClass [I] = args [I]. getClass ();
9}
10
11 Constructor cons = newoneClass. getConstructor (argsClass );
12
13 return cons. newInstance (args );
14
15}
The method described here is to execute a constructor with parameters to create an instance. If no parameter is required, you can use newoneClass. newInstance () directly.
Class newoneClass = Class. forName (className): the first step is to obtain the Class of the instance to be constructed.
5th ~ Row 9th: obtains the Class array of the parameter.
Constructor cons = newoneClass. getConstructor (argsClass): Obtain the Constructor.
Cons. newInstance (args): creates an instance.
6. Determine whether it is an instance of a class
1 public boolean isInstance (Object obj, Class cls ){
2 return cls. isInstance (obj );
3}
7. Obtain an element in the array.
1 public Object getByArray (Object array, int index ){
2 return Array. get (array, index );
3}
Complete source code:
Import java. lang. reflect. Array;
Import java. lang. reflect. Constructor;
Import java. lang. reflect. Field;
Import java. lang. reflect. Method;
/**
* Java Reflection Cookbook
*
* @ Author Michael Lee
* @ Since 2006-8-23
* @ Version 0.1a
*/
Public class Reflection {
/**
* Obtain the public attributes of an object.
*
* @ Param owner, fieldName
* @ Return this property object
* @ Throws Exception
*
*/
Public Object getProperty (Object owner, String fieldName) throws Exception {
Class ownerClass = owner. getClass ();
Field field = ownerClass. getField (fieldName );
Object property = field. get (owner );
Return property;
}
/**
* Obtain a static public attribute
*
* @ Param className Class Name
* @ Param fieldName: attribute name
* @ Return this property object
* @ Throws Exception
*/
Public Object getStaticProperty (String className, String fieldName)
Throws Exception {
Class ownerClass = Class. forName (className );
Field field = ownerClass. getField (fieldName );
Object property = field. get (ownerClass );
Return property;
}
/**
* Execute an object Method
*
* @ Param owner
* Object
* @ Param methodName
* Method name
* @ Param args
* Parameters
* @ Return method return Value
* @ Throws Exception
*/
Public Object invokeMethod (Object own