-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
fix: Enable creation of packages with repeated simple names in qualified name #4770
Merged
MartinWitt
merged 2 commits into
INRIA:master
from
slarse:issue/4764-fix-package-name-collision
Jul 9, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/test/java/spoon/reflect/factory/PackageFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package spoon.reflect.factory; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import spoon.Launcher; | ||
import spoon.reflect.declaration.CtPackage; | ||
import spoon.test.GitHubIssue; | ||
|
||
class PackageFactoryTest { | ||
|
||
@GitHubIssue(issueNumber = 4764, fixed = true) | ||
void getOrCreate_returnsNestedPackageStructure_whenQualifiedNameRepeatsSimpleName() { | ||
// contract: A qualified name that is simply repetitions of a single simple name results in the expected | ||
// nested package structure | ||
|
||
PackageFactory factory = new Launcher().getFactory().Package(); | ||
String topLevelPackageName = "spoon"; | ||
String nestedPackageName = topLevelPackageName + "." + topLevelPackageName; | ||
|
||
CtPackage packageWithDuplicatedSimpleNames = factory.getOrCreate(nestedPackageName); | ||
CtPackage topLevelPackage = factory.get(topLevelPackageName); | ||
|
||
assertThat(topLevelPackage.getQualifiedName(), equalTo(topLevelPackageName)); | ||
assertThat(packageWithDuplicatedSimpleNames.getQualifiedName(), equalTo(nestedPackageName)); | ||
|
||
assertThat(topLevelPackage.getPackage(topLevelPackageName), sameInstance(packageWithDuplicatedSimpleNames)); | ||
assertThat(packageWithDuplicatedSimpleNames.getParent(), sameInstance(topLevelPackage)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment implies that the package A to which we are trying to add the package B are the same by comparing their qualified names. This simply does not make sense, as the act of add B as a subpackage to A changes its qualified name to
A.B
.It's simply impossible for
A
andB
to end up with the same qualified name after addingB
toA
as the qualified name ofB
will always be prefixed withA.
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was quite confused about the check as well when adjusting this class. It sounded like this was meant to merge equal packages. If I didn't miss anything, the two names can be the same at the time of the check, if you have two parallel package hierarchies. The linking is only done in the
super
call, so the parent isn't yet replaced.I think this could happen with the module system (though the overlap is purely due to spoon's creation of synthetic packages).
I was afraid to touch it back then, but it doesn't seem particularly likely that code depends on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, the names can be the same (e.g.
A = B
, which is precisely the bug reported in #4764). It doesn't matter, however, because the semantics of this method is to addB
as a subpackage ofA
. A subpackage ofA
cannot have the same qualified name asA
, asA
's qualified name is prepended to all of its supbackages. There simply cannot be a name collision; the action that this method performs makes that impossible.It just.. doesn't make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I was trying to convey is, that you can hit this code path in existing code (e.g.):
or if you obtain a second package another way. The existing code would merge this, your code would nest them. This is an observable semantic difference. I just wanted to point out, that this can happen and is not impossible and why I left that code in there when refactoring the ModelSet.
I do agree though, that the new semantics are likely closer to what the user intended and probably won't do any harm. You just might break something, as you change observable behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes I totally get that, I didn't mean that the code path couldn't be hit. It's the source of the bug. The removal of this code path is the bug fix. It's unarguably a bug, because at this time it isn't possible to create packages on the from
A.A
, because when you addA
as a subpackage ofA
the (now removed) code path just merges them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity, if you run
Launcher().getFactory().Package().getOrCreate("A.A")
, you get back a package with the qualified nameA
(before removal of the discussed code path).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I just wanted to make sure that you are aware of the ways this path could be hit in the wild by two valid hierarchies in e.g. different modules coinciding instead of just because they have the same simple name.
Sounds like I might have misunderstood your position then. Sorry about that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no need to apologize, I appreciate your attention to detail :)