Skip to content

Instantly share code, notes, and snippets.

@kaspernielsen
Last active April 28, 2020 08:25
Show Gist options
  • Save kaspernielsen/9986952eb50af1a80d688fdb605af415 to your computer and use it in GitHub Desktop.
Save kaspernielsen/9986952eb50af1a80d688fdb605af415 to your computer and use it in GitHub Desktop.
public class TestClass {
// So assumming internal classes are closed of with Java 16
// For Java [9;15]
// This will work because all packages are open to the unnamed module (stuff on the classpath)
// It will print a warning because you use method.setAccessible on a class in a package that will
// be closed of in Java 16
public static void main1(String[] args) throws Exception {
Module unnamed = TestClass.class.getModule();
Class<?> cls = Class.forName("sun.nio.ch.NativeThread");
java.lang.reflect.Method method = cls.getDeclaredMethod("signal", long.class);
assert cls.getModule().isOpen("sun.nio.ch", unnamed); //true
method.setAccessible(true);
method.invoke(null, 123);
}
// This will work because all packages are open to the unnamed module (stuff on the classpath)
// It will not print a warning, because warnings are only printed when you use reflection
public static void main2(String[] args) throws Throwable {
Module unnamed = TestClass.class.getModule();
Class<?> cls = Class.forName("sun.nio.ch.NativeThread");
java.lang.reflect.Method method = cls.getDeclaredMethod("signal", long.class);
assert cls.getModule().isOpen("sun.nio.ch", unnamed); //true
MethodHandle mh = MethodHandles.lookup().unreflect(method);
mh.invoke(123);
}
// Java [16;], you can simulate this with --illegal-access=deny
// This will fail with java.lang.reflect.InaccessibleObjectException
// Because access to the sun.nui.ch package has been closed effectively with Java 16
public static void main3(String[] args) throws Exception {
Module unnamed = TestClass.class.getModule();
Class<?> cls = Class.forName("sun.nio.ch.NativeThread");
java.lang.reflect.Method method = cls.getDeclaredMethod("signal", long.class);
assert !cls.getModule().isOpen("sun.nio.ch", unnamed); //false
method.setAccessible(true);
}
// This will fail with java.lang.IllegalAccessException
// Because access to the sun.nui.ch package has been closed effectively with Java 16
public static void main4(String[] args) throws Exception {
Module unnamed = TestClass.class.getModule();
Class<?> cls = Class.forName("sun.nio.ch.NativeThread");
java.lang.reflect.Method method = cls.getDeclaredMethod("signal", long.class);
assert !cls.getModule().isOpen("sun.nio.ch", unnamed); //false
MethodHandles.lookup().unreflect(method);
}
// Calling cls.getModule().addOpens("sun.nio.ch", TestClass.class.getModule());
// or --add-opens java.base/sun.nio.ch=ALL-UNNAME on the first two examples.
// Have no other program effect that disabling the reflection warning because
// all classes are opened by default to any class on the classpath.
// If you use --add-opens java.base/sun.nio.ch=ALL-UNNAME on the last two examples
// with Java16 they will no longer fail, because they are actually closedv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment