From 04b1be7f4920d591c66147b9d3b40cb7952a066e Mon Sep 17 00:00:00 2001 From: aKandzior Date: Fri, 8 May 2026 14:13:59 +0200 Subject: [PATCH 1/2] Fix wildcard import expansion for JDK XML types expandWildcardImports uses JavaParser symbol resolution to inspect the whole compilation unit before replacing wildcard imports. The default ReflectionTypeSolver only covers java.* and javax.* APIs, so JDK classes in packages such as org.xml.sax could not be resolved. This caused formatting to fail with an UnsolvedSymbolException when a file contained both a wildcard import and a JDK XML type such as org.xml.sax.InputSource. Use ReflectionTypeSolver.JCL_ONLY so the resolver covers the full Java class library package set known to JavaParser, including org.xml.sax. Adds a regression test with org.xml.sax.InputSource and a java.io.* wildcard import. Test: ./gradlew :testlib:test --tests com.diffplug.spotless.java.ExpandWildcardImportsStepTest --no-daemon --no-configuration-cache --console=plain --- .../ExpandWildcardsFormatterFunc.java | 2 +- .../java/ExpandWildcardImportsStepTest.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java b/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java index fdde8eb362..d5c7dd1956 100644 --- a/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java +++ b/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java @@ -70,7 +70,7 @@ public ExpandWildcardsFormatterFunc(Collection typeSolverClasspath) throws this.parser = new JavaParser(); CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(); - combinedTypeSolver.add(new ReflectionTypeSolver()); + combinedTypeSolver.add(new ReflectionTypeSolver(ReflectionTypeSolver.JCL_ONLY)); for (File element : typeSolverClasspath) { if (element.isFile()) { combinedTypeSolver.add(new JarTypeSolver(element)); diff --git a/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java index c71be50bc6..723429081d 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/ExpandWildcardImportsStepTest.java @@ -150,6 +150,39 @@ public class Test { StepHarness.forStep(step).test(simpleCode, expectedOutput); } + @Test + void expandWildcardImports_resolvesJdkXmlTypes() throws Exception { + FormatterStep step = ExpandWildcardImportsStep.create(Collections.emptySet(), TestProvisioner.mavenCentral()); + + String simpleCode = """ + package test; + + import java.io.*; + import org.xml.sax.InputSource; + + public class Test { + InputSource inputSource(String value) { + return new InputSource(new StringReader(value)); + } + } + """; + + String expectedOutput = """ + package test; + + import java.io.StringReader; + import org.xml.sax.InputSource; + + public class Test { + InputSource inputSource(String value) { + return new InputSource(new StringReader(value)); + } + } + """; + + StepHarness.forStep(step).test(simpleCode, expectedOutput); + } + @Test void expandWildcardImports_nullSafety() throws Exception { // Test null safety From ed0eddc704e07f607fe884ee4891f25da189a1bc Mon Sep 17 00:00:00 2001 From: aKandzior Date: Fri, 8 May 2026 15:30:09 +0200 Subject: [PATCH 2/2] Update changelog for wildcard import fix --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 1 + 3 files changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 3bb2ae6f56..d820fa667a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added - Add `javaparserVersion` option to the Cleanthat step, allowing callers to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903)) ### Changes +- Fix `expandWildcardImports` failing on JDK XML types such as `org.xml.sax.InputSource`. ([#2921](https://github.com/diffplug/spotless/pull/2921)) - Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903)) - Bump default `eclipse-jdt` version from `4.35` to `4.39`. ([#2912](https://github.com/diffplug/spotless/pull/2912)) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e939b70b70..74a7637865 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -9,6 +9,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed - Fix `tableTestFormatter` editorconfig cache not honoring `.editorconfig` changes across Gradle daemon runs due to a shared static `EditorConfigProvider`. ([#2893](https://github.com/diffplug/spotless/pull/2893)) ### Changes +- Fix `expandWildcardImports` failing on JDK XML types such as `org.xml.sax.InputSource`. ([#2921](https://github.com/diffplug/spotless/pull/2921)) - Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903)) - Bump default `eclipse-jdt` version from `4.35` to `4.39`. ([#2912](https://github.com/diffplug/spotless/pull/2912)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 70f0ec99c7..39a5c69b31 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added - Add `` option to ``, allowing users to override the JavaParser version pulled in transitively by Cleanthat. ([#2903](https://github.com/diffplug/spotless/pull/2903)) ### Changes +- Fix `expandWildcardImports` failing on JDK XML types such as `org.xml.sax.InputSource`. ([#2921](https://github.com/diffplug/spotless/pull/2921)) - Bump default `cleanthat` version `2.24` -> `2.25`. ([#2903](https://github.com/diffplug/spotless/pull/2903)) - Bump default `eclipse-jdt` version from `4.35` to `4.39`. ([#2912](https://github.com/diffplug/spotless/pull/2912))