The value field is a char[] that internally stores an array of characters that the string uses as storage. Other fields indicate the initial offset into the character array and the length of the string. (To take a substring, it simply creates a new String object that references the same char[] , but with a different starting offset and length.)
If you change these fields, you can do almost anything you want with a string. Example:
import java.lang.reflect.Field; public class Test { // Obviously in real code you wouldn't use Exception like this... // Although hacking string values with reflection is worse, of course. public static void main(String[] args) throws Exception { System.out.println("Hello World"); replaceHelloWorld("Goodbye!"); System.out.println("Hello World"); replaceHelloWorld("Hello again!"); System.out.println("Hello World"); } static void replaceHelloWorld(String text) throws Exception { // Note: would probably want to do hash as well... copyField(text, "Hello World", "value"); copyField(text, "Hello World", "offset"); copyField(text, "Hello World", "count"); } static void copyField(String source, String target, String name) throws Exception { Field field = String.class.getDeclaredField(name); field.setAccessible(true); field.set(target, field.get(source)); } }
source share