samedi 8 septembre 2018

Illegal reflective access operation warning in Java 10

I would appreciate some help to rewrite some Java code which uses reflection, to remove warning from the compiler on Java 10:

This is the Java method in question:

public static boolean clean(final java.nio.ByteBuffer buffer) {
    if (buffer == null || !buffer.isDirect())
        return false;

    Boolean b = (Boolean) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            Boolean success = Boolean.FALSE;
            try {
                Method getCleanerMethod = buffer.getClass().getMethod("cleaner", (Class[])null);
                getCleanerMethod.setAccessible(true);
                Object cleaner = getCleanerMethod.invoke(buffer, (Object[])null);
                Method clean = cleaner.getClass().getMethod("clean", (Class[])null);
                clean.invoke(cleaner, (Object[])null);
                success = Boolean.TRUE;
            } catch (Exception e) {
                // This really is a show stopper on windows
                //e.printStackTrace();
            }
            return success;
        }
    });

    return b.booleanValue();
}

This is the code in question on GitHub: https://github.com/LibrePDF/OpenPDF/blob/master/openpdf/src/main/java/com/lowagie/text/pdf/MappedRandomAccessFile.java#L199

This is the warning I get during compilation on Java 10:

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (file:/[local path removed from here]/openpdf.jar) to method java.nio.DirectByteBuffer.cleaner() WARNING: Please consider reporting this to the maintainers of com.lowagie.text.pdf.MappedRandomAccessFile$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

The library can be found here: https://github.com/LibrePDF/OpenPDF

Any help about how to fix this in the proper way? Should the code be rewritten to not use reflection? Can I replace the MappedRandomAccessFile class with some similar code in a supported Java library instead? Thanks in advance!





Aucun commentaire:

Enregistrer un commentaire