Got a project which is built to still support Java 6. The code below is inside a jar file built with Compiler compliance level 1.6
That jar file should be called from java apps built for java 6 or newer. It runs fine in Java 8 as well.
Now with Java9, I get problems with nio.DirectByteBuffer, and I tried to solve it this way, using reflection:
@SuppressWarnings("unchecked")
static void cleanDirectBuffer(sun.nio.ch.DirectBuffer buffer) {
if (JAVA_VERSION < 1.9) {
sun.misc.Cleaner cleaner = buffer.cleaner();
if (cleaner != null) cleaner.clean();
} else {
// For java9 do it the reflection way
@SuppressWarnings("rawtypes")
Class B = buffer.getClass();
// will be a java.nio.DirectBuffer, which is unknown if compiled in 1.6 compliance mode
try {
java.lang.reflect.Method CleanerMethod = B.getMethod("cleaner");
CleanerMethod.setAccessible(true); // fails here !
Object cleaner = CleanerMethod.invoke(buffer);
if (cleaner == null) return;
@SuppressWarnings("rawtypes")
Class C = cleaner.getClass();
java.lang.reflect.Method CleanMethod = C.getMethod("clean");
CleanMethod.invoke(cleaner);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Exception other) {
other.printStackTrace();
}
}
}
The JAVA_VERSION detection is fine and switches nicely, depending on the version my calling code is using. jre6 to jre8 environments use nicely the sun.misc.Cleaner path, but that does not work in java9
You'll probably notice that I'm not an expert in java.reflection. By guessing, I found .setAccessible(true);
but that did not help at all.
What else am I missing, or is there a better approach to my problem?
Aucun commentaire:
Enregistrer un commentaire