Detailed description of private fields and methods of Java reflection

  • 2020-04-01 01:08:51
  • OfStack

Although we generally think that it is possible to access private fields and methods of other classes through JAVA's reflection mechanism, it is not that difficult.
Note: It only works if you run the code in a separate JAVA program, like you do unit tests or regular programs. If you try to use this method in a JAVA APPLET, you'll need to modify the SecurityManager slightly. However, since you don't have to deal with it very often, I won't go into details here.
Here's the list:
1. Access private fields.
2. Access private methods.
Access to private fields:
To access private fields, you need to call the class.getdeclaredfield (String name) or the class.getdeclaredfields () method. The methods class.getfield (String name) and class.getfields () simply return common fields, so neither of them works. Here is an example of a class that contains a private field, under which there is code to access the private field through reflection.
 
public class PrivateObject { 
private String privateString = null; //Declare as private field
public PrivateObject(String privateString) { 
this.privateString = privateString; 
} 
} 

 
PrivateObject privateObject = new PrivateObject("The Private Value");//Instantiate object
Field privateStringField = PrivateObject.class. 
getDeclaredField("privateString"); 
privateStringField.setAccessible(true);//Allows access to private fields
String fieldValue = (String) privateStringField.get(privateObject);//Gets 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 the method we use 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, not the fields declared by the parent class.
Also look carefully at the bold statements. By calling field.setaccessible (true), you turn off the access check for this specified Field instance, only for reflection. Now that you can access it, whether it is private, protected, or default, the immediate caller is not in the scope. You still can't access this field in the normal way because the compiler won't allow it.
Accessing private methods
To access a private method, you need to call the class.getdeclaredmethod (String name,Class[] parameterTypes) or the 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 of a class with a private method, and below the class is the code that accesses the private method through the reflection mechanism.
 
public class PrivateObject { 
private String privateString = null; 
public PrivateObject(String privateString) { 
this.privateString = privateString; 
} 
private String getPrivateString(){//Private methods
return this.privateString; 
} 
} 

 
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 out The text "returnValue = The private Value", which happens to be The returnValue of The private method.

Related articles: