Although we usually think that it is feasible to access private fields and private methods of other classes through JAVA's reflection mechanism, it is not that difficult.
Note: This code will only work if you run it in a separate JAVA program, like you do some unit tests or a regular program. If you try to use this method inside a JAVA APPLET, you need to modify the SecurityManager slightly. However, since you don’t often need to deal with it, I won’t go into details here.
Here is a list of this content:
1. Access private fields.
2. Access private methods.
Access private fields:
In order to access private fields, you need to call the Class.getDeclaredField(String name) or Class.getDeclaredFields() method. The methods Class.getField(String name) and Class.getFields() only return common fields, so they have no effect. Here is an example of a class containing private fields, and underneath the class there is code to access the private fields through reflection.
Copy the code code as follows:
public class PrivateObject {
private String privateString = null; //Declared as a private field
public PrivateObject(String privateString) {
this.privateString = privateString;
}
}
Copy the code code as follows:
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);//Get the private field value
System.out.println("fieldValue = " + fieldValue);
This code will print out the text "fieldValue = The Private Value", which happens to be the value of the private field privateString of the object PrivateObject.
Notice that we used the method PrivateObject.class.getDeclaredfield("privateString"). It is this call to this method that returns the private field. This method only returns fields based on the specified class, and will not return fields declared by the parent class.
Also look carefully at the bolded statements. By calling Field.setAccessible(true), you turn off access checking for this specified field instance, which is only effective for reflection. Now you can access it, whether it is private, protected or default, even if the caller is not in that scope. You still can't access the field via regular methods because the compiler doesn't allow it.
Accessing private methods <BR>In order to access a private method, you need to call the Class.getDeclaredMethod(String name,Class[] parameterTypes) or Class.getDeclaredMethods() method. The methods Class.getMethod(String name,Class[] parameterTypes) and Class.getMethods() only return public methods, so they will not play any role. Below is a simple example of a class with private methods. Below the class is the code that accesses the private methods through reflection.
Copy the code code 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 the code code 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 happens to be the return value of the private method.