private static MethodHandle unmapJava7Or8(MethodHandles.Lookup lookup) throws ReflectiveOperationException
  {
    // "Compile" a MethodHandle that is roughly equivalent to the following lambda:
    //
    // (ByteBuffer buffer) -> {
    //   sun.misc.Cleaner cleaner = ((java.nio.DirectByteBuffer) byteBuffer).cleaner();
    //   if (nonNull(cleaner))
    //     cleaner.clean();
    //   else
    //     noop(cleaner); // the noop is needed because MethodHandles#guardWithTest always needs both if and else
    // }
    //
    Class<?> directBufferClass = Class.forName("java.nio.DirectByteBuffer");
    Method m = directBufferClass.getMethod("cleaner");
    m.setAccessible(true);
    MethodHandle directBufferCleanerMethod = lookup.unreflect(m);
    Class<?> cleanerClass = directBufferCleanerMethod.type().returnType();
    MethodHandle cleanMethod = lookup.findVirtual(cleanerClass, "clean", MethodType.methodType(void.class));
    MethodHandle nonNullTest = lookup.findStatic(Objects.class, "nonNull",
                                                 MethodType.methodType(boolean.class, Object.class)
    ).asType(MethodType.methodType(boolean.class, cleanerClass));
    MethodHandle noop = MethodHandles.dropArguments(MethodHandles.constant(
        Void.class,
        null
    ).asType(MethodType.methodType(void.class)), 0, cleanerClass);
    MethodHandle unmapper = MethodHandles.filterReturnValue(
        directBufferCleanerMethod,
        MethodHandles.guardWithTest(nonNullTest, cleanMethod, noop)
    ).asType(MethodType.methodType(void.class, ByteBuffer.class));
    return unmapper;
  }
private static MethodHandle unmapJava7Or8 ( MethodHandles . Lookup lookup ) throws ReflectiveOperationException { Class < ? > directBufferClass = Class . forName ( "java.nio.DirectByteBuffer" ) ; Method m = directBufferClass . getMethod ( "cleaner" ) ; m . setAccessible ( true ) ; MethodHandle directBufferCleanerMethod = lookup . unreflect ( m ) ; Class < ? > cleanerClass = directBufferCleanerMethod . type ( ) . returnType ( ) ; MethodHandle cleanMethod = lookup . findVirtual ( cleanerClass , "clean" , MethodType . methodType ( void . class ) ) ; MethodHandle nonNullTest = lookup . findStatic ( Objects . class , "nonNull" , MethodType . methodType ( boolean . class , Object . class ) ) . asType ( MethodType . methodType ( boolean . class , cleanerClass ) ) ; MethodHandle noop = MethodHandles . dropArguments ( MethodHandles . constant ( Void . class , null ) . asType ( MethodType . methodType ( void . class ) ) , 0 , cleanerClass ) ; MethodHandle unmapper = MethodHandles . filterReturnValue ( directBufferCleanerMethod , MethodHandles . guardWithTest ( nonNullTest , cleanMethod , noop ) ) . asType ( MethodType . methodType ( void . class , ByteBuffer . class ) ) ; return unmapper ; }
NB: while Druid no longer support Java 7, this method would still work with that version as well.