The reflection of Java basic completion series

Basic use of reflection

Java's reflection mechanism is at runtime, for any class, you can know all the properties and methods of the reclassification; for any object, you can call any of its methods and properties; you can get it by the string name of a class The information of this class is called according to the name of the method. This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.

The reflection of Java is mainly the following categories:

Class name effect
Class A class corresponding to Java
Method A method corresponding to the Java class
Field Corresponds to a field of the Java class
Constructor Corresponds to a construction method of the Java class

Here we look at the acquisition method of these four objects:

//Class对象的获取
Class aClass1 = String.class;
Class aClass2 = "".getClass();
try {
    Class aClass3 = Class.forName("java.lang.String");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

After we get the Class object, we can get three other objects through the method of the Class object:

Method[] methods = aClass1.getMethods();
Method[] methods2 = aClass1.getDeclaredMethods();

Field[] fields = aClass1.getFields();
Field[] fields2 = aClass1.getDeclaredFields();

Constructor[] constructors = aClass1.getConstructors();
Constructor[] constructors2 = aClass1.getDeclaredConstructors();

Declared means to get all methods of the current class, including private methods, but not its parent class, without this parameter means to get all public methods of the class, including public methods of its parent class.

Then we can use some methods on these four objects to achieve the purpose of creating objects, calling methods, obtaining properties and so on.

Below we introduce several application scenarios of reflection.

Call system private methods
try {
    Field field = Person.class.getDeclaredField("name");
    field.setAccessible(true);

    String name = (String) field.get(new Person("Tony",24));
    System.out.println(name);

} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}

After we obtain the reflection object corresponding to the private property or private field, we can bypass the system permission check by calling the setAccessible method to achieve the purpose of calling the private field or private method.

The reflection call does not exist in the current compilation environment, but there are classes in the specific operating environment

When programming in Java, there are some classes that we do not have in the current compilation environment, but there is this class in the specific operating environment, then of course we ca n’t call it with the conventional method, but we can create that class through reflection and call The methods and fields in it may be used in Android development if you use middleware technology (using self-developed middleware to communicate between the system and app, and finally middleware will be packaged into the system). But the most common way is to expose the interface that will be used by the client in the middleware and put it on the client for calling.

发布了19 篇原创文章 · 获赞 8 · 访问量 4041

Guess you like

Origin blog.csdn.net/u014068277/article/details/102887652