Skip to content

Support JDK 27 end positions (JDK-8372948) - #1786

Open
slachiewicz wants to merge 2 commits into
palantir:developfrom
slachiewicz:jdk27-end-positions
Open

slachiewicz wants to merge 2 commits into
palantir:developfrom
slachiewicz:jdk27-end-positions

Conversation

@slachiewicz

Copy link
Copy Markdown

Before this PR

JDK-8372948 (JDK 27) removed EndPosTable and JCCompilationUnit.endPositions, so Trees.getEndPosition, StringWrapper and RemoveUnusedImports fail with NoSuchFieldError on JDK 27. The same change re-ordered the four-argument ParserFactory.newParser, so Formatter was passing parseModuleInfo=true on JDK 27 without noticing. Fixes #1785.

After this PR

==COMMIT_MSG==
Support JDK 27 end positions (JDK-8372948)

Trees now looks up the end-position API once through a method handle: JCTree.getEndPosition() on JDKs without EndPosTable, and getEndPosition(unit.endPositions) via a VarHandle on older ones. All three call sites go through it, and Trees.newParser passes the parser flags in the order each JDK generation expects. Same approach as google/google-java-format#1328.
==COMMIT_MSG==

Verified: ./gradlew :palantir-java-format:check → 1405 tests pass; the built CLI formats identically on JDK 26 and JDK 27.

Possible downsides?

The handle is resolved from class presence, not from the runtime version number, so an early-access build that has one half of JDK-8372948 but not the other would fail at class init instead of at the first formatted file. No such build exists in the released tags.

This change was created with AI assistance.

JDK-8372948 removed EndPosTable and JCCompilationUnit.endPositions and
re-ordered the four-argument ParserFactory.newParser. Trees now resolves
the end-position API once through a method handle and all three call
sites go through it; Trees.newParser passes the parser flags in the order
each JDK generation expects.
@palantirtech

Copy link
Copy Markdown
Member

Thanks for your interest in palantir/palantir-java-format, @slachiewicz! 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 Sep 15, 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 JDK 27 end positions (JDK-8372948)

Trees now looks up the end-position API once through a method handle: JCTree.getEndPosition() on JDKs without EndPosTable, and getEndPosition(unit.endPositions) via a VarHandle on older ones. All three call sites go through it, and Trees.newParser passes the parser flags in the order each JDK generation expects. Same approach as google/google-java-format#1328.

Check the box to generate changelog(s)

  • Generate changelog entry

@slachiewicz

This comment was marked as resolved.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

palantir-java-format fails on JDK 27 with NoSuchFieldError for JCCompilationUnit.endPositions

2 participants