Search code examples
javareflectionopenpdf

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?

(I am one of the maintainers of OpenPDF, and need some help with this)

Thanks in advance!


Solution

  • I see that this problem has already been reported to the maintainers of the library; see https://github.com/LibrePDF/OpenPDF/issues/101.

    For now, you can ignore the message since it is only a warning.

    Can I replace the MappedRandomAccessFile class with some similar code in a supported Java library instead?

    Nobody is going to stop you :-) But if you are asking for a recommendation, that is off-topic.

    Your other alternative is to wait for the LibrePDF maintainers to fix it.

    However, I suspect that they won't ... because they can't. You should take a look at this unresolved Java Bug report from 2005: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4724038. I think that this means that MappedRandomAccessFile can only be fixed by fixing the Java bug ... which would render the illicit call to cleaner() unnecessary.


    After a bit more digging, I found this:

    This describes a problem where it was possible for the GC's calling of the cleaner to not "keep up", resulting in premature OOMEs. This has been fixed in Java 9, and the fix has been back-patched to Java 8 & 7.

    What you need to do is check through your codebase's history and issues and try to figure out why the "cleaner" code was included in your MappedRandomAccessFile class. If it was primarily to work around the problem of JDK-6857566, then you should be able to remove it now.