java - Java 模块指令如何影响对模块的反射访问?

标签 java reflection java-9 java-module module-info

根据 https://www.oracle.com/corporate/features/understanding-java-9-modules.html ,Java 模块系统引入了以下指令:

  • 导出,导出...到
  • 使用
  • 提供...
  • 打开,打开,打开...

每个指令对使用反射访问内部成员的外部模块有什么影响(如果有的话)?

例如,exports <package>允许外部模块访问所有 public , protected , private使用反射导出包的成员?其他指令呢?

最佳答案

我只想引用 #JLS7.7此处(由我格式化和分类):

Distinct from access at compile time and access at runtime, the Java SE Platform provides reflective access via the Core Reflection API (§1.4).

更多关于您的问题分类为普通模块(module foo)和开放模块(open module bar):

普通模块

A normal module grants reflective access to types in only those packages which are explicitly exported or explicitly opened (or both).

  • 模块的导出包(exports com.example.foo.bar)

    For code outside a normal module, the reflective access granted to types in the module's exported (and not opened) packages is specifically to the public and protected types in those packages, and the public and protected members of those types.

  • 模块打开的包(opens com.example.foo.internal to com.example.bar)

    The reflective access granted to types in the module's opened packages (whether exported or not) is to all types in those packages, and all members of those types.

    No reflective access is granted to types, or their members, in packages which are not exported or opened.

  • 在模块内

    The code inside the module enjoys reflective access to all types, and all their members, in all packages in the module.

打开模块

An open module grants reflective access to types in all its packages, as if all packages had been opened.

  • 模块打开的包

    For code outside an open module, the reflective access granted to types in the module's opened packages (that is, all packages in the module) is to all types in those packages, and all members of those types.

  • 在模块内

    Code inside the module enjoys reflective access to all types, and all their members, in all packages in the module.

关于java - Java 模块指令如何影响对模块的反射访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927375/

相关文章:

java - Payara 5 与 Java 9 的兼容性

java - 如何在我的 Netbeans 项目中包含外部 jar

java - 从类类型实例化 C++ 中的类作为 std::map 值

Java 反射 : Call method from Interface name

c# - 如何从具有 EF 模型的程序集中获取 csdl、ssdl 和 msl 规范

java - 如何在启动时将 JVM 选项传递给 jshell

java - 为什么项目 Jigsaw/JPMS?

java - 为未检查的错误编写错误消息?

java - 仅在不使用 sysout 时才延迟初始化角色集合失败

java - 如何从 Java 9 模块导出所有包?