我试图在我的一个项目中使用 Java 10 和 intellij,但遇到了警告。
它说我没有导出类服务器。如果您看到我的包结构,我在 network
包中有服务器,我的模块信息文件如下所示
module core {
requires com.google.guice;
requires io.netty.all;
requires config;
requires org.apache.logging.log4j;
exports com.money.heist.server.core;
opens com.money.heist.server.core.network to com.google.guice;
}
我有意不导出包网络,因为我不想这样做,但我想在父包内的网络包内使用类。
他们在这里有什么好的/坏的做法,还是只是 intelliJ 发疯了?
最佳答案
如果你将警告信息展开,你会找到它背后的原因
In addition to that, in Java 9 a module may hide some of its classes by not exporting their packages.
If the public API of a class in an exported package references a class from a non-exported package, such API isn't useful outside of the module.
这样的 API 没有用的原因是没有其他模块可以实例化/访问 Server
类。
请注意,在您的模块描述符中,您已经包含
opens com.money.heist.server.core.network to com.google.guice;
这将在运行时而不是编译时提供访问(可能是 IntelliJ 不感知它的原因),public
和 protected
包中的类型,以及这些类型的 public
和 protected
成员到它们打开的模块。
与上述相关,如果您将 opens
指令更改为 exports
,您将不会进一步看到来自 IntelliJ 的警告。
关于Java 10,Intellij 警告(类未导出,在同一模块中访问),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755914/