what is an illegal reflective access

前端 未结 4 1615
南笙
南笙 2020-11-22 15:17

There are a lot of questions around about illegal reflective access in Java 9.

Now what I can\'t find because all Google spews up is people trying to work around the

4条回答
  •  無奈伤痛
    2020-11-22 16:08

    There is an Oracle article I found regarding Java 9 module system

    By default, a type in a module is not accessible to other modules unless it’s a public type and you export its package. You expose only the packages you want to expose. With Java 9, this also applies to reflection.

    As pointed out in https://stackoverflow.com/a/50251958/134894, the differences between the AccessibleObject#setAccessible for JDK8 and JDK9 are instructive. Specifically, JDK9 added

    This method may be used by a caller in class C to enable access to a member of declaring class D if any of the following hold:

    • C and D are in the same module.
    • The member is public and D is public in a package that the module containing D exports to at least the module containing C.
    • The member is protected static, D is public in a package that the module containing D exports to at least the module containing C, and C is a subclass of D.
    • D is in a package that the module containing D opens to at least the module containing C. All packages in unnamed and open modules are open to all modules and so this method always succeeds when D is in an unnamed or open module.

    which highlights the significance of modules and their exports (in Java 9)

提交回复
热议问题