@@ -0,0 +1,2945 @@
+package io.github.martinseeler.io.github.martinseeler.eviljava;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;%0Aimport java.util.Arrays;%0A%0A/**%0A * %22http://stackoverflow.com/questions/2481862/how-to-limit-setaccessible-to-only-legitimate-uses%22%0A */%0Apublic final class IntegerCacheCleaner %7B%0A%0A  public static void main(final String... args) throws Exception %7B%0A    // get the class of Integer's nested class IntegerCache%0A    final Class%3C?%3E intCache = Class.forName(%22java.lang.Integer$IntegerCache%22);%0A    // get the Field describing 'cache', which is a private and final field%0A    final Field cache = intCache.getDeclaredField(%22cache%22);%0A    // disable the private and final modifiers of 'cache'%0A    disableFinalPrivate(cache);%0A    // replace the current cache with a new one.%0A    // 'cache' is static, so no instance is required.%0A    cache.set(null, getUltimateAnswer());%0A%0A    // now see what you get for 1 + 1...%0A    // note that (1 + 1) is an int, and is boxed in an Integer only%0A    // when passed to format().%0A    System.out.println(String.format(%221 + 1 = %25d%22, 1 + 1));%0A%0A    for (int i = 0; i %3C Integer.valueOf(0); i++) %7B%0A      System.out.println(String.format(%22%25d%22, i));%0A    %7D%0A%0A    // node: the hack only changed the particular Field pointed by cache.%0A    // subsequent calls to intCache.getDeclaredField(%22cache%22) will have%0A    // to be hacked again in order to use them to modify 'cache' again.%0A  %7D%0A%0A  public static void disableFinalPrivate(Field field) throws Exception %7B%0A    // first, disable the 'private' modifier (this is not a hack - it's%0A    // what setAccessible is designed to do)%0A    field.setAccessible(true);%0A    // all modifiers are stored in one int, acting as a bitmap%0A    final int fieldModifiers = field.getModifiers();%0A    if (Modifier.isFinal(fieldModifiers)) %7B%0A      // the field is final, as its 'modifiers' member indicates.%0A      // we'd like to change that member, but it's private.%0A      final Field fieldMod = Field.class.getDeclaredField(%22modifiers%22);%0A      // so now we disable the private limitation%0A      fieldMod.setAccessible(true);%0A      // and turn the FINAL bit off. Note that the object whose%0A      // 'modifiers' member is being modified is the original%0A      // field, which describes Integer$IntegerCache.cache%0A      fieldMod.setInt(field, fieldModifiers & ~Modifier.FINAL);%0A      // So now we've used reflection to work around its own limitations,%0A      // and the final modifier has been disabled...%0A      // this is sufficient, because when modifying a final field%0A      // using reflection, the exception is thrown by the reflection%0A      // code and not by some lower-level JVM stuff.%0A    %7D%0A  %7D%0A%0A  static Integer%5B%5D getUltimateAnswer() %7B%0A    // return an array of 256 integers (as is the original cache),%0A    // all pointing on Integer(42)%0A    Integer%5B%5D ret = new Integer%5B256%5D;%0A    Arrays.fill(ret, 42);%0A    return ret;%0A  %7D%0A%7D%0A

Add IntegerCacheCleaner