Reprinted from Concurrent Programming network –ifeve.com
Content index
Accessing private variables
Accessing Private methods
It is not permissible to access private variables and methods from outside the object in a common view, but the Java reflection mechanism can do this. It is not difficult to use this feature, which is very effective in unit testing. This section will show you how to use this feature.
Note: This feature works only if the code is running in a standalone Java application (standalone Java application), just as you do unit testing or some regular application. If you use this feature in a Java applet, you'll have to find a way to cope with securitymanager restrictions. But in general we don't do that, so we won't discuss this in this section.
Accessing private variables
To get a private variable you can call the Class.getdeclaredfield (String name) method or the Class.getdeclaredfields () method. Class.getfield (String name) and Class.getfields () only return public variables and cannot get private variables. The following example defines a class that contains a private variable, and below it is an example of how to get a private variable by reflection:
Public classPrivateobject {PrivateString privatestring =NULL; PublicPrivateobject (String privatestring) { This. privatestring =privatestring; }} privateobject Privateobject=NewPrivateobject ("The Private Value"); Field Privatestringfield= Privateobject.class. Getdeclaredfield ("Privatestring");p rivatestringfield.setaccessible (true); String Fieldvalue=(String) privatestringfield.get (privateobject); System.out.println ("Fieldvalue =" + Fieldvalue);
This example outputs "Fieldvalue = the private value", the private value is the value of the privatestring private variable of the Privateobject instance, Note that calling the PrivateObject.class.getDeclaredField ("Privatestring") method returns a private variable, The variable returned by this method is a variable defined in the Privateobject class rather than in its parent class.
Note Privatestringfield.setaccessible (true) this line of code, by calling the Setaccessible () method, turns off the reflection access check for the specified class field instance, after which the line of code executes whether it is private, Protected as well as the scope of the package access, you can access it anywhere, even if you are not within the scope of his access rights. But if you use generic code to access the code that is not within the scope of your permission is still not possible, at compile time will be error.
Accessing Private methods
To access a private method you need to call Class.getdeclaredmethod (String name, class[] parametertypes) or the Class.getdeclaredmethods () method. The Class.getmethod (String name, class[] parametertypes) and the Class.getmethods () method only return the public method and cannot get the private method. The following example defines a class that contains a private method, and below it is an example of how to get a private method through reflection:
Public classPrivateobject {PrivateString privatestring =NULL; PublicPrivateobject (String privatestring) { This. privatestring =privatestring; } PrivateString getprivatestring () {return This. privatestring; }}privateobject Privateobject=NewPrivateobject ("The Private Value"); Method Privatestringmethod= Privateobject.class. Getdeclaredmethod ("Getprivatestring",NULL);p rivatestringmethod.setaccessible (true); String returnvalue=(String) Privatestringmethod.invoke (Privateobject,NULL); System.out.println ("returnvalue =" + returnvalue);
This example outputs "returnvalue = the private value", and the Private value is the return value of the Getprivatestring () method of the Privateobject instance.
The PrivateObject.class.getDeclaredMethod ("Privatestring") method returns a private method that is defined in the Privateobject class rather than in its parent class.
Similarly, note that method.setacessible (true) this line of code, by calling the Setaccessible () method, turns off the reflection access check for the method instance of the specified class, after which the line of code executes, whether private, protected, or scoped to the package access, You can access it anywhere, even if you are not within the scope of his access rights. But if you use generic code to access the code that is not within the scope of your permission is still not possible, at compile time will be error.
Original address Author: Jakob Jenkov Translator: Yevenhai ([email protected])
Java Reflection (vii): Private variables and private methods