Java Reflection mechanism (access to private fields and private methods)

Source: Internet
Author: User

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
    1. Public class Privateobject {
    2. private String privatestring = null; //declared as Private field
    3. Public Privateobject (String privatestring) {
    4. this.privatestring = privatestring;
    5. }
    6. }

[Java]View PlainCopy
  1. Privateobject privateobject = new Privateobject ("The Private Value"); Instantiating an object
  2. Field Privatestringfield = Privateobject.  class.
  3. Getdeclaredfield ("privatestring");
  4. Privatestringfield.setaccessible (true); Allow access to private fields
  5. String fieldvalue = (string) privatestringfield.get (Privateobject); //Get private field values
  6. 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
  1. Public class Privateobject {
  2. private String privatestring = null;
  3. Public Privateobject (String privatestring) {
  4. this.privatestring = privatestring;
  5. }
  6. Private String getprivatestring () {//proprietary method
  7. return this.privatestring;
  8. }
  9. }

[Java]View PlainCopy
  1. Privateobject privateobject = new Privateobject ("The Private Value");
  2. Method Privatestringmethod = Privateobject.  class.
  3. Getdeclaredmethod ("getprivatestring", null);
  4. Privatestringmethod.setaccessible (true);
  5. String returnvalue = (string)
  6. Privatestringmethod.invoke (Privateobject, null);
  7. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.