Although we usually think that using the JAVA reflection mechanism to access private fields and private methods of other classes is feasible, it is not that difficult.
Note:This code is valid only when it is run in a separate JAVA program, just like you do some unit tests or regular programs. If you try to use this method in a java applet, You Need To slightly modify SecurityManager. However, because you do not often need to deal with it, I will not go into details here.
Here is the list of the content:
1. access private fields.
2. Access the private method.
Access private fields:
To access private fields, you must call the Class. getDeclaredField (String name) or Class. getDeclaredFields () method. Methods Class. getField (String name) and Class. getFields () only return the common fields, so they do not work. Here is an example. In this example, there is a class containing private fields, and there is code for accessing private fields through reflection under this class.
Copy codeThe Code is as follows: public class PrivateObject {
Private String privateString = null; // declared as a private field
Public PrivateObject (String privateString ){
This. privateString = privateString;
}
}
Copy codeCode: PrivateObject privateObject = new PrivateObject ("The Private Value"); // instantiate The object
Field privateStringField = PrivateObject. class.
GetDeclaredField ("privateString ");
PrivateStringField. setAccessible (true); // allow access to private fields
String fieldValue = (String) privateStringField. get (privateObject); // obtain the private field value
System. out. println ("fieldValue =" + fieldValue );
This code prints The text "fieldValue = The Private Value", which is The Value of The Private field privateString of The object PrivateObject.
Note that the PrivateObject. class. getDeclaredfield ("privateString") method is used "). This method returns a private field. This method only returns fields based on the specified class and does not return fields declared by the parent class.
In addition, carefully observe the bold statements. By calling Field. setAccessible (true), you disable the access check for this specified Field instance, which is only valid for reflection. Now you can access it. Whether it is private, protected, or default, instant callers are not in this range. You still cannot access this field through conventional methods because the compiler does not allow this field.
Access private methods
To access a private method, you need to call the Class. getDeclaredMethod (String name, Class [] parameterTypes) or Class. getDeclaredMethods () method. Methods Class. getMethod (String name, Class [] parameterTypes) and Class. getMethods () only return public methods, so they do not work. The following is a simple example. In this example, there is a class with private methods. The code for accessing private methods through reflection is as follows.Copy codeThe Code is as follows: public class PrivateObject {
Private String privateString = null;
Public PrivateObject (String privateString ){
This. privateString = privateString;
}
Private String getPrivateString () {// private Method
Return this. privateString;
}
}
Copy codeThe Code is as follows: PrivateObject privateObject = new PrivateObject ("The Private Value ");
Method privateStringMethod = PrivateObject. class.
GetDeclaredMethod ("getPrivateString", null );
PrivateStringMethod. setAccessible (true );
String returnValue = (String)
PrivateStringMethod. invoke (privateObject, null );
System. out. println ("returnValue =" + returnValue );
This code example prints The text "returnValue = The private Value", which is exactly The return Value of The private method.