Reputation: 2184
Let us say I have three public java packages:
my.com.model
my.com.view
my.com.controler
Now I want to restrict which package is allowed to import classes from a dedicated package.
So e.g. the classes from view package are only allowed to import classes from controler package but not from model package and the controler package is only allowed to import classes from model package but not from the view package and the model package has no access at all to the model and view packages.
How can I achieve this? I know that Java does not support such a requirement so maybe some static code analysis tool can help me.
Update: I am using Java 7
Upvotes: 2
Views: 1401
Reputation: 131316
You could define this by extracting each concern in a separated module.
The view module would have a dependency on the controller module and the controller module would have a dependency on the model module.
You could use Maven to achieve these constraints.
You could for example define a reactor or a multi module Maven project with 3 modules.
Maven provides transitive dependencies for a declared dependency. So to respect fully your requirements, the view
module should exclude the transitive model
dependency that is pulled by the controller
dependency .
Upvotes: 2