getMethod method.invoke

JAVA reflection mechanism

The JAVA reflection mechanism is that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any of its methods; this dynamically obtained information And the function of dynamically calling methods of objects is called the reflection mechanism of the Java language.
The Java reflection mechanism mainly provides the following functions: Determine the class to which any object belongs at runtime; Construct an object of any class at runtime; Determine the members of any class at runtime Variables and methods; call methods of any object at runtime; generate dynamic proxies.
1. Get the attributes of an object

Java code   
  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. }  
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;
}

 
Class ownerClass = owner.getClass(): Get the Class of the object.

Field field = ownerClass.getField(fieldName): Get the attributes declared by the class through Class.

Object property = field.get(owner): Get the instance of the property through the object. If this property is non-public, an IllegalAccessException will be reported here.

2. Get the static attributes of a certain class

Java code Copy code  favorite code
  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. }  
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;
}

 

Class ownerClass = Class.forName(className): First get the Class of this class.

Field field = ownerClass.getField(fieldName): Same as above, get the attributes declared by the class through Class.

Object property = field.get(ownerClass): This is somewhat different from the above, because this property is static, so it is taken directly from the Class of the class.

3. Execute the method of an object

Java code Copy code  favorite code
  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. }  
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
 
     Class ownerClass = owner.getClass();
 
     Class[] argsClass = new Class[args.length];
 
     for (int i = 0, j = args.length; i < j; i++) {
         argsClass[i] = args[i].getClass();
     }

      Method method = ownerClass.getMethod(methodName,argsClass);
 
     return method.invoke(owner, args);
}

 
Class owner_class = owner.getClass(): First, you must get the Class of this object.

Lines 5 to 9: Class array of configuration parameters, used as conditions for finding Method.

Method method = ownerClass.getMethod(methodName, argsClass): Get the Method to be executed through the methodName and the argsClass (parameter type collection in the method) array of parameters.

method.invoke(owner, args): The parameters for executing the Method.invoke method are the object owner that executes this method, and the parameter array args. It can be understood like this: the owner object has parameters args method. The return value is Object, which is also the return value of this method.

4. Execute the static method of a certain class

Java code Copy code  favorite code
  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.  }  
public Object invokeStaticMethod(String className, String methodName,
             Object[] args) throws Exception {
     Class ownerClass = Class.forName(className);
 
     Class[] argsClass = new Class[args.length];
 
     for (int i = 0, j = args.length; i < j; i++) {
         argsClass[i] = args[i].getClass();
     }
 
    Method method = ownerClass.getMethod(methodName,argsClass);
 
     return method.invoke(null, args);
 }

 

The basic principle of is the same as Example 3. The difference is that in the last line, one parameter of invoke is null, because this is a static method and does not need to be run with the help of an instance.

5. Create a new instance

Java code Copy code  favorite code
  1. public Object newInstance(String className, Object[] args) throws Exception {   
  2.      Class newoneClass = Class.forName(className);   
  3.     
  4.      Class[] argsClass = new Class[args.length];   
  5.     
  6.      for (int i = 0, j = args.length; i < j; i++) {   
  7.          argsClass[i] = args[i].getClass();   
  8.      }   
  9.     
  10.      Constructor cons = newoneClass.getConstructor(argsClass);   
  11.     
  12.      return cons.newInstance(args);   
  13.     
  14. }  
public Object newInstance(String className, Object[] args) throws Exception {
     Class newoneClass = Class.forName(className);
 
     Class[] argsClass = new Class[args.length];
 
     for (int i = 0, j = args.length; i < j; i++) {
         argsClass[i] = args[i].getClass();
     }
 
     Constructor cons = newoneClass.getConstructor(argsClass);
 
     return cons.newInstance(args);
 
}

 
The method mentioned here is to execute the constructor with parameters to create a new instance. If no parameters are required, you can use newoneClass.newInstance() directly.

Class newoneClass = Class.forName(className): The first step is to get the Class of the instance to be constructed.

Lines 5 to 9: Get the Class array of parameters.

Constructor cons = newoneClass.getConstructor(argsClass): Get the constructor.

cons.newInstance(args): Create a new instance.

6. Determine whether it is an instance of a certain class

Java code Copy code  favorite code
  1. public boolean isInstance(Object obj, Class cls) {   
  2.      return cls.isInstance(obj);   
  3. }  
public boolean isInstance(Object obj, Class cls) {
     return cls.isInstance(obj);
}

 
7. Get an element in the array

Java code Copy code  favorite code
  1. public Object getByArray(Object array, int index) {   
  2.      return Array.get(array,index);   
  3. }  

Guess you like

Origin blog.csdn.net/SSSU/article/details/83954234