Use reflection to call cleaner on direct byte buffers in JDK9.

Motivation:

Project Jigsaw in JDK9 has moved the direct byte buffer cleaner from
sun.misc.Cleaner to java.lang.ref.Cleaner$Cleanable. This cause the
current platform tests to throw a ClassNotFoundException, disabling the
use of direct byte buffer cleaners.

Modifications:

I use reflection to find the clean method in either sun.misc.Cleaner or
java.lang.ref.Cleaner$Cleanable.

Result:

Netty uses direct byte buffers on JDK9 as it already do on earlier JDKs.
This commit is contained in:
Carsten Varming 2016-06-29 13:17:22 -04:00 committed by Norman Maurer
parent 8780062a43
commit 34d8b514d7

View File

@ -17,40 +17,45 @@ package io.netty.util.internal;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import sun.misc.Cleaner;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /**
* Allows to free direct {@link ByteBuffer} by using {@link Cleaner}. This is encapsulated in an extra class to be able * Allows to free direct {@link ByteBuffer} by using Cleaner. This is encapsulated in an extra class to be able
* to use {@link PlatformDependent0} on Android without problems. * to use {@link PlatformDependent0} on Android without problems.
* *
* For more details see <a href="https://github.com/netty/netty/issues/2604">#2604</a>. * For more details see <a href="https://github.com/netty/netty/issues/2604">#2604</a>.
*/ */
final class Cleaner0 { final class Cleaner0 {
private static final long CLEANER_FIELD_OFFSET; private static final long CLEANER_FIELD_OFFSET;
private static final Method CLEAN_METHOD;
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Cleaner0.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(Cleaner0.class);
static { static {
ByteBuffer direct = ByteBuffer.allocateDirect(1); ByteBuffer direct = ByteBuffer.allocateDirect(1);
Field cleanerField; Field cleanerField;
long fieldOffset = -1; long fieldOffset = -1;
Method clean = null;
if (PlatformDependent0.hasUnsafe()) { if (PlatformDependent0.hasUnsafe()) {
try { try {
cleanerField = direct.getClass().getDeclaredField("cleaner"); cleanerField = direct.getClass().getDeclaredField("cleaner");
cleanerField.setAccessible(true); cleanerField.setAccessible(true);
Cleaner cleaner = (Cleaner) cleanerField.get(direct); Object cleaner = cleanerField.get(direct);
cleaner.clean();
fieldOffset = PlatformDependent0.objectFieldOffset(cleanerField); fieldOffset = PlatformDependent0.objectFieldOffset(cleanerField);
clean = cleaner.getClass().getDeclaredMethod("clean");
clean.invoke(cleaner);
} catch (Throwable t) { } catch (Throwable t) {
// We don't have ByteBuffer.cleaner(). // We don't have ByteBuffer.cleaner().
fieldOffset = -1; fieldOffset = -1;
clean = null;
} }
} }
logger.debug("java.nio.ByteBuffer.cleaner(): {}", fieldOffset != -1? "available" : "unavailable"); logger.debug("java.nio.ByteBuffer.cleaner(): {}", fieldOffset != -1? "available" : "unavailable");
CLEANER_FIELD_OFFSET = fieldOffset; CLEANER_FIELD_OFFSET = fieldOffset;
CLEAN_METHOD = clean;
// free buffer if possible // free buffer if possible
freeDirectBuffer(direct); freeDirectBuffer(direct);
@ -60,10 +65,11 @@ final class Cleaner0 {
if (CLEANER_FIELD_OFFSET == -1 || !buffer.isDirect()) { if (CLEANER_FIELD_OFFSET == -1 || !buffer.isDirect()) {
return; return;
} }
assert CLEAN_METHOD != null : "CLEANER_FIELD_OFFSET != -1 implies CLEAN_METHOD != null";
try { try {
Cleaner cleaner = (Cleaner) PlatformDependent0.getObject(buffer, CLEANER_FIELD_OFFSET); Object cleaner = PlatformDependent0.getObject(buffer, CLEANER_FIELD_OFFSET);
if (cleaner != null) { if (cleaner != null) {
cleaner.clean(); CLEAN_METHOD.invoke(cleaner);
} }
} catch (Throwable t) { } catch (Throwable t) {
// Nothing we can do here. // Nothing we can do here.