Java 10, Intellij Warning (class is not exported, accessing in same module)

Sneh :

I am trying to use Java 10 with intellij in one of my project and came across a warning.

enter image description here

It is saying that I am not exporting class Server. If you see my package structure, I have Server inside network package my module-info file looks like this

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;
}

I am intentionally not exporting the package network as I don't want to do that but I want to use classes inside network package inside the parent package.

Is their any good/bad practice here or it is just intelliJ going crazy?

Naman :

If you would expand the warning message, you would find the reason behind it

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.

The reason such API isn't useful is that no other module can instantiate/access Server class.

Noticeably, in your module descriptor, you've included

opens com.money.heist.server.core.network to com.google.guice;

which would provide access at run time, but not compile time(possibly the reason why IntelliJ doesn't sense it), to the public and protected types in the package, and the public and protected members of those types to the modules they are opened to.

To relate to the above, if you change the opens directive to exports, you wouldn't further see the warning from IntelliJ.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100984&siteId=1