Explain in detail the application of reflection in Java programming in Android development

Reflection definition

Reflection enables programs running in the JVM to detect and modify runtime behavior. Why reflection

The benefits of reflection include:

Reflection method getdeclaredmethod method

The statement is as follows:

Explanation:

Returns a method object that reflects the specified declared method of the class or interface represented by this class object. 1. Name: is a string that specifies the short name of the required method. 2. Parametertypes: is a variable length array of class objects, which identifies the formal parameter types of the method in the declared order.

Note: getdeclaraedmethod gets the public method or protected method declared by this class, but does not include inherited methods. Getmethod method

The statement is as follows:

Explanation:

Returns a method object that reflects the specified public member method of the class or interface represented by this class object. 1. Name: is a string that specifies the short name of the required method. 2. Parametertypes: is a variable length array of class objects, which identifies the formal parameter types of the method in the declared order.

Parameter interpretation

The name parameter does not need to be explained. It is the method name of the calling class.

When many students first came into contact with this method, they may have questions about the parametertypes parameter. For example, why this parameter is a class generic variable length array is well understood by taking an example.

Suppose that the method we want to reflect has four parameters, and the function prototype is as follows:

When we get the method object through return, the passed parametertypes are as follows:

Therefore, parametertypes is actually a type abstraction of method parameters. Invoke method

The statement is as follows:

Explanation:

The parameter received by the invoke (object obj, object... Args) method of the method class must be an object. Among them: 1. obj: the object of the underlying method is invoked from it. 2. Args: parameter for method call.

Android reflection app

We know that some Android classes are not open in the SDK. For example, you need to obtain system properties and call the get method of SystemProperties class, but this class is not open in the SDK. We can check this class in the Android source code:

As you can see, there is a @hide tag in front of it, so this class can not be called directly in the code.

However, in Android applications, we often need to get the phone type attribute (RO. Product. Model). Therefore, at this time, we need to reflect the SystemProperties class in the application layer and call the get method. The specific implementation source code is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>