Skip to content

Instantly share code, notes, and snippets.

@mcy
Created June 2, 2014 16:50
Show Gist options
  • Save mcy/9b1a0e6fad89a0c7d2d8 to your computer and use it in GitHub Desktop.
Save mcy/9b1a0e6fad89a0c7d2d8 to your computer and use it in GitHub Desktop.
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class DestroyInt {
public static void main(String[] args) throws Throwable {
Class<?> cache = Integer.class.getDeclaredClasses()[0];
Field c = cache.getDeclaredField("cache");
nonFinal(c);
c.setAccessible(true);
Integer[] array = (Integer[]) c.get(null);
array = array.clone();
List<Integer> list = Arrays.asList(array);
Collections.shuffle(list);
c.set(null, list.toArray(new Integer[list.size()]));
System.out.printf("2 + 2 = %d", 2 + 2);
}
public static void nonFinal(Field f) throws Throwable{
Field mod = Field.class.getDeclaredField("modifiers");
mod.setAccessible(true);
mod.setInt(f, f.getModifiers() & ~Modifier.FINAL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment