-
Notifications
You must be signed in to change notification settings - Fork 11
Static import fix #459
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
Merged
+94
−23
Merged
Static import fix #459
Changes from all commits
Commits
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 hidden or 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
18 changes: 18 additions & 0 deletions
18
src/test/java/org/checkerframework/specimin/StaticWildcardClassImportTest.java
This file contains hidden or 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,18 @@ | ||
| package org.checkerframework.specimin; | ||
|
|
||
| import java.io.IOException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * This test checks that static wildcard imports targeting classes that have used members in the | ||
| * target file are not removed. | ||
| */ | ||
| public class StaticWildcardClassImportTest { | ||
| @Test | ||
| public void runTest() throws IOException { | ||
| SpeciminTestExecutor.runTestWithoutJarPaths( | ||
| "staticwildcardclassimport", | ||
| new String[] {"com/example/Simple.java"}, | ||
| new String[] {"com.example.Simple#foo()"}); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/test/resources/staticwildcardclassimport/expected/com/example/Foo.java
This file contains hidden or 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,7 @@ | ||
| package com.example; | ||
|
|
||
| public class Foo { | ||
| public static void staticFoo() { | ||
| throw new java.lang.Error(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/test/resources/staticwildcardclassimport/expected/com/example/Simple.java
This file contains hidden or 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,9 @@ | ||
| package com.example; | ||
|
|
||
| import static com.example.Foo.*; | ||
|
|
||
| public class Simple { | ||
| void foo() { | ||
| staticFoo(); | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/test/resources/staticwildcardclassimport/input/com/example/Foo.java
This file contains hidden or 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,5 @@ | ||
| package com.example; | ||
|
|
||
| public class Foo { | ||
| public static void staticFoo() { } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/test/resources/staticwildcardclassimport/input/com/example/Simple.java
This file contains hidden or 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,9 @@ | ||
| package com.example; | ||
|
|
||
| import static com.example.Foo.*; | ||
|
|
||
| public class Simple { | ||
| void foo() { | ||
| staticFoo(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Fix logic that skips tracking fully-qualified names in the default package.
When a compilation unit has no package declaration (the default package), the
continuestatement skips the inner loop, meaning noTypeDeclarations from that file are added to the set. Similarly, for class names without a dot,classNameitself is not added to the set.While Java prohibits importing types from the unnamed package—which prevents this bug from affecting the wildcard import removal directly—this logic leaves the
usedPackagesAndClassesset incomplete and could lead to subtle bugs if the set is ever repurposed.🐛 Proposed fix
private static Set<String> getUsedPackagesAndClasses( SliceResult sliceResult, UnsolvedSymbolEnumeratorResult enumeratorResult) { Set<String> usedPackagesAndClasses = new HashSet<>(); for (CompilationUnit cu : sliceResult.solvedSlice()) { - if (cu.getPackageDeclaration().isEmpty()) { - usedPackagesAndClasses.add(""); - continue; - } - usedPackagesAndClasses.add(cu.getPackageDeclaration().get().getNameAsString()); + if (cu.getPackageDeclaration().isPresent()) { + usedPackagesAndClasses.add(cu.getPackageDeclaration().get().getNameAsString()); + } else { + usedPackagesAndClasses.add(""); + } for (TypeDeclaration<?> typeDecl : cu.findAll(TypeDeclaration.class)) { String fqn = typeDecl.getFullyQualifiedName().orElse(null); if (fqn == null) { continue; } usedPackagesAndClasses.add(fqn); } } for (String className : enumeratorResult.classNamesToFileContent().keySet()) { int lastDot = className.lastIndexOf('.'); if (lastDot < 0) { usedPackagesAndClasses.add(""); } else { usedPackagesAndClasses.add(className.substring(0, lastDot)); - usedPackagesAndClasses.add(className); } + usedPackagesAndClasses.add(className); } return usedPackagesAndClasses; }📝 Committable suggestion
🤖 Prompt for AI Agents