[英]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
[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.