Do you remember when email was reliable? Having to move almaer.com
Jan 31

Heinz tricks Java by Mangling Integers

Java, Tech Add comments

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...
}

4 Responses to “Heinz tricks Java by Mangling Integers”

  1. Jason Carreira Says:

    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]);

  2. Jason Carreira Says:

    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]);

  3. md formulation skin care Says:

    big thank

  4. md formulation skin care Says:

    big thank

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What is the number before 3? (just put in the digit)