使用iText PDF FontFactory.registerDirectories()和Java 11的非法反射访问操作警告 [英] Illegal reflective access operation warning with iText PDF FontFactory.registerDirectories() and Java 11

查看:451
本文介绍了使用iText PDF FontFactory.registerDirectories()和Java 11的非法反射访问操作警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iText PDF FontFactory.registerDirectories()Java 11是否可以避免Illegal reflective access operation警告?

Is it possible to avoid Illegal reflective access operation warning with iText PDF FontFactory.registerDirectories() and Java 11?

重现问题的步骤:

  1. 安装OpenJDK 11.
  2. iText PDFv5.5.13.2添加到Java项目.
  3. 致电com.itextpdf.text.FontFactory.registerDirectories().
  4. 请参阅警告:
  1. Install OpenJDK 11.
  2. Add iText PDF library v5.5.13.2 to your Java project.
  3. Call com.itextpdf.text.FontFactory.registerDirectories().
  4. See the warning:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.itextpdf.text.io.ByteBufferRandomAccessSource$1 to method java.nio.DirectByteBuffer.cleaner()
WARNING: Please consider reporting this to the maintainers of com.itextpdf.text.io.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

请参阅Java 11& Poc中的POC. Gradle 此处.

See the poc with Java 11 & Gradle here.

推荐答案

唯一的干净解决方案是,一旦存在新版本,则切换到不执行此非法反射访问的新版本.

The only clean solution is to switch to a newer version that does not perform this illegal reflective access, once this new version exists.

作为临时解决方法,您可以使用

As a temporary work-around, you could use

package com.example;

import org.junit.Test;

import com.itextpdf.text.FontFactory;
import org.junit.BeforeClass;

public class FontFactoryTest {
    @BeforeClass
    public static void prepare() {
        Module mod = FontFactory.class.getClassLoader().getUnnamedModule();
        if(mod == FontFactory.class.getModule()) {
            Object.class.getModule().addOpens("java.nio", mod);
        }
    }

    @Test
    public void test() {
            FontFactory.registerDirectories();
    }
}

这仅在将代码放在未命名模块中(通过类路径而不是模块路径加载)并且JDK默认授予反射访问权限的情况下才有效(预期将来会更改)

This only work as long as the code is placed in the unnamed module (loaded via class path, rather than module path) and as long as the JDK grants the reflective access by default (this is expected to change in the future).

否则,您可能需要在命令行中使用-add-opens选项或类似的选项(假设特定的执行环境具有这样的选项),并且仍然不能保证它可以正常工作,因为这意味着要访问一个不需要在特定的实现中完全存在.

Otherwise, you would need an -add-opens option at the command line or something similar (assuming that the particular execution environment has such an option) and it still is not guaranteed to work, as it implies an access to a member that doesn’t need to be there at all in a particular implementation.

如果模块本身没有权限,则无法添加打开"边缘.它完全起作用的原因是,从java.base模块到未命名模块的打开"边缘已经存在,并且仅使用此警告进行了补充.上面的代码利用此权限来添加显式的打开"边缘,该边缘未与警告链接.如前所述,这只是一个临时解决方法,它将在以后的版本中停止工作.

A module can not add an "opens" edge if it doesn’t have the permission itself. The reason, it works at all, is that the "opens" edge from the java.base module to the unnamed module does already exist, it’s just augmented with this warning. The code above utilizes this permission to add an explicit "opens" edge, which is not linked with the warning. As said, this is only a temporary work-around that will stop working in future versions.

这篇关于使用iText PDF FontFactory.registerDirectories()和Java 11的非法反射访问操作警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆