While trying to explain reflection to one my young friend, I finally ended up writing this code:
package aminur.test.mutate; import java.lang.reflect.Field; public class MutableString { public static void main(String[] argv) { mutate(); System.out.println("TRUE".equals("FALSE")); } public static void mutate() { try { String t = "TRUE"; String f = "FALSE"; Field val = String.class.getDeclaredField("value"); Field count = String.class.getDeclaredField("count"); Field off = String.class.getDeclaredField("offset"); val.setAccessible(true); count.setAccessible(true); off.setAccessible(true); count.set(t, f.length()); val.set(t, f.toCharArray()); off.set(f, off.getInt(f)); } catch (Exception e) { e.printStackTrace(); } } }
This won’t work in 1.3/.4 but will work in higher/lower version of JDK
The bug #5044412 fix(?) may disallow setting of value of fields.
Related posts
Feed
- India
- World
- Score
- Cricket
Popular
- Allama Iqbal – Sele... 42k views
- Harappan Culture (Indus V... 26.5k views
- Allama Iqbal – Sele... 20.6k views
- Bhagalpur to Singapore 17.3k views
- Islamic Banking? Whats th... 16.5k views
- Beach Trek Across Arabian... 14.8k views
- Sach hai, Vipatti jab aat... 12.4k views
- I Protest 10.4k views
- O ye who don’t beli... 10k views
- Thread unsafe Format in J... 6.5k views
This site uses cookies. These cookies come mainly from third party library used at this site(Google/Jetpack/etc). By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here Cookie Policy
To find out more, including how to control cookies, see here Cookie Policy