Skip to content

shortenFullyQualifiedTypes: skip FQNs whose simple name clashes with types declared in the same file - #3031

Open
maxandersen wants to merge 1 commit into
diffplug:mainfrom
maxandersen:fix-shorten-fqn-same-name-collision
Open

shortenFullyQualifiedTypes: skip FQNs whose simple name clashes with types declared in the same file#3031
maxandersen wants to merge 1 commit into
diffplug:mainfrom
maxandersen:fix-shorten-fqn-same-name-collision

Conversation

@maxandersen

Copy link
Copy Markdown
Contributor

Turns out my original PR missed a case that affects my own projects — ironic! 😅

In jbang, several packages have classes with the same simple name (e.g. dev.jbang.cli.Alias and dev.jbang.catalog.Alias). The code intentionally uses FQNs to disambiguate, but shortenFullyQualifiedTypes() was happily replacing them with the short name and adding an import that fails with "Alias is already defined in this compilation unit". Sorry about that!

Fix: Before deciding which FQNs are safe to shorten, collect all type names declared in the file (classes, interfaces, enums, records — including nested ones). Skip any FQN whose simple name appears in that set.

Tests added:

  • fqnCollisionWithEnclosingClassName — the jbang scenario
  • fqnCollisionWithInnerClassName — simple name matches a nested class
  • fqnNoCollisionWithDifferentSimpleName — confirms non-conflicting FQNs still shorten

… the same file

Skip shortening when the simple name of a fully-qualified type matches
a class, interface, enum, or record declared in the same compilation unit.

Fixes compilation errors in projects like jbang where e.g.
dev.jbang.cli.Alias intentionally uses dev.jbang.catalog.Alias as a FQN
to avoid clashing with the enclosing class name.
@maxandersen

Copy link
Copy Markdown
Contributor Author

original #3005 - @nedtwigg hoping this can be in bugfix release soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant