- my webspace

- my webspace

Latest Comment

Allama Iqbal - Selective verse...
Yahoouj
Really good work about this website was done. Keep trying mo...
07/03/10 21:04 More...
By Roderick

Allama Iqbal - Selective verse...
Great Job
You have dont a great job of collecting these... Even I had ...
25/08/09 07:01 More...
By Sikandar

O ye who don't believe !
It's like Lehman Brothers :grin
11/10/08 16:31 More...
By anurag Chaturvedi

I Protest
@Sikku
Thanks Sikku for the feedback. I never intend to blame, a...
29/07/08 17:06 More...
By Aminur Rashid

I Protest
Great !!!
:zzz this is a wonderful story and very honest from the hea...
29/07/08 10:33 More...
By Jennifer Gallagher

Login






Lost Password?
No account yet? Register

Tell a Friend

Home arrow Java arrow When false is true
When false is true PDF Print E-mail
User Rating: / 1
PoorBest 
Written by Aminur Rashid   
Thursday, 12 August 2010
.

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.

StumbleUponDigg This!Bookmark on Delicious

Add as favourites (61) | Quote this article on your site | Views: 535 | E-mail

Be first to comment this article
RSS comments

Only registered users can write comments.
Please login or register.

Last Updated ( Thursday, 12 August 2010 )
 
< Prev   Next >
Aminur Rashid