Learning Notes-Reflection

Keywords: Java Attribute

The Concept and Function of Reflection

concept

Dynamic access to information and a reflection mechanism for dynamically invoking object methods. Class attributes, methods, and constructors can be dynamically retrieved while the program is running.

Effect

Objects can be created dynamically without importing the package of the class, and all the information in the class can be known only by knowing the complete path of the class.

 

Three Ways of Reflective Acquisition Class

1. Acquisition based on fully qualified paths

Class.forName("Route");

2. Acquisition by Object

Object name. getClass();

3. Get by class name

class name.

Note: A class has only one class object.

Common methods:

        System.out.println("Get the package name of the class object:" + c.getPackage());
        System.out.println("Get the modifier for the class:" + c.getModifiers());
        System.out.println("Get the name of the class (fully qualified):" + c.getName());
        System.out.println("Get the name of the class (class name):" + c.getSimpleName());
        System.out.println("Get the class object of the parent class of the class:" + c.getSuperclass());

 

Reflective Operational Properties

Gets all the common fields of this class and all its parent classes

// Gets all the common fields of this class and all its parent classes
Field[] fields = c.getFields();        
for(Field f : fields) {
    System.out.println("Get the property name:" + f.getName());
    System.out.println("Get modifiers" + f.getModifiers());
    System.out.println("Acquisition type" + f.getType());        //Returns type Class object
}

Get all fields of this type of declaration

        // Gets all fields of this class declaration
        Field[] fieldall = c.getDeclaredFields();
        for(Field f : fieldall) {
            System.out.println("Get the property name:" + f.getName());
            System.out.println("Get modifiers" + f.getModifiers());
            System.out.println("Acquisition type" + f.getType());        //Returns type Class object
        }

Gets the specified field

        // Specify the public fields of the retrieved class and its parent class
        Field field = c.getField("pname");    
        
        // Specify all fields of the retrieved class
        Field field2 = c.getDeclaredField("snull");        
        
        // Specify to get the parent class declaration field
        Field field3 = c.getSuperclass().getDeclaredField("pname");    

Operational class attributes

// Operating static properties
        Class attribute object. get(null) // Returns the value of the static attribute
        Class attribute object. set(null, "// assignment"
// Operating non-static properties
        Class attribute object. get(Object obj);
        Class attribute object. set(Object obj, "value");

 

Reflection operation method

Get all the common methods for this class and all its parent classes

        // Get all public methods (including parent classes)
        Method[] methods = c.getMethods();
        for(Method method:methods) {
            System.out.println(method.getName());
        }

Get all the methods for this type of declaration

        // Method to get all declarations (excluding parent classes)
        Method[] methodall= c.getDeclaredMethods();
        for(Method method:methodall) {
            System.out.println(method.getName());
        }

Gets the specified method

// Gets the specified public method
getMethod(String name, Class ... cla);

// Gets the specified method
getDeclaredMethod(String name,Class ... cla);

/**  parameter
  * String : name   Representation method name
  * Class ... cla Class object representing the type of parameter received by the method
  *
*/

Posted by ttroy on Sat, 30 Mar 2019 14:27:29 -0700