Java modifies the value of the private attribute in the class through the reflection mechanism. java private

Source: Internet
Author: User

Java modifies the value of the private attribute in the class through the reflection mechanism. java private

First, create a class that contains a private property:

class PrivateField{    private String username = "Jason";}

Modify the username value through the reflection mechanism:

// Create a Class Object PrivateField privateField = new PrivateField (); // obtain the object's ClassClass <?> ClassType = privateField. getClass (); // obtain the private Field field = classType. getDeclaredField ("username"); // sets the suppression access type check. Only in this way can the value corresponding to the Field of a specific class be obtained and set. Field. setAccessible (true); System. out. println (field. get (privateField); // set the value of the private field. set (privateField, "aaaaa"); System. out. println (field. get (privateField ));

I am the dividing line of tiantiao


The java reflection mechanism can call private methods. Does it damage JAVA's plug-ins?

This is a very noteworthy issue. Many people are skeptical about the strong metropolis of the reflection function when they are exposed to reflection, and feel that the encapsulation is seriously damaged. But what is encapsulation and security?
Encapsulation is to hide the specific implementation details and provide the functions as a whole to the external use of the class. That is to say, the public method can complete the functions of the class. When someone else uses this class, if a private method is called through reflection, it may not be able to implement the class function at all, or even cause errors. Therefore, calling a private method through reflection is useless, developers do not have to intentionally destroy encapsulated classes. From this point of view, encapsulation is not broken.
The so-called security, if it means to protect the implementation of the source code is not seen by others, it does not work. Source code can be easily obtained without reflection.
Therefore, I think the reflection mechanism only provides a powerful function that allows developers to implement some functions in addition to encapsulation according to specific needs. It is like nuclear technology. Although nuclear bombs are dangerous, nuclear power plants are still very useful (this metaphor does not seem very appropriate and will be applied soon ).

I have discussed this issue with my friends and I feel that the explanation is not very clear. Let's discuss it and see how it can be easily understood :)

How to assign a value to a private attribute in Java reflection when only the class name is known

Reflection capability
// Assume that the class is named A // instantiate Class A p = new A (); // obtain classClass c = p. getClass (); // obtain all fields of the class Field [] fields = c. getDeclaredFields (); // returns a value for (int I = 0; I <fields. length; I ++) {String filedName = fields [I]. getName (); // The member variable in the AccessibleTest class is private, so you must perform this operation fields [I]. setAccessible (true); // Class of the judgment type <?> Type = fields [I]. getType (); // obtain the field type String typeName = type. getName (); System. out. println (type. getName (); // assign values to fields. The first parameter is the object reference. The second parameter is the value to be attached. // if it is a string type, if ("java. lang. string ". equals (typeName) {fields [I]. set (p, "1");} // if the date type is else if ("java. util. date ". equals (typeName) {fields [I]. set (p, new Date ();} else {fields [I]. set (p, 1) ;}/// I just wrote it down. You may also need to determine the field name and other types.

Finally, I hope you can take a look at the java documentation.


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.