Use the Runnable.run method to clean direct byte buffers if avaiable.
Motivation: In JDK9 the Cleaner.clean method cannot be called as it is not exported from `java.base`. `Runnable.run` should be called instead. Modifications: Pick Runnable.run if the cleaner implements Runnable. Otherwise try the clean method on the class implementing the cleaner. Result: The cleaner for direct byte buffers is run on JDK9 as well as earlier JDKs.
This commit is contained in:
parent
f04ff5b715
commit
cb071910ba
|
@ -31,10 +31,17 @@ public final class ByteBufferUtil {
|
||||||
Method directBufferCleanerCleanX = null;
|
Method directBufferCleanerCleanX = null;
|
||||||
boolean v;
|
boolean v;
|
||||||
try {
|
try {
|
||||||
directBufferCleanerX = Class.forName("java.nio.DirectByteBuffer").getMethod("cleaner");
|
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
||||||
|
directBufferCleanerX = direct.getClass().getMethod("cleaner");
|
||||||
directBufferCleanerX.setAccessible(true);
|
directBufferCleanerX.setAccessible(true);
|
||||||
directBufferCleanerCleanX = Class.forName("sun.misc.Cleaner").getMethod("clean");
|
Object cleaner = directBufferCleanerX.invoke(direct);
|
||||||
directBufferCleanerCleanX.setAccessible(true);
|
try {
|
||||||
|
Runnable runnable = (Runnable) cleaner;
|
||||||
|
directBufferCleanerCleanX = Runnable.class.getDeclaredMethod("run");
|
||||||
|
} catch (ClassCastException ignored) {
|
||||||
|
directBufferCleanerCleanX = cleaner.getClass().getMethod("clean");
|
||||||
|
}
|
||||||
|
directBufferCleanerCleanX.invoke(cleaner);
|
||||||
v = true;
|
v = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
v = false;
|
v = false;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user