-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaPackage#getPackageDependenciesFromSelf returns dependencies from both self AND subpackages #919
Comments
Thanks for raising this issue! The behavior actually was implemented like this intentionally 🤔 Because that would have been my expectation of the API 😂 I admit that the javadoc does not specify this 😞 |
I noticed this when trying to implement a "a package should not depend on its subpackages" rule (reasoning: https://javadevguy.wordpress.com/2017/12/18/happy-packaging/). What I was trying to do was something like this:
This resulted in a number of false positives, where a package was reported to depend on its subpackages even if there was no direct dependency between the package and subpackage. It is likely that it's useful to have access to both all dependencies and just direct dependencies depending on the context - similar to At the same time, this could be worked around by using |
I ended up implementing the rule I wanted in a slightly different way (shared here: #921), which did require the methods of In any case, I think I could do something here, and the options would probably be:
|
I thought about it, and I think it makes sense to offer both these alternatives in the API. The only problem for me is, to be consistent with other naming it would have to be something like So I thought more about it and ended up with these two naming ideas:
It would break the naming schema a little, but maybe that's even good. Because in the end it is also a slightly different case. It is an aggregate and in no other place to we have this ambiguity, that it could refer to the flat package or the package plus subpackages. And I also think it would be way better to break the API in a non-compiling way with |
Opened a PR with the proposed fix. Though it seems the root problem here might be that the concept of Examples of methods considering just the current package: Additionally, I think the term "subpackage" in Java means "direct child", as seen in Java Language Specification:
^ Note that Maybe a proper solution would after all be something along the lines of:
This would be a major change though. |
Yes, I agree with your observations 🤔 Also, that it might be more precise to have two objects. The only think I'm wondering about is, if this then really makes the API that users find clear and easy to use 🤔 |
This breaking change will clear up some semantic ambiguities around the methods of `JavaPackage` that deal with subpackages. In particular, we will now consider dependencies from/to a package only those where the origin/target class **directly** resides within the package. Subpackages are considered only those packages that reside **directly** within a package. Everything that considers all classes within the package and all subpackages recursively (i.e. subpackages of subpackages and so on) will now refer to the term "package tree". Changes: * Added `getClassDependencies{From/To}ThisPackage` * Renamed `getClassDependencies{From/To}Self` to `getClassDependencies{From/To}ThisPackageTree` * Added `getPackageDependencies{From/To}ThisPackage` * Renamed `getPackageDependencies{From/To}Self` to `getPackageDependencies{From/To}ThisPackageTree` * Renamed `getAllSubpackages` to `getSubpackagesInTree` * Renamed `accept(..., ClassVisitor/PackageVisitor)` to `traversePackageTree(..., ClassVisitor/PackageVisitor)` Resolves: #919
In
JavaPacakge
:getPackageDependenciesFromSelf
collects packages of classes returned bygetClassDependenciesFromSelf
getClassDependenciesFromSelf
collects dependencies originating from classes returned bygetAllClasses
getAllClasses
collects classes from both the current package AND all subpackages (descendants)This caught me by surprise - I expected
getPackageDependenciesFromSelf
to only return the package dependencies originating from classes in the current package (i.e. excluding any subpackages).At first glance it would seem that just replacing
getAllClasses
withgetClasses
ingetClassDependenciesFrom/ToSelf
would make it match my expectation.If this is a bug, I'd be happy to open a PR with a fix.
The text was updated successfully, but these errors were encountered: