Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itext Java 11: Illegal reflective access by com.itextpdf.io.source.ByteBufferRandomAccessSource$1

Recently upgraded to Java 11 and began performing a regression check. Currently get an Illegal reflective access error when trying to call com.itextpdf.text.pdf.PdfReader.close. Currently on Itext version 5.5.13 but also tried on itext 7.0.0 and had same issue.

Does anyone have any suggestions on how to fix compatibility issues between Java-11 and Itext?

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.itextpdf.io.source.ByteBufferRandomAccessSource$1 (file:...repository/com/itextpdf/io/7.0.0/io-7.0.0.jar) to method java.nio.DirectByteBuffer.cleaner() WARNING: Please consider reporting this to the maintainers of com.itextpdf.io.source.ByteBufferRandomAccessSource$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

like image 432
D. Millard Avatar asked Nov 14 '18 13:11

D. Millard


2 Answers

While I second the comments encouraging you to debug the code and find the root cause (and then submit a pull request), or creating an issue in iText Jira if you are a customer with a support contract (which would raise the priority of the issue), here is a workaround suggestion (that I haven't tested, but I chances are it will work):

Use PdfReader and PdfWriter constructors that accept InputStream and OutputStream, respectively. In this case the code causing the problem should not be invoked. Same thing for all the other cases in which iText interacts with your file system - wrap everything into InputStream/OutputStream, or deal with byte[] arrays.

So this line:

new PdfDocument(new PdfReader(inFilePath), new PdfWriter(outFilePath))

becomes this one:

new PdfDocument(new PdfReader(new FileInputStream(inFilePath)), 
                new PdfWriter(new FileOutputStream(outFilePath)))

You might also want to wrap the streams into BufferedInputStream/BufferedOutputStream.

Similarly, when dealing with PdfFontFactory, use methods that accept byte[] instead of String representing file path and so on.

like image 102
Alexey Subach Avatar answered Nov 04 '22 16:11

Alexey Subach


If you're curious what is the Illegal Reflective Access is all about, please refer here: what is an illegal reflective access

This particular warning comes from this class:

https://github.com/itext/itext7/blob/develop/io/src/main/java/com/itextpdf/io/source/ByteBufferRandomAccessSource.java

From this particular method:

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

    Boolean b = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
        public Boolean run() {
            Boolean success = Boolean.FALSE;
            try {
                // java 9
                if (UNMAP_SUPPORTED)
                    CLEANER.freeBuffer(buffer.toString(), buffer);
                // java 8 and lower
                else {
                    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
                Logger logger = LoggerFactory.getLogger(ByteBufferRandomAccessSource.class);
                logger.debug(e.getMessage());
            }
            return success;
        }
    });

    return b;
}

This line exactly:

getCleanerMethod.setAccessible(true);

As long as this warning does not prevent iText from working as expected, I think the best you can do is to submit an issue/PR to iText team and wait for the proper fix to be available.

like image 42
Mikhail Kholodkov Avatar answered Nov 04 '22 15:11

Mikhail Kholodkov