Skip to content

Commit cc8e43e

Browse files
committed
Fix JDK 11 code-level incompatibilities
sun.nio.ch.DirectBuffer.cleaner() is of type jdk.internal.ref.Cleaner on JDK9+. It's also not accessible due to tighter encapsulation without special handling like --add-opens=java.base/jdk.internal.ref=ALL-UNNAMED
1 parent 195b7c6 commit cc8e43e

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lab-core/src/main/java/com/github/jnthnclt/os/lab/core/io/DirectBufferCleaner.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class DirectBufferCleaner {
2424
try {
2525
_directBufferClass = Class.forName("sun.nio.ch.DirectBuffer");
2626
_directBufferCleanerMethod = _directBufferClass.getMethod("cleaner");
27-
_cleanerClass = Class.forName("sun.misc.Cleaner");
27+
_cleanerClass = _directBufferCleanerMethod.getReturnType();
2828
_cleanMethod = _cleanerClass.getMethod("clean");
2929
_available = true;
3030
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
31-
System.out.println("Failed to reflect direct buffer cleaner, these methods will be unavailable");
32-
e.printStackTrace();
31+
System.out.println("Failed to reflect direct buffer cleaner, these methods will be unavailable. " +
32+
"If you are on Java 9+ add --add-opens=java.base/jdk.internal.ref=ALL-UNNAMED " +
33+
"to JVM options");
3334
}
3435
directBufferClass = _directBufferClass;
3536
directBufferCleanerMethod = _directBufferCleanerMethod;
@@ -46,8 +47,9 @@ static public void clean(ByteBuffer bb) {
4647
cleanMethod.invoke(cleaner);
4748
}
4849
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
49-
System.out.println("Failed to clean buffer");
50-
e.printStackTrace();
50+
System.out.println("Failed to clean buffer. " +
51+
"If you are on Java 9+ add --add-opens=java.base/jdk.internal.ref=ALL-UNNAMED " +
52+
"to JVM options");
5153
}
5254
}
5355
}

lab-core/src/test/java/com/github/jnthnclt/os/lab/core/LABValidationNGTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private void validationTest(LAB lab) throws InterruptedException, Exception, Exe
325325
},
326326
(index, key, timestamp, tombstoned, version1, value1) -> {
327327
hits.incrementAndGet();
328-
found.add(key == null ? 0 : key.getLong(0));
328+
found.add(key == null ? 0L : key.getLong(0));
329329
return true;
330330
}, true);
331331
}

0 commit comments

Comments
 (0)