From 341410cdbe7be4a6723e8aa064b53e746a84da79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Fri, 2 Oct 2026 21:07:52 +0800 Subject: [PATCH 1/3] Add JRE 27 to the CI test matrix --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f34b197ce0..39a74b1646 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,7 +46,7 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - jre: [ 17, 21, 25, 26 ] # Test on all supported LTS and latest JDK. + jre: [ 17, 21, 25, 26, 27 ] # Test on all supported LTS and latest JDK. include: - os: windows-latest jre: 26 # Test on the latest JDK for speeds. @@ -77,7 +77,7 @@ jobs: fail-fast: false matrix: os: [ ubuntu-latest ] - jre: [ 17, 21, 25, 26 ] # Test on all supported LTS and latest JDK. + jre: [ 17, 21, 25, 26, 27 ] # Test on all supported LTS and latest JDK. include: - os: windows-latest jre: 26 # Test on the latest JDK for speeds. From b8ae36893a20a5ce16d822477552367cbfcc3e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Fri, 2 Oct 2026 20:56:06 +0800 Subject: [PATCH 2/3] Avoid hardcoded com.sun.* package names ModuleHelper opened a hardcoded list of JDK-internal packages and probed a hardcoded class of each one. Enumerate the packages of the jdk.compiler module instead, and open the ones which aren't open yet, so that no JDK-internal name has to be hardcoded. Because spotless opens those packages itself, the plugin-maven test harness no longer needs to add --add-exports for them, and the import order test fixtures use com.jna.* instead of com.sun.jna.*. --- .../diffplug/spotless/java/ModuleHelper.java | 88 +++++++------------ .../maven/MavenIntegrationHarness.java | 12 +-- .../JavaCodeSortedLexicographic.test | 4 +- .../JavaCodeSortedSemanticSort.test | 4 +- .../JavaCodeUnsortedSemanticSort.test | 4 +- .../spotless/java/ImportOrderStepTest.java | 2 +- 6 files changed, 40 insertions(+), 74 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/java/ModuleHelper.java b/lib/src/main/java/com/diffplug/spotless/java/ModuleHelper.java index db0cc2c170..f0bcc4cf28 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/ModuleHelper.java +++ b/lib/src/main/java/com/diffplug/spotless/java/ModuleHelper.java @@ -22,36 +22,30 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Map; - -import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.diffplug.spotless.Jvm; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import sun.misc.Unsafe; +/** + * Some Java formatters (google-java-format, palantir-java-format, cleanthat) need access to + * the internal packages of the JDK compiler module. Instead of hardcoding those package names, + * which are JDK internals that may be renamed or removed, we enumerate and open every package + * of that module. + */ final class ModuleHelper { private static final Logger LOGGER = LoggerFactory.getLogger(ModuleHelper.class); + /** The module which owns the packages that the Java formatters above require. */ + private static final String REQUIRED_MODULE = "jdk.compiler"; + // prevent direct instantiation private ModuleHelper() {} - private static final Map REQUIRED_PACKAGES_TO_TEST_CLASSES = new HashMap<>(); - - static { - REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.util", "Context"); - REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.file", "CacheFSInfo"); - REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.tree", "TreeTranslator"); - REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.parser", "Tokens$TokenKind"); - REQUIRED_PACKAGES_TO_TEST_CLASSES.putIfAbsent("com.sun.tools.javac.api", "DiagnosticFormatter$PositionKind"); - } - private static boolean checkDone; public static synchronized void doOpenInternalPackagesIfRequired() { @@ -67,9 +61,8 @@ public static synchronized void doOpenInternalPackagesIfRequired() { if (!failedToOpen.isEmpty()) { final StringBuilder message = new StringBuilder(); message.append("WARNING: Some required internal classes are unavailable. Please consider adding the following JVM arguments\n"); - message.append("WARNING: "); for (String name : failedToOpen) { - message.append("--add-opens jdk.compiler/%s=ALL-UNNAMED".formatted(name)); + message.append("WARNING: --add-opens %s/%s=ALL-UNNAMED%n".formatted(REQUIRED_MODULE, name)); } LOGGER.warn("{}", message); } @@ -79,34 +72,31 @@ public static synchronized void doOpenInternalPackagesIfRequired() { } } - @SuppressFBWarnings("REC_CATCH_EXCEPTION") // workaround JDK11 + /** @return the packages of {@link #REQUIRED_MODULE} which are not open to the module calling this code. */ private static List unavailableRequiredPackages() { + final Module callerModule = ModuleHelper.class.getModule(); final List packages = new ArrayList<>(); - for (Map.Entry e : REQUIRED_PACKAGES_TO_TEST_CLASSES.entrySet()) { - final String key = e.getKey(); - final String value = e.getValue(); - try { - final Class clazz = Class.forName(key + "." + value); - if (clazz.isEnum()) { - clazz.getMethod("values").invoke(null); - } else { - clazz.getDeclaredConstructor().newInstance(); + for (Module module : requiredModules()) { + for (String name : module.getPackages()) { + if (!module.isOpen(name, callerModule)) { + packages.add(name); } - } catch (IllegalAccessException ex) { - packages.add(key); - } catch (Exception ignore) { - // in old versions of JDK some classes could be unavailable } } return packages; } - @SuppressWarnings("unchecked") - private static void openPackages(Collection packagesToOpen) throws Throwable { - final Collection modules = allModules(); - if (modules == null) { - return; + private static List requiredModules() { + final List modules = new ArrayList<>(); + for (Module module : ModuleLayer.boot().modules()) { + if (REQUIRED_MODULE.equals(module.getName())) { + modules.add(module); + } } + return modules; + } + + private static void openPackages(Collection packagesToOpen) throws Throwable { final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe"); unsafeField.setAccessible(true); final Unsafe unsafe = (Unsafe) unsafeField.get(null); @@ -115,30 +105,14 @@ private static void openPackages(Collection packagesToOpen) throws Throw unsafe.staticFieldBase(implLookupField), unsafe.staticFieldOffset(implLookupField)); final MethodHandle modifiers = lookup.findSetter(Method.class, "modifiers", Integer.TYPE); - final Method exportMethod = Class.forName("java.lang.Module").getDeclaredMethod("implAddOpens", String.class); - modifiers.invokeExact(exportMethod, Modifier.PUBLIC); - for (Object module : modules) { - final Collection packages = (Collection) module.getClass().getMethod("getPackages").invoke(module); - for (String name : packages) { + final Method addOpensMethod = Module.class.getDeclaredMethod("implAddOpens", String.class); + modifiers.invokeExact(addOpensMethod, Modifier.PUBLIC); + for (Module module : requiredModules()) { + for (String name : module.getPackages()) { if (packagesToOpen.contains(name)) { - exportMethod.invoke(module, name); + addOpensMethod.invoke(module, name); } } } } - - @Nullable @SuppressFBWarnings("REC_CATCH_EXCEPTION") // workaround JDK11 - private static Collection allModules() { - // calling ModuleLayer.boot().modules() by reflection - try { - final Object boot = Class.forName("java.lang.ModuleLayer").getMethod("boot").invoke(null); - if (boot == null) { - return null; - } - final Object modules = boot.getClass().getMethod("modules").invoke(boot); - return (Collection) modules; - } catch (Exception ignore) { - return null; - } - } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index bcc5791a5b..652f96f431 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -91,16 +91,8 @@ public class MavenIntegrationHarness extends ResourceHarness { void gitAttributes() throws IOException { setFile(".gitattributes").toContent("* text eol=lf"); if (Jvm.version() >= 16) { - // for GJF https://github.com/diffplug/spotless/issues/834 - setFile(".mvn/jvm.config").toContent( - "--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" - + " --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" - + " --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" - + " --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" - + " --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" - // this last line is for Detekt - + // this last line is for Detekt - " --add-opens java.base/java.lang=ALL-UNNAMED"); + // this line is for Detekt + setFile(".mvn/jvm.config").toContent("--add-opens java.base/java.lang=ALL-UNNAMED"); } // copy the mvnw resources copy("mvnw").setExecutable(true); diff --git a/testlib/src/main/resources/java/importsorter/JavaCodeSortedLexicographic.test b/testlib/src/main/resources/java/importsorter/JavaCodeSortedLexicographic.test index 2b59d52e58..01bc1d736c 100644 --- a/testlib/src/main/resources/java/importsorter/JavaCodeSortedLexicographic.test +++ b/testlib/src/main/resources/java/importsorter/JavaCodeSortedLexicographic.test @@ -13,5 +13,5 @@ import com.example.Test.Nested; import com.example.a.A; import com.example.b; import com.example.c.C; -import com.sun.jna.platform.win32.COM.Unknown; -import com.sun.jna.platform.win32.Guid.CLSID; +import com.jna.platform.win32.COM.Unknown; +import com.jna.platform.win32.Guid.CLSID; diff --git a/testlib/src/main/resources/java/importsorter/JavaCodeSortedSemanticSort.test b/testlib/src/main/resources/java/importsorter/JavaCodeSortedSemanticSort.test index 3ab813b227..8833f2beba 100644 --- a/testlib/src/main/resources/java/importsorter/JavaCodeSortedSemanticSort.test +++ b/testlib/src/main/resources/java/importsorter/JavaCodeSortedSemanticSort.test @@ -13,5 +13,5 @@ import com.example.Test.Nested; import com.example.b; import com.example.a.A; import com.example.c.C; -import com.sun.jna.platform.win32.Guid.CLSID; -import com.sun.jna.platform.win32.COM.Unknown; +import com.jna.platform.win32.Guid.CLSID; +import com.jna.platform.win32.COM.Unknown; diff --git a/testlib/src/main/resources/java/importsorter/JavaCodeUnsortedSemanticSort.test b/testlib/src/main/resources/java/importsorter/JavaCodeUnsortedSemanticSort.test index 7bfcfc6f7a..ad650c0e52 100644 --- a/testlib/src/main/resources/java/importsorter/JavaCodeUnsortedSemanticSort.test +++ b/testlib/src/main/resources/java/importsorter/JavaCodeUnsortedSemanticSort.test @@ -7,8 +7,8 @@ import com.example.Test.Nested; import com.example.Test; import com.example.Test.*; -import com.sun.jna.platform.win32.COM.Unknown; -import com.sun.jna.platform.win32.Guid.CLSID; +import com.jna.platform.win32.COM.Unknown; +import com.jna.platform.win32.Guid.CLSID; import com.example.a.A; import com.example.b; diff --git a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java index 03e17bdc15..1e117a22f6 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/ImportOrderStepTest.java @@ -105,7 +105,7 @@ void lexicographicSort() { @Test void semanticSort() { - FormatterStep step = ImportOrderStep.forJava().createFrom(false, true, Set.of("com.sun.jna.platform.win32.COM"), + FormatterStep step = ImportOrderStep.forJava().createFrom(false, true, Set.of("com.jna.platform.win32.COM"), Set.of("com.example.b"), createTestFile("java/importsorter/import.properties")); StepHarness.forStep(step).testResource("java/importsorter/JavaCodeUnsortedSemanticSort.test", "java/importsorter/JavaCodeSortedSemanticSort.test"); } From cffccda43cb4fa56a1b88e6a23ed88d67cb0b12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Fri, 2 Oct 2026 21:26:46 +0800 Subject: [PATCH 3/3] Document removal of hardcoded com.sun.* package names (#3128) --- 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 127d1f839f..4b403a4a36 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Changes - Bump default Eclipse JDT formatter version from `4.40` to `4.41`. +- Java formatters (google-java-format, palantir-java-format, cleanthat) no longer rely on a hardcoded list of `com.sun.*` internal packages. The required packages are discovered in the `jdk.compiler` module at runtime, which improves support for newer JDKs such as JDK 27. ([#3128](https://github.com/diffplug/spotless/issues/3128)) ## [4.10.3] - 2026-09-25 diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c0955726a6..b512269911 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Changes - Bump default `eclipse` version from `4.40` to `4.41`. +- No longer hardcode the `com.sun.*` internal packages required by the Java formatters, improving support for newer JDKs such as JDK 27. ([#3128](https://github.com/diffplug/spotless/issues/3128)) ## [8.10.3] - 2026-09-25 ### Changes diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index a5fd72c780..453730e684 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 ( ### Changes - Bump default `eclipse` version from `4.40` to `4.41`. +- No longer hardcode the `com.sun.*` internal packages required by the Java formatters, improving support for newer JDKs such as JDK 27. ([#3128](https://github.com/diffplug/spotless/issues/3128)) ## [3.10.3] - 2026-09-25 ### Changes