Skip to content

Support Java 22-26 language features#1707

Open
asm0dey wants to merge 3 commits into
palantir:developfrom
asm0dey:feature/java-26-language-support
Open

Support Java 22-26 language features#1707
asm0dey wants to merge 3 commits into
palantir:developfrom
asm0dey:feature/java-26-language-support

Conversation

@asm0dey

@asm0dey asm0dey commented Jul 3, 2026

Copy link
Copy Markdown

Before this PR

palantir-java-format supports Java language features through Java 21. Source code using features finalized in Java 22–26 does not format correctly — several cases crash the formatter outright:

  • import module java.base; (module import declarations) throws expected token: 'module'.
  • Compact source files / instance main methods crash (the implicit top-level class emits a non-existent class token).
  • Unnamed patterns in deconstruction, e.g. case Box(_, _), crash on the AnyPatternTree node.

After this PR

The formatter handles Java language features finalized through Java 26:

  • Module import declarations import module M; — JEP 511 (kept as a leading, blank-line-separated import group; never stripped as "unused").
  • Compact source files and instance main methods — JEP 512.
  • Unnamed patterns in deconstruction, e.g. case Box(_, _) — JEP 456 (fixes the crash above).
  • Markdown documentation comments /// — JEP 467 (preserved verbatim, including fenced code blocks and {@snippet} content).
  • Flexible constructor bodies — JEP 513.

Dispatch selects Java25InputAstVisitor / Java26InputAstVisitor by runtime major version, with a graceful fallback to the existing Java21InputAstVisitor, so consumers on older JDKs are unaffected. Every javac API newer than Java 17 is reached via reflection / flag-bit detection, so the library still compiles at libraryTarget = 17. Formatter tests run on a JDK 21/22/23/25/26 matrix so each visitor-dispatch tier and each feature's GA boundary is exercised natively; all other modules stay on JDK 21.

Fixes

==COMMIT_MSG==
Support Java 22–26 language features: module import declarations (JEP 511), compact source files and instance main methods (JEP 512), unnamed patterns in deconstruction (JEP 456), markdown documentation comments (JEP 467), and flexible constructor bodies (JEP 513). Visitors are selected by runtime major version with graceful fallback to Java 21, and post-17 javac APIs are accessed reflectively so the library still targets Java 17. Formatter tests run on a JDK 21/22/23/25/26 matrix.
==COMMIT_MSG==

Possible downsides?

  • Formatting Java 25/26-specific syntax requires running the formatter on a matching JDK — an inherent constraint of parsing with javac internals. Earlier syntax, and consumers on JDK 17+, are unaffected.
  • ./gradlew build now provisions JDK 22/23/25/26 toolchains for the test matrix, which increases first-run CI provisioning cost.

Disclosure: this change was prepared with AI assistance (Claude Code). It has been reviewed, and the full test suite passes across the JDK 21/22/23/25/26 matrix.

asm0dey added 3 commits July 3, 2026 18:33
The formatter parses with javac internals, so Java 25/26 syntax can only
be exercised when the tests run on a matching JDK. Register azul-zulu
JDK 22/25/26 toolchains and add per-tier testJdkNN tasks so each
visitor-dispatch branch (Java21/25/26) and each feature's GA boundary is
exercised natively, while every other module stays on JDK 21. The
published library still targets Java 17 (libraryTarget unchanged).
Add formatting support for language features finalized through Java 26:

- Module import declarations (import module M;) — JEP 511
- Compact source files and instance main methods — JEP 512
- Unnamed patterns in deconstruction (case Box(_, _)) — JEP 456, fixes a
  formatter crash on the AnyPatternTree node
- Markdown documentation comments (///) — JEP 467
- Flexible constructor bodies — JEP 513

Formatter selects Java25InputAstVisitor / Java26InputAstVisitor by runtime
major version, with a graceful fallback to the existing Java21 visitor so
older runtimes are unaffected. All javac APIs newer than Java 17 are
reached via reflection / flag-bit detection, so the library keeps
compiling at libraryTarget 17.
@palantirtech

Copy link
Copy Markdown
Member

Thanks for your interest in palantir/palantir-java-format, @asm0dey! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@changelog-app

changelog-app Bot commented Jul 3, 2026

Copy link
Copy Markdown

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Support Java 22–26 language features: module import declarations (JEP 511), compact source files and instance main methods (JEP 512), unnamed patterns in deconstruction (JEP 456), markdown documentation comments (JEP 467), and flexible constructor bodies (JEP 513). Visitors are selected by runtime major version with graceful fallback to Java 21, and post-17 javac APIs are accessed reflectively so the library still targets Java 17. Formatter tests run on a JDK 21/22/23/25/26 matrix.

Check the box to generate changelog(s)

  • Generate changelog entry

@asm0dey asm0dey marked this pull request as ready for review July 3, 2026 16:42
@abashev

abashev commented Jul 4, 2026

Copy link
Copy Markdown

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants