Reputation: 334
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)
Upvotes: 0
Views: 1579
Reputation: 334
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.
Upvotes: 2