Java – what is the purpose of using reflection to modify strings?

I'm reading article, which says that Java strings are not completely immutable However, in the article's sample code for modifying strings, it calls string toUpperCase(). Tochararray(), which returns a new string So, if you call touppercase (), what is the purpose of changing the string? This is the code:

public static void toUpperCase(String orig)
{
 try
 {
    Field stringValue = String.class.getDeclaredField("value");
    stringValue.setAccessible(true);
    stringValue.set(orig,orig.toUpperCase().tocharArray());
 }
 catch (Exception ex){}
}

Also, I noticed that string Touppercase() does not work by itself It needs to be a string toUpperCase(). tocharArray(). Is there a reason?

Solution

What is he doing:

He is taking some character arrays (such as the uppercase version of string) that he knows the correct length and using them as a backup array of string (arrays are called values in the string class.)

Why did he do this:

For illustration, you can put any char array there

Why is this useful:

Strings are immutable, which allows you to circumvent invariance Of course, this is not recommended – always On the contrary, I wouldn't be surprised if he said, "please note, because people may do this to your code. Even if you think your stuff is safe, it may not be!"

This influence is widespread Immutable variables are no longer immutable The final variable is no longer final Thread - safe objects are no longer thread - safe You can't rely on the contract you think you can rely on All this is because an engineer has a problem somewhere and he can't repair it by normal means, so he goes deep into studying and solving the problem Don't 'that guy'

You will also notice how to change the hashcode of the string now Therefore, if you have never calculated the hashcode of the string, it is still 0, so you are fine On the other hand, if you have calculated it, it will not be retrieved when you put it in HashMap or HashSet

Consider the following:

import java.util.*;
import java.lang.reflect.*;

class HashTest {        

    /** Results:
     C:\Documents and Settings\glowcoder\My Documents>java HashTest
        Orig hash: -804322678
        New value: STACKOVERFLOW
        Contains orig: true
        Contains copy: false
     */

    public static void main(String[] args) throws Exception {

        Set<String> set = new HashSet<String>();
        String str = "StackOverflow";
        System.out.println("Orig hash: " + str.hashCode());
        set.add(str);

        Field stringValue = String.class.getDeclaredField("value");
        stringValue.setAccessible(true);
        stringValue.set(str,str.toUpperCase().tocharArray()); // 

        System.out.println("New value: " + str);

        String copy = new String(str); // force a copy
        System.out.println("Contains orig: " + set.contains(str));
        System.out.println("Contains copy: " + set.contains(copy));
    }

}

I bet he did it to warn against bad behavior, not to show "cool" tricks

Editor: I found the article you refer to and the article on which it is based The original article pointed out: "this means that if a class in another package" fiddles with "an internship string, it will cause serious damage to your program. Is this a good thing? (you don't need to answer; -)" I think it's clear that this is a protection guide rather than a suggestion on how to encode

Therefore, if you leave this thread with only one message, reflection is dangerous, unreliable, and should not be underestimated!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>