Keep imports of qualifier types and static fields in expandWildcardImports - #3114
Open
SulimanAbdulrazzaq wants to merge 1 commit into
Open
SulimanAbdulrazzaq wants to merge 1 commit into
SulimanAbdulrazzaq wants to merge 1 commit into
Conversation
…ports A type from a wildcard import that is only used to qualify a member, such as Collections in Collections.sort(list) or TimeUnit in TimeUnit.SECONDS, was never collected, so its import was dropped and the result did not compile. The same happened to static fields and enum constants brought in by a static wildcard import, such as PI from import static java.lang.Math.*. Resolve simple names: a static field or enum constant is matched against static wildcard imports, and a name that qualifies a method call, field access or method reference and is not a value is resolved as a type and matched against the regular wildcard imports.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
expandWildcardImportsdrops imports that the code still needs, so the output does not compile (#2833).Two cases were missed:
Collections.sort(values)orTimeUnit.SECONDS.JSON.toJSONString(...)from the issue is the same case. The visitor only collectedClassOrInterfaceTypenodes, annotations and the declaring type of static methods. A qualifier likeCollectionsis aNameExpr, and resolving the methodsortgives onlyjava.util.Collections.sort, which was matched against static wildcard imports. Soimport java.util.*;expanded toimport java.util.List;andCollectionslost its import.PIfromimport static java.lang.Math.*;orSECONDSfromimport static java.util.concurrent.TimeUnit.*;. Only static method calls were collected, soimport static java.lang.Math.*;expanded toimport static java.lang.Math.max;andPIno longer compiled.The visitor now also visits
NameExpr:Names that do not resolve are skipped, so the first segment of a fully qualified name such as
java.util.List.of()and local variables used as qualifiers change nothing.Tests: two new cases in
ExpandWildcardImportsStepTest, as resource files next to the existingJavaClassWithWildcards*.testones. Once the test file is touched, the ratchetedforbidWildcardImportslint checks the whole file. The two existing inline cases (emptyClasspath,resolvesJdkXmlTypes) would then fail that lint, so their sources moved to resource files too, unchanged.Before the fix, the new tests fail. For example,
keepsTypesOnlyUsedAsQualifiersgets onlyimport java.util.List;, andkeepsStaticFieldsAndEnumConstantsgets onlyimport static java.lang.Math.max;. After the fix:./gradlew spotlessCheckpasses;./gradlew :lib:build :testlib:testpasses on Java 21 (482 tests, 0 failures);:plugin-gradle:test --tests JavaDefaultTargetTestpasses.Changelog entries are added to
CHANGES.md,plugin-gradle/CHANGES.mdandplugin-maven/CHANGES.md.Fixes #2833