Reflection definition
"Reflection" allows programs running in the JVM to detect and modify runtime behavior.
Why do you need reflection
The benefits of reflection include:
- Detect the object type at runtime.
- Dynamically construct objects of a certain class.
- Detect the properties and methods of the class.
- Methods that call objects arbitrarily.
- Modify the visibility of constructors, methods, and attributes.
Reflection method Method
getDeclaredMethod method
The statement is as follows:
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
explain:
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 abbreviation of the required method.
2. parameterTypes: is a variable-length array of Class objects that identifies the formal parameter type of the method in the order of declaration.
Notice:
getDeclaredMethod gets the public method or protected method declared by this class, but does not include inherited methods.
getMethod method
The statement is as follows:
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
explain:
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 abbreviation of the required method.
2. parameterTypes: is a variable-length array of Class objects that identifies the formal parameter type of the method in the order of declaration.
Parameter explanation
The name parameter does not need to be explained, it is the method name of the calling class.
Many students may have questions about the parameterTypes parameter when they first come into contact with this method. For example, why is this parameter a Class generic variable-length array? In fact, it is easy to understand with an example.
Suppose the method we want to reflect has 4 parameters, and the function prototype is as follows:
public void printInfo(String str, int iNum, double dNum, long i);
When we get this Method object by returning, the parameterTypes passed is as follows:
getMethod("printInfo", , , , );
Therefore, parameterTypes is actually a type abstraction of method formal parameters.
invoke method
The statement is as follows:
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
explain:
The parameters received by the method invoke(Object obj, Object… args) method must be an object. in:
1. obj: The object from which the underlying method is called.
2. args: Parameters used for method calls.
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 the SystemProperties class, but this class is not exposed in the SDK. We can check this class in the Android source code:
package ; import ; import ; /** * Gives access to the system properties store. The system properties * store contains a list of string key-value pairs. * * {@hide} */ public class SystemProperties { //Omit specific implementation code /** * Get the value for the given key. * @return an empty string if the key isn't found * @throws IllegalArgumentException if the key exceeds 32 characters */ public static String get(String key) { if (() > PROP_NAME_MAX) { throw new IllegalArgumentException(" > " + PROP_NAME_MAX); } return native_get(key); } }
As you can see, there is a @hide tag in front of this, so this class cannot be called directly in the code.
However, in Android applications, we often need to obtain the mobile phone type attribute (). So, at this time, we need to reflect the SystemProperties class at the application layer and call the get method. The specific implementation source code is as follows:
import ; import ; import ; public class SystemProperties { public static String get(String key) { String value = ""; Class<?> cls = null; try { cls = (""); Method hideMethod = ("get", ); Object object = (); value = (String) (object, key); } catch (ClassNotFoundException e) { ("", "get error() ", e); } catch (NoSuchMethodException e) { ("", "get error() ", e); } catch (InstantiationException e) { ("", "get error() ", e); } catch (IllegalAccessException e) { ("", "get error() ", e); } catch (IllegalArgumentException e) { ("", "get error() ", e); } catch (InvocationTargetException e) { ("", "get error() ", e); } return value; } }