Java reflection summary

catalogue

1, Overview

2, Reflection and encapsulation

3, Java Understanding of lang. class

1. How to get an instance of Class

2.Class can represent not only classes, but also other types it learns.  

4, Create an object of the corresponding runtime class through reflection.

1.newInstance(); Call this method to create the object of the corresponding runtime class. Example:

2. Dynamics of reflection

5, Gets the complete structure of the runtime class

1. Get the property structure of the current runtime class

2. Get the method structure of the runtime class

3. Get the constructor of the runtime class

4. Get the parent class of the runtime class

5. Get the implementation interface of the runtime class

6. Get the package where the runtime class is located

7. Get the annotation of runtime class declaration

VII Calls the specified structure of the runtime class

1. Call the specified structure

2. Operate the method specified in the runtime class

3. Specified constructor

1, Overview

Reflection is the key to being regarded as a dynamic language. Reflection mechanism allows programs to obtain the internal information of any class with the help of Reflection API during execution, and can directly operate the internal properties and methods of any object.

2, Reflection and encapsulation

1. Through reflection, you can call private constructors, private methods and private fields in the class; You can also call non private structures. Use reflection in dynamic calls.

2. Contradiction between reflection and packaging

No contradiction, the original intention of reflection is to call methods that are public in the class and are in the runtime class.

3, Java Understanding of lang. class

The class loaded into memory is called the runtime class. This runtime class is an instance of class. Class corresponds to a runtime class.

1. How to get an instance of Class

① Method 1: call the name of the runtime class class

Example:

Class clazz = Person.class;

② Method 2: call getClass() through the object of the runtime class

Class clazz = P1.getClass;

③ Method 3: call the static method of Class: forName(String classPath)

Class clazz3 = Class.forName("com.ALi.java.Person");

④ Method 4: use classloader (understand)

ClassLoader classloader = ReflectionTest.Class.getClassLoader();

classloader.loadClass("Class Path");

2.Class can represent not only classes, but also other types it learns.  

The element types and dimensions in the array are the same, and their Class instances are equal.

4, Create an object of the corresponding runtime class through reflection.

1.newInstance(); Call this method to create the object of the corresponding runtime class.
Example:

Class clazz = Person.class;

Person obj = clazz.newInstance();

newInstance():

① The null parameter constructor of the runtime class is called internally.

② The minimum access permission of the null parameter constructor is the default, which is usually set to public.

A public null parameter constructor is required in javaBean. Reason:

        1. It is convenient to create objects of runtime classes through reflection

        2. When a subclass inherits this runtime class, it is guaranteed that the parent class has this constructor when super() is called by default.

2. Dynamics of reflection

Create the required objects when the code runs.

5, Gets the complete structure of the runtime class

1. Get the property structure of the current runtime class

Class clazz = Person.class();

Field[] f = clazz.getFields[];

① Gets the property declared as public access permission in the current runtime class and its parent class.

clazz.getDeclaredFields();

Return Field [] array

② Get all the properties declared in the current runtime class (properties with all permissions), excluding parent class properties.

③ Get property permissions getModifiers()

Field[] fs = clazz.getDeclaredFields();
for(Field f :fs){
    int modifier = f.getModifiers();
    Modifier.toString(modifier);//Convert int number to corresponding permission
}

④ Get property type: getType()

class type = f.getType();

⑤ Get variable name: getName()

String name = f.getName();

2. Get the method structure of the runtime class

① Get all public methods in the parent class

getMethods();

The return value is Method []

② Gets all declared methods of the current runtime class, excluding those declared in the parent class

getDeclaredMethods();

③ Gets the annotation of the method

getAnnotations();

for(Method m:declaredMethods){
    Annotation[] annos = m.getAnnotations();
    for(Annotation a:annos){
        System.out.println(a);
    }
}

④ Get permission modifier

getModifiers();

Return as int

Modifier. ToString (permission modifier of type int);

⑤ Gets the return value type of the method

m.getReturnType().getName();

⑥ Get method name

m.getName();

⑦ Get parameter list

m.getParameterTypes();

⑧ Gets the exception thrown

m.getExceptionTypes();

3. Get the constructor of the runtime class

① Gets the constructor declared public in the current runtime class

clazz.getConstructors();

Return array

② Gets all constructors declared in the current runtime class

clazz.getDeclaredConstructors(); 

4. Get the parent class of the runtime class

①clazz.getSuperclass(); Return Class type

② Get parent class with generics

Type superclass = clazz.getGenericSuperclass();

③ Gets the generic type of the parent class with the generic type

Type s = clazz.getGenericSuperclass();
ParameterizedType p = (ParameterizedType)s;
Type[] ac = p.getActualTypeArguments();
ac[0].getTypeName();

5. Get the implementation interface of the runtime class

class clazz = Person.class;
Class[] interfaces = clazz.getInterface();

② Gets the interface implemented by the parent class

Class[] interface = clazz.getSuperclass().getInterface();

6. Get the package where the runtime class is located

Package ap = clazz.getPackage();

7. Get the annotation of runtime class declaration

Annotation a[] = clazz.getAnnotations();

VII Calls the specified structure of the runtime class

1. Call the specified structure

① Specified attribute: the attribute is publi.

Field id = clazz.getDield("property name")// This method is usually not used.

Mode 2:

Person p = (Person)clazz.newInstance();

Field name = clazz.getDeclaredFiedld("Attribute name");
name.setAccessible(true);
name.set(p,New value);
name.get(p);

② Sets the value of the current property

id.set(p,100);

Parameter p is the object; Parameter 100 is set to this property.

③ Gets the value of the current property

int PID = (int) id.get(p);

2. Operate the method specified in the runtime class

Class clazz = Person.class();
Person p = (Person)clazz.newInstance();
//Gets a specified method
Method show = clazz.getDeclaredMethod("Method name",Parameter class);
show.setAccessible(true);

② Method name Invoke (object, parameter);

Parameter 1: object calling method

Parameter 2: argument

show.invoke(p,"CHN"); Returns the return value of the show method

In a static method, p can be written to null

3. Specified constructor

Class clazz = Person.class;

Constructor cc = clazz.getDeclaredConstructor(String.class);//Gets the specified constructor. The parameter is a formal parameter type.

//Ensure that this constructor is accessible

cc.setAccessible(true);

//The object that calls this constructor runtime class

person p = cc.newInstance("Tom");//Tom is a parameter in a parameterized constructor

Keywords: Java

Added by AstroTeg on Sat, 15 Jan 2022 16:07:30 +0200