Repository navigation
[java] Fix #7215: Handle qualified types from unresolved on-demand imports - #7217
Conversation
Treat an unresolved field-access qualifier as a possible type from an on-demand import. Add a regression test for a wildcard import used through a qualified member access. Signed-off-by: Eymen <[email protected]>
|
Compared to main: (comment created at 2026-11-13 08:41:06+00:00 for 371119e) |
Signed-off-by: Eymen <[email protected]>
|
I'm not sure the tests are testing what they are supposed to test. There is no Could you please add a C.java to a new subdirectory of pmd-java/src/test/java-resources/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport and adjust your imports? |
|
You’re right, these tests exercise incomplete classpath recovery. I tried adding |
|
Maybe I've misunderstood the issue? I thought this was also happens when the classpath is set up correctly? |
|
Yes, I was wrong. The issue disappears when I set up a maven project. |
adangel
left a comment
There was a problem hiding this comment.
Thanks for working on this! I reviewed the change and found a few issues:
1. Regression: unused-import check now wrongly flags static on-demand imports
recordFailedTypeResWithName(..., onlyStatics=false) clears only typeImportsOnDemand. Before this PR, an unresolved qualifier in an expression (e.g. X in X.foo()) cleared staticImportsOnDemand. Now it clears typeImportsOnDemand instead, which trades one false positive for another:
import static something.Foo.*; // now reported as unused (wasn't before)
public class Example {
void m() { CONST.length(); } // CONST may be a static field from Foo.*
}An unresolved name in this position may be a type from a type-import-on-demand, or a static field / static member type from a static-import-on-demand. So both sets need to be cleared. No existing test covers this, which is why CI and the regression tester didn't catch it.
Comment: Well, the regression tester didn't catch it, because there doesn't seem to be an example of this case in any project we analyze.
2. The new condition is always false
As @oowekyala already noted: AstDisambiguationPass only allows ASTClassType or ASTExpression as the parent of an ASTAmbiguousName (anything else throws an AssertionError). So onlyStatic is now always false, and both the condition and the ASTExpression import can be removed.
3. Tests only cover the incomplete auxclasspath case
As discussed above, the issue only occurs when C can't be resolved, so the two new tests only exercise the incomplete auxclasspath case. Their descriptions should say so. There should also be tests for:
- the static on-demand case from point 1,
- resolved types through a type-import-on-demand (e.g.
import java.util.*;withLocale.ROOT/Calendar.YEAR), - resolved members through a static-import-on-demand (e.g.
import static java.util.Collections.*;/import static java.util.Map.*;withEMPTY_LIST.size()/Entry.comparingByKey()).
I'll take care of these points myself and push the changes to this PR, so no further changes are needed from your side. Thanks again for the contribution!
🤖 Review assisted by Claude Code
…d qualifiers An unresolved ambiguous name may be a type from a type-import-on-demand or a static field / static member type from a static-import-on-demand, so both kinds of on-demand imports must be considered used. Previously only the type imports on demand were cleared, which introduced a false positive for static imports on demand. - Simplify visit(ASTAmbiguousName): the parent is always ASTClassType or ASTExpression, so onlyStatic was always false - Prefix comments about incomplete auxclasspath workarounds with "Incomplete auxclasspath: " - Add tests for static on-demand imports and for resolved types - Add release notes entry for pmd#7215 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixes #7215
Problem
UnnecessaryImport reports an on-demand import as unused when an unresolved qualified field access may refer to a type supplied by that import.
Changes
Tests
./mvnw -pl pmd-java -Dtest=UnnecessaryImportTest -Dsurefire.failIfNoSpecifiedTests=false test./mvnw -q -pl pmd-java -am test./mvnw -pl pmd-java -am -DskipTests verify