简体   繁体   English

如何为非法反射访问警告抛出异常?

[英]How can I throw an exception for an illegal reflective access warning?

How can I throw an exception for an illegal reflective access warning?如何为非法反射访问警告抛出异常? For example, consider the following code:例如,考虑以下代码:

import org.apache.commons.lang3.builder.*;

class Test {
    public static void main(String[] args) {
        System.out.println(ReflectionToStringBuilder.toString(Boolean.TRUE));
    }
}

This code prints the following warning to System.err:此代码向 System.err 打印以下警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.commons.lang3.builder.ReflectionToStringBuilder (file:/Users/brianschack/eclipse-workspace/User%20Libraries/com
mons-lang3-3.7/commons-lang3-3.7.jar) to field java.lang.Boolean.value
WARNING: Please consider reporting this to the maintainers of org.apache.commons.lang3.builder.ReflectionToStringBuilder
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

Boolean.TRUE is such a simple value that I don't really need ReflectionToStringBuilder. Boolean.TRUE 是一个如此简单的值,我真的不需要 ReflectionToStringBuilder。 But more complex types (such as HashMap) print the same warning.但是更复杂的类型(例如 HashMap)会打印相同的警告。 I chose Boolean.TRUE in order to simplify this example.我选择 Boolean.TRUE 是为了简化这个例子。

When I searched for this warning message, I found suggestions to report it to the package maintainer, avoid the warning, or disable it altogether ( JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState ).当我搜索此警告消息时,我发现建议将其报告给包维护者、避免警告或完全禁用它( JDK9:发生非法反射访问操作。org.python.core.PySystemState )。

I would like to throw an exception for the warning in order to get a stack trace of where the illegal access occurred.我想为警告抛出异常,以便获得非法访问发生位置的堆栈跟踪。 Then I could change the code to avoid the illegal access that caused the warning.然后我可以更改代码以避免导致警告的非法访问。 I would also like to make a unit test that would check for the warning in the future.我还想做一个单元测试,以便将来检查警告。

I tried to test for printing to System.err according to the StackOverflow question, JUnit test for System.out.println() .我试图根据 StackOverflow 问题,即System.out.println() 的 JUnit 测试来测试是否打印到 System.err。 This involves setting System.err to a ByteArrayOutputStream and then examining the contents.这涉及将 System.err 设置为 ByteArrayOutputStream,然后检查内容。 But unfortunately, according to How to hide warning “Illegal reflective access” in java 9 without JVM argument?但不幸的是,根据如何在没有 JVM 参数的情况下在 java 9 中隐藏警告“非法反射访问”? , the IllegalAccessLogger gets a reference to System.err during JVM bootstrap before I can change it. ,IllegalAccessLogger 在 JVM 引导期间获得对 System.err 的引用,然后我才能更改它。

I also tried closing System.err, but it seems that printing to a closed stream silently fails instead of throwing an exception.我也尝试关闭 System.err,但似乎打印到关闭的流会以静默方式失败而不是引发异常。 Notice that the output for the following code does not contain the string "err-2":请注意,以下代码的输出不包含字符串“err-2”:

Code:代码:

class Test {
    public static void main(String[] args) {
        System.out.println("Start");
        System.err.println("err-1");
        System.err.close();
        System.err.println("err-2");
        System.out.println("End");
    }
}

Output:输出:

Start
err-1
End
  1. Open the Eclipse development environment.打开 Eclipse 开发环境。
  2. Choose Run menu –> Run Configurations... –> Java Application –> (your configuration) –> Arguments.选择运行菜单 –> 运行配置... –> Java 应用程序 –>(您的配置) –> 参数。
  3. In the VM arguments text box, type "--illegal-access=deny" [0].在 VM 参数文本框中,键入“--illegal-access=deny”[0]。
  4. Click the Apply button.单击应用按钮。
  5. Click the Run button.单击运行按钮。
  6. Illegal access will throw the java.lang.reflect.InaccessibleObjectException [1].非法访问将抛出 java.lang.reflect.InaccessibleObjectException [1]。

[0] http://mail.openjdk.java.net/pipermail/jigsaw-dev/2017-May/012673.html [0] http://mail.openjdk.java.net/pipermail/jigsaw-dev/2017-May/012673.html

[1] http://docs.oracle.com/javase/9/docs/api/java/lang/reflect/InaccessibleObjectException.html [1] http://docs.oracle.com/javase/9​​/docs/api/java/lang/reflect/InaccessibleObjectException.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java - 如何在没有JVM参数的情况下在java 9中隐藏警告“非法反射访问”? - How to hide warning "Illegal reflective access" in java 9 without JVM argument? Java 10中的非法反射访问操作警告 - Illegal reflective access operation warning in Java 10 警告:发生了非法反射访问操作 - WARNING: An illegal reflective access operation has occurred 如何修复 Hadoop 警告:在 Ubuntu 上发生了非法反射访问操作错误 - How to fix Hadoop WARNING: An illegal reflective access operation has occurred error on Ubuntu 如何修复警告:eclipse中的org.testng.xml.XMLParser非法反射访问? - how to fix WARNING: Illegal reflective access by org.testng.xml.XMLParser in eclipse? 警告:发生了非法的反射访问操作(java 中的便携式 opencv) - WARNING: An illegal reflective access operation has occurred (portable opencv in java) Retrofit2、maven 项目、非法反射访问警告 - Retrofit2, maven project, Illegal reflective access warning Groovy - 警告:发生了非法反射访问操作 - Groovy - WARNING: An illegal reflective access operation has occurred “警告:发生了非法反射访问操作”,安装了 JDK 8 - “WARNING: An illegal reflective access operation has occurred” with JDK 8 installed groovysh > sdk - java - 警告:发生了非法的反射访问操作 - groovysh > sdk - java - WARNING: An illegal reflective access operation has occurred
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM