Java reflection modifies the value of a static method setaccessible

Source: Internet
Author: User
There is nothing to do these days. Read a topic on the Internet, I believe we all know the topic
static void Change (String str) {
Str= "Welcome";
}

public static void Main (string[] args) {
String str = "123";
Change (str);
System.out.println (str);
}
str = how much.
If we all know that.
Then I want to put the final output to "welcome" how to do.
So I changed it.
static string change (String str) {
Str= "Welcome";
return str;
}
Then main str=change (str); The result was changed, but there were too many changes in the code. 、
I think I can only change the method. You do not need to return a value to solve the problem. First looked at the next string class found private final char value[]; is private. Just change the value. Then think of the reflection:
The code is as follows:
static void Change (String str) {
try {
Class<?> clazz = Str.getclass ();
Field fields = Clazz.getdeclaredfield ("value");
Object obj = fields.get (str);
char [] charvalue = (char []) obj;
System.out.println (Charvalue);
catch (Exception e) {
E.printstacktrace ();
}
}
Find out the results welcome so just get rid of charvalue OK. Then continue to add code found modifiers "private final" will make this mistake,
To add field.setaccessible (true); try {
Class<?> clazz = Str.getclass ();
Field field = Clazz.getdeclaredfield ("value");
Field.setaccessible (TRUE);
Object obj = field.get (str);
char [] charvalue = (char []) obj;
charvalue = new char [3];
for (int i=0;i<charvalue.length;i++) {
Charvalue[i]= ' a ';
}
Field.set (str, charvalue);
catch (Exception e) {
E.printstacktrace ();
}
Found to be able to meet my requirements. The value was changed, and then I wanted to change the length, so I moved the code charvalue = new Char [4];
Found value cannot be changed continue to see string source code, found that there is a Count property. And then changed the value of the code as follows

try {
Class<?> clazz = Str.getclass ();
Field field = Clazz.getdeclaredfield ("value");
Field.setaccessible (TRUE);
Object obj = field.get (str);
char [] charvalue = (char []) obj;
charvalue = new char [4];
for (int i=0;i<charvalue.length;i++) {
Charvalue[i]= ' a ';
}
Field field1 = Clazz.getdeclaredfield ("Count");
Field1.setaccessible (TRUE);
Field1.set (str, charvalue.length);
Field.set (str, charvalue);
catch (Exception e) {
E.printstacktrace ();
}
Found to be able to solve the problem.
I tried it again, and then I tried the integer discovery.
The code is as follows:
static void Changeinteger (Integer a) {
try {
Class<?> clazz = A.getclass ();
Field field = Clazz.getdeclaredfield ("value");
Field.setaccessible (TRUE);
Field.set (A, 12312);
catch (Exception e) {
E.printstacktrace ();
}
Personally feel as long as field.setaccessible (true); After that, even the final keyword indicates that the attribute can have access rights. This reflection will change the structure of Java, even your code maintainability, you can completely
Change the value in the other code, so this is just a simple example. Just verify that you can do something that you can't imagine by reflection.
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.