Controlling package dependencies in Java (Eclipse)
I have a Java project (in Eclipse) consisting of several packages. I would like to control the dependencies between packages so that, for example, one package couldn't use some other package in the project. The reason for this is that I am going to make a self-contained jar from a subset of packages.
Do I have to separate my project into several to achieve this or is there some开发者_运维百科 tool or Eclipse plugin that does it?
Well since Aspectj is now an Eclipse Project, I guess an Aspectj solution qualifies as well. AspectJ is great for policy enforcements like that, because it lets you create compile-time warnings and errors based on pointcuts. And if you use the AspectJ Developer Tools, you get aspects up and running in Eclipse.
Here's a sample Policy Enforcement Aspect:
public aspect PolicyEnforcement {
pointcut project1Call() : call(* com.project1..*.*(..));
pointcut inProject1() : within(com.project1..*);
pointcut project2Call() : call(* com.project2..*.*(..));
pointcut inProject2() : within(com.project2..*);
declare error : project1Call() && inProject2()
: "Project2 is not allowed to access Project1";
declare error : project2Call() && inProject1()
: "Project1 is not allowed to access Project2";
}
Note: It's perfectly OK to use AspectJ like that during the development phase only without causing any runtime dependencies.
精彩评论