From: http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html
Although we generally think that it is possible to access private fields and private methods of other classes through the reflection mechanism of Java, it is not so difficult.
Note: It is only valid to run the code in a separate Java program, as if you were doing some unit testing or a regular program. If you try to use this method within a Java applet, you need to modify the SecurityManager slightly. But because you don't always have to deal with it, you don't have to go into it.
Here is a list of the contents of this session:
1. Access private fields.
2. Access the private method.
To access private fields:
In order to access the private fields, you need to call Class.getdeclaredfield (String name) or the Class.getdeclaredfields () method. Method Class.getfield (String name) and Class.getfields () only return the fields that are common, so they do not work . Here is an example of a class with a private field, under which there is code to access the private field through reflection.
[Java]View PlainCopy
- Public class Privateobject {
- private String privatestring = null; //declared as Private field
- Public Privateobject (String privatestring) {
- this.privatestring = privatestring;
- }
- }
[Java]View PlainCopy
- Privateobject privateobject = new Privateobject ("The Private Value"); Instantiating an object
- Field Privatestringfield = Privateobject. class.
- Getdeclaredfield ("privatestring");
- Privatestringfield.setaccessible (true); Allow access to private fields
- String fieldvalue = (string) privatestringfield.get (Privateobject); //Get private field values
- System.out.println ("fieldvalue =" + Fieldvalue);
This code prints out the text "Fieldvalue = The Private value", which is exactly the value privatestring the private field of the object Privateobject.
Notice that we used the method PrivateObject.class.getDeclaredfield ("Privatestring"). It is this call to this method that returns a private field. This method returns a field based only on the specified class and does not return the field declared by the parent class.
Also 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 that you can access it, whether it's private, protected, or default, the immediate caller is not in that range. You still cannot access the field by using the usual 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 class.getdeclaredmethods () Method. Method Class.getmethod (String name,class[] parametertypes) and Class.getmethods () only return public methods , so they do not work. Here is a simple example in which there is a class with a private method, and the following is the code that accesses the private method through the reflection mechanism.
[Java]View PlainCopy
- Public class Privateobject {
- private String privatestring = null;
- Public Privateobject (String privatestring) {
- this.privatestring = privatestring;
- }
- Private String getprivatestring () {//proprietary method
- return this.privatestring;
- }
- }
[Java]View PlainCopy
- 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.
Java Reflection mechanism (access to private fields and private methods)