Jan 31
I always enjoy the Dr. Heinz Kabutz Java Specialists’ Newsletter .
He often seems to find the most bizarre corners of Java and the JVM.
A long time ago he showed how he could get:
(”hi there”.equals(”cheers !”)) == true
Now he shows how auto-boxing allows him to cheat the flyweight pattern of: Integer valueOf(int) (and for the other primitives).
Each class seems to keep a cache of its values, and if a security manager isn’t setup, we can dip into this cache and make some changes via:
try {
Class[] classes = Integer.class.getDeclaredClasses();
for (Class clazz : classes) {
if (clazz.getName().endsWith("IntegerCache")) {
Field cacheField = clazz.getDeclaredField("cache");
cacheField.setAccessible(true);
Integer[] cache = (Integer[]) cacheField.get(null);
for (int i = 0; i < cache.length; i++) {
cache[i] = new Integer(0);
}
}
}
} catch (Throwable e) {
// we silently pretend we didn't want to destroy Java...
}

February 2nd, 2005 at 12:57 pm
We use a similar trick to make ResourceBundle reload in WebWork when we have a debug flag set:
Class klass = ResourceBundle.class;
Field field = klass.getDeclaredField(”cacheList”);
field.setAccessible(true);
Object cache = field.get(null);
Method clearMethod = cache.getClass().getMethod(”clear”, new Class[0]);
clearMethod.invoke(cache, new Object[0]);
February 2nd, 2005 at 12:57 pm
We use a similar trick to make ResourceBundle reload in WebWork when we have a debug flag set:
Class klass = ResourceBundle.class;
Field field = klass.getDeclaredField(”cacheList”);
field.setAccessible(true);
Object cache = field.get(null);
Method clearMethod = cache.getClass().getMethod(”clear”, new Class[0]);
clearMethod.invoke(cache, new Object[0]);
October 8th, 2006 at 6:42 pm
big thank
October 8th, 2006 at 6:42 pm
big thank