OpenNMS / jrobin

GNU Lesser General Public License v2.1
17 stars 12 forks source link

Java9 Support, remove use of internal api:s #3

Open pethers opened 8 years ago

pethers commented 8 years ago

jdeps result

org.jrobin.core.RrdNioBackend -> sun.misc.Cleaner JDK internal API (JDK removed internal API) org.jrobin.core.RrdNioBackend -> sun.nio.ch.DirectBuffer JDK internal API (java.base)

Support Oracle JDK6-9

fix from http://stackoverflow.com/questions/1854398/how-to-garbage-collect-a-direct-buffer-java


private void unmapFile() { destroyBuffer(byteBuffer); }

private static void destroyBuffer(Buffer buffer) {
    if (buffer != null && buffer.isDirect()) {
        try {
            if (!buffer.getClass().getName().equals("java.nio.DirectByteBuffer")) {
                Field attField = buffer.getClass().getDeclaredField("att");
                attField.setAccessible(true);
                buffer = (Buffer) attField.get(buffer);
            }

            Method cleanerMethod = buffer.getClass().getMethod("cleaner");
            cleanerMethod.setAccessible(true);
            Object cleaner = cleanerMethod.invoke(buffer);
            Method cleanMethod = cleaner.getClass().getMethod("clean");
            cleanMethod.setAccessible(true);
            cleanMethod.invoke(cleaner);
        } catch (Exception e) {
            throw new RuntimeException("Could not destroy direct buffer " + buffer, e);
        }
    }

}

Tested with JDK8, JDK9_b121 require jvm option for JDK9 "-XaddExports:java.base/jdk.internal.ref=ALL-UNNAMED"

Possible solution to support other JDK providers at https://sourceforge.net/p/tuer/code/HEAD/tree/pre_beta/src/main/java/engine/misc/DeallocationHelper.java

fbacchella commented 5 years ago

This is how I solved it in RRD4J: https://github.com/rrd4j/rrd4j/commit/4b791a7ac03c26ff159f97fa0716c0d28aa15a45.

It was issued in release 3.3.