Log why it was not possible to use ByteBuffer.cleaner

Motivation:

We should log why we can not use ByteBuffer.cleaner and so maybe allow the user to fix it.

Modifications:

- Use Unsafe to access the field
- Log the exception when we can not use ByteBuffer.cleaner

Result:

Easier to debug why using cleaner is not possible.
This commit is contained in:
Norman Maurer 2017-02-16 20:23:28 +01:00
parent 5de4029b43
commit 67be7c5b9f

View File

@ -42,12 +42,12 @@ final class Cleaner0 {
long fieldOffset = -1; long fieldOffset = -1;
Method clean = null; Method clean = null;
boolean cleanerIsRunnable = false; boolean cleanerIsRunnable = false;
Throwable error = null;
if (PlatformDependent0.hasUnsafe()) { if (PlatformDependent0.hasUnsafe()) {
try { try {
cleanerField = direct.getClass().getDeclaredField("cleaner"); cleanerField = direct.getClass().getDeclaredField("cleaner");
cleanerField.setAccessible(true);
fieldOffset = PlatformDependent0.objectFieldOffset(cleanerField); fieldOffset = PlatformDependent0.objectFieldOffset(cleanerField);
Object cleaner = cleanerField.get(direct); Object cleaner = PlatformDependent0.getObject(direct, fieldOffset);
try { try {
// Cleaner implements Runnable from JDK9 onwards. // Cleaner implements Runnable from JDK9 onwards.
Runnable runnable = (Runnable) cleaner; Runnable runnable = (Runnable) cleaner;
@ -62,9 +62,14 @@ final class Cleaner0 {
fieldOffset = -1; fieldOffset = -1;
clean = null; clean = null;
cleanerIsRunnable = false; cleanerIsRunnable = false;
error = t;
} }
} }
logger.debug("java.nio.ByteBuffer.cleaner(): {}", fieldOffset != -1? "available" : "unavailable"); if (error == null) {
logger.debug("java.nio.ByteBuffer.cleaner(): available");
} else {
logger.debug("java.nio.ByteBuffer.cleaner(): unavailable", error);
}
CLEANER_FIELD_OFFSET = fieldOffset; CLEANER_FIELD_OFFSET = fieldOffset;
CLEAN_METHOD = clean; CLEAN_METHOD = clean;
CLEANER_IS_RUNNABLE = cleanerIsRunnable; CLEANER_IS_RUNNABLE = cleanerIsRunnable;