Java reflection mechanism and method.invoke

  • 2020-04-01 03:45:51
  • OfStack

JAVA reflection mechanism

JAVA reflection mechanism is in the running state, for any class, can know all the attributes and methods of the class; For any object, you can call any method of it. This ability to dynamically retrieve information and dynamically invoke methods on objects is called the Java language's reflection mechanism.

Java reflection mainly provides the ability to determine at runtime which class any object belongs to. Construct an object of any class at runtime. Determines at runtime which member variables and methods any class has; Methods that call any object at run time; Generate a dynamic proxy.

1. Get the properties of an object


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 () : gets the Class of the object.

Field Field = ownerclass.getfield (fieldName) : gets the property of the Class declaration by Class.

Object property = field.get(owner) : gets an instance of this property from the Object. If the property is private, an IllegalAccessException is reported.

2. Get static properties of a class


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) : as above, get the Class declared by Class.

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

Method to execute an object


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 () : you still have to get the Class of this object first.

Lines 5-9: configure the Class array of parameters as a condition for finding Method.

Method Method = ownerclass.getmethod (methodName, argsClass) : gets the Method to execute through the methodName and argsClass (set of parameter types in the Method) array of arguments.

Method. Invoke (owner, args) : the parameters to execute the method.invoke method are the object owner that executes the method, and the parameters array args. The return value is Object, which is also the return value of the method.

Execute a class's static methods


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 is the same as for instance 3, except that on the last line, one of the arguments to invoke is null, because this is a static method that does not need to be run with an instance.

5. Create a new instance


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 described here is a method that executes a constructor with arguments to create a new instance. If you don't need parameters, you can directly use newoneClass. NewInstance ().

Class newoneClass = class.forname (className) : first, get the Class of the instance to be constructed.

Lines 5 through 9: get the Class array of arguments.

Constructor cons = newoneClass. GetConstructor (argsClass) : get structure.

Cons. NewInstance (args) : newInstance.

6. Determine if it is an instance of a class


public boolean isInstance(Object obj, Class cls) { 
     return cls.isInstance(obj); 

7. Get an element in the array


public Object getByArray(Object array, int index) { 
     return Array.get(array,index); 


Related articles: