Although we generally think that it is feasible to access private fields and private methods of other classes through the reflection mechanism of Java, it is not so difficult.
Note:It works only if you run the code in a separate Java program, just as you do some unit tests or regular programs. If you try to use this method within the Java applet, you need to modify the SecurityManager slightly. But, because you don't often need to deal with it, there's no longer much to repeat here.
Here is a list of the contents of this session:
1. Access private fields.
2. Access to private methods.
To access a private field:
In order to access the private field, you need to call Class.getdeclaredfield (String name) or the Class.getdeclaredfields () method. Method Class.getfield (String name) and Class.getfields () only return common fields, so they do not work. Here's an example of a class that contains a private field that has code to access the private field by reflection.
Copy Code code as follows:
public class Privateobject {
Private String privatestring = null; Declare as Private field
Public Privateobject (String privatestring) {
this.privatestring = privatestring;
}
}
Copy Code code as follows:
Privateobject privateobject = new Privateobject ("The Private Value");//Instantiate Object
Field Privatestringfield = Privateobject.class.
Getdeclaredfield ("privatestring");
Privatestringfield.setaccessible (TRUE);//Allow access to private fields
String fieldvalue = (string) privatestringfield.get (privateobject);//Get private field value
System.out.println ("Fieldvalue =" + Fieldvalue);
This code prints out the text "Fieldvalue = The Private value", which is exactly the value of the private field privatestring of the object privateobject.
Note that we used the method PrivateObject.class.getDeclaredfield ("Privatestring"). It is this calling method that returns the private field. This method returns a field only based on the specified class, and does not return the field declared by the parent class.
In addition, carefully observe the bold statement. by calling Field.setaccessible (true), you turn off the access check for this specified field instance, which is only valid for reflection. Now you can access it, whether it's private, protected, or default, and the immediate caller is not in that range. You still cannot access the field through the normal method because the compiler does not allow it.
Accessing Private methods
In order to access a private method, you need to call 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 that has a private method, and the following class is the code that accesses the private method through the reflection mechanism.
Copy 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 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 out the text "returnvalue = The Private value", which is exactly the return value of the private method.