Created
June 3, 2021 13:43
-
-
Save mikesamuel/3b8478e2811eb54efed7b2a32b2d23da to your computer and use it in GitHub Desktop.
Java boxing is violable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.*; | |
import java.util.*; | |
public class Foo { | |
public static void main(String[] argv) throws Exception { | |
Class<?> integerCacheClass = Arrays.stream(Integer.class.getDeclaredClasses()) | |
.filter(c -> c.getSimpleName().equals("IntegerCache")) | |
.findFirst() | |
.get(); | |
Field cacheField = Arrays.stream(integerCacheClass.getDeclaredFields()) | |
.filter(c -> c.getName().equals("cache")) | |
.findFirst() | |
.get(); | |
cacheField.setAccessible(true); | |
Integer[] cache = (Integer[]) cacheField.get(null); | |
Arrays.fill(cache, new Integer(1337)); | |
Integer boxed = 3; | |
System.out.println("" + boxed); | |
} | |
} |
Author
mikesamuel
commented
Jun 3, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment