且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以通过Jigsaw在Java 9中通过反射访问包范围的方法?

更新时间:2023-12-01 20:06:10

在早期的原型中它曾经是可能的,但现在已经不存在了.拼图的可访问性规则现在仅限制对public元素(类型,方法,字段)的访问.

It used to be possible in an early prototype but it is no longer. Jigsaw's accessibility rules now restrict access to public elements (types, methods, fields) only.

在您的示例中,对method.setAccessible(true)的调用将失败,并显示类似于以下内容的消息:

In your example the call to method.setAccessible(true) will fail with a message similar to this one:

java.lang.reflect.InaccessibleObjectException:无法使getURLStreamHandler可访问:模块java ....不会打开Java ...."到未命名的模块@ 1941a8ff

java.lang.reflect.InaccessibleObjectException: Unable to make getURLStreamHandler accessible: module java.... does not "opens java...." to unnamed module @1941a8ff

有关此问题的解决方法,请参见此问题.

See this question for how to work around that.