Java – string text, practice and reflection

I tried to find a third solution this question

I don't understand why I don't print errors

public class MyClass {

    public MyClass() {
        try {
            Field f = String.class.getDeclaredField("value");
            f.setAccessible(true);
            f.set("true",f.get("false"));
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        MyClass m = new MyClass();
        System.out.println(m.equals(m));
    }
}

Of course, due to the string practice, the modified "real" instance is exactly the same as that used in the printing method of printstream?

public void print(boolean b) {
    write(b ? "true" : "false");
}

I'm missing what

edit

@An interesting thing about yshavit is that if you add rows

System.out.println(true);

Before attempting, the output is

true
false

Solution

This can be said to be a hotspot JVM error

The problem is the string literal mechanism

>In the constant pool parsing process, string literal Java Lang. string instance is lazily created. > Initially, the string literal is in constant_ String_ Info structure points to constant_ Utf8_ Info in the constant pool. > Each class has its own constant pool That is, MyClass and printstream have their own pair of constants for the text "true"_ String_ info / CONSTANT_ Utf8_ Info cpool entry. > When you access constant for the first time_ String_ Info, the JVM starts the parsing process String practice is part of this process. > To find a match in which the text is matched, the JVM will confirm_ Utf8_ Compare the contents of info with those of string instances in stringtable. >^^^ This is the problem Compare the raw UTF data from cpool with the contents of Java char [] array that can be spoofed through reflection

So, what happened to your test?

>F.set ("true", f.get ("false") starts the parsing of the text "true" in MyClass. > The JVM finds that there is no instance matching the sequence 'true' in the stringtable and creates a new Java Lang. string, which is stored in stringtable. > String the value from the value of stringtable will be replaced by reflection. > System. out. Println (true) starts the parsing of the text "true" in the printstream class. > The JVM compares the UTF sequence 'true' with the string in the stringtable, but finds no match because the string already has a value of 'false' Another "true" string is created and placed in a stringtable

Why do I think this is a mistake?

JLS §3.10. 5 and JVMs § 5.1 require that string text containing the same character sequence must point to Java Lang. string is the same instance

However, in the following code, the resolution of two string literals with the same character sequence will lead to different instances

public class Test {

    static class Inner {
        static String trueLiteral = "true";
    }

    public static void main(String[] args) throws Exception {
        Field f = String.class.getDeclaredField("value");
        f.setAccessible(true);
        f.set("true",f.get("false"));

        if ("true" == Inner.trueLiteral) {
            System.out.println("OK");
        } else {
            System.out.println("BUG!");
        }
    }
}

A possible fix for the JVM is to put a pointer to the original UTF sequence in the stringtable and Java Lang. string objects are stored together, so the actual process does not compare cpool data (which is inaccessible to the user) with the value array (which can be accessed through reflection)

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
分享
二维码
< <上一篇
下一篇>>