我可以阻止一个Java包从导入特定的其他包吗?

huangapple go评论68阅读模式
英文:

Can I prevent a package from importing specific other packages in Java

问题

Java,或者Eclipse(或Eclipse插件)是否具有一种功能,在同一个Java项目中,无论公共类的可见性如何,都可以防止一个包命名空间使用另一个包命名空间?

例如,包"myapp.model"绝不可以从"myapp.fxview"导入。

背景

我正在使用Java和Eclipse编写一个使用“模型-视图-控制器”架构的应用程序。

"myapp.model"包含纯Java代码,没有外部库或特定于平台的代码。
"myapp.fxview"包含覆盖JavaFX类的类。

我希望确保我的模型代码不会受到视图代码的影响(例如,意外地在我的模型中使用“视图”实现中的枚举),以便我可以在多个平台(JavaFX PC、Android、Web服务器后端)上使用我的模型。

我希望将所有代码都放在一个项目中,以最小化处理项目/工作区设置问题的时间。(我知道使用单独的项目可以轻松解决这个“问题”)

英文:

Does Java, or eclipse (or an eclipse plugin) have a feature to prevent one package namespace from using another when the code is all located within the same Java project, regardless of public class visibility?

e.g. package "myapp.model" may never import from "myapp.fxview"

Background

I am writing an application using a "Model View Controller" architechture in Java using eclipse.

"myapp.model" contains pure Java code, no external libraries or platform specific code
"myapp.fxview" contains a classes overriding JavaFX classes

I want to ensure that my Model code does not become "contaminated" with dependencies to the View code (for example accidentallly using an enum from the "view" implementation in my Model) so that I can use my Model accross multiple platforms (JavaFX PC, Android, WebServer Backend)

I would like to keep all my code in one project to minimise time messing around with project / workspace setup issues. (I am aware taht this "issue" would be easy to resolve using seperate projects)

答案1

得分: 2

使用Java 9或更新版本中提供的模块系统。有诸如“exports...to”或“opens...to”的模块声明。通过使用这些声明,您可以明确限制哪些包在何处可用。为了支持这一点,最好重新构造包名称,以表示可能有多个视图(myapp.fxview -> myapp.view.fx)。

这段未经测试的代码大致显示了其外观:

module modelmodule {
    exports myapp.model to myapp.view.fx;
    exports myapp.model to myapp.application;
}

module viewmodule {
    exports myapp.view.fx to myapp.application;
}

更多信息请参见:https://www.oracle.com/corporate/features/understanding-java-9-modules.html

感谢评论中的Thomas,为我指出了正确的方向。我在工作中一直在使用Java 8,因此在家也是如此。因此,直到现在我才了解到模块系统。

英文:

By using the Modules system available in Java 9 or newer. There are module declerations such as "exports...to", or "opens...to". By using these you can specifically restrict which packages are available where. To support this, better restructure the package names to represent that there may be multiple views (myapp.fxview -> myapp.view.fx).

This untested code shows roughly how this would look

module modelmodule {
    exports myapp.model to myapp.view.fx;
    exports myapp.model to myapp.application;
}

module viewmodule {
    exports myapp.view.fx to myapp.application;
}

@see https://www.oracle.com/corporate/features/understanding-java-9-modules.html

Thanks to Thomas in the comments for pointing me in the correct direction. I have been using Java 8 at work and as such stuck to the same at home. Hence, I had no idea of the Modules system until now.

huangapple
  • 本文由 发表于 2020年8月27日 23:32:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63619416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定