Skip to content

Instantly share code, notes, and snippets.

@peter-lawrey
Created January 3, 2013 10:40
Show Gist options
  • Save peter-lawrey/4442523 to your computer and use it in GitHub Desktop.
Save peter-lawrey/4442523 to your computer and use it in GitHub Desktop.
A horrible hack to change a String.
import java.lang.reflect.Field;
class MainClass {
// requires Java 7 update 5+
static {
try {
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("B", value.get("ABC"));
} catch (Throwable e) {
throw new AssertionError(e);
}
}
public static void main(String[] args) {
System.out.println("B"); // prints ABC
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment