From 5794bf743053e10b9d4e8f0a5068b2cf70d2555c Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Wed, 16 Sep 2026 00:41:57 +0200 Subject: [PATCH 1/3] Support JDK 27 end positions (JDK-8372948) 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. --- .../palantir/javaformat/java/Formatter.java | 4 +- .../javaformat/java/RemoveUnusedImports.java | 2 +- .../javaformat/java/StringWrapper.java | 2 +- .../com/palantir/javaformat/java/Trees.java | 77 ++++++++++++++++++- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java index ef8dab408..b6b78bfda 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Formatter.java @@ -184,8 +184,8 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); - JavacParser parser = parserFactory.newParser( - sourceText, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); + JavacParser parser = + Trees.newParser(parserFactory, sourceText, /*keepDocComments=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java index a66b6b559..c5e3be2a1 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java @@ -232,7 +232,7 @@ private static RangeMap buildReplacements( continue; } // delete the import - int endPosition = importTree.getEndPosition(unit.endPositions); + int endPosition = Trees.getEndPosition(importTree, unit); endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition); String sep = Newlines.guessLineSeparator(contents); if (endPosition + sep.length() < contents.length() diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java index 958fa0436..36ff5b04d 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/StringWrapper.java @@ -576,7 +576,7 @@ private static boolean noComments(String input, JCTree.JCCompilationUnit unit, T CharMatcher.whitespace().or(CharMatcher.anyOf("\"+")); private static int getEndPosition(JCTree.JCCompilationUnit unit, Tree tree) { - return ((JCTree) tree).getEndPosition(unit.endPositions); + return Trees.getEndPosition(tree, unit); } private static int getStartPosition(Tree tree) { diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Trees.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Trees.java index ead97b908..501561fc6 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/Trees.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/Trees.java @@ -23,11 +23,19 @@ import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; +import com.sun.tools.javac.parser.JavacParser; +import com.sun.tools.javac.parser.ParserFactory; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.Pretty; import com.sun.tools.javac.tree.TreeInfo; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.invoke.VarHandle; +import javax.annotation.Nullable; import javax.lang.model.element.Name; /** Utilities for working with {@link Tree}s. */ @@ -44,8 +52,73 @@ static int getStartPosition(Tree expression) { /** Returns the source end position of the node. */ static int getEndPosition(Tree expression, TreePath path) { - return ((JCTree) expression) - .getEndPosition(((JCTree.JCCompilationUnit) path.getCompilationUnit()).endPositions); + return getEndPosition(expression, (JCCompilationUnit) path.getCompilationUnit()); + } + + /** Returns the source end position of the node. */ + static int getEndPosition(Tree tree, JCCompilationUnit unit) { + try { + return (int) GET_END_POSITION.invokeExact((JCTree) tree, unit); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new IllegalStateException(e); + } + } + + /** + * Creates a parser that records end positions. JDK-8372948 (JDK 27) dropped the {@code keepEndPos} argument + * from {@link ParserFactory#newParser}, so the remaining four-argument overload now reads + * {@code (input, keepDocComments, keepLineMap, parseModuleInfo)}. + */ + static JavacParser newParser( + ParserFactory parserFactory, CharSequence input, boolean keepDocComments, boolean keepLineMap) { + if (END_POS_TABLE_CLASS != null) { + return parserFactory.newParser(input, keepDocComments, /* keepEndPos= */ true, keepLineMap); + } + // The last argument is parseModuleInfo on these JDKs. + return parserFactory.newParser(input, keepDocComments, keepLineMap, false); + } + + /** + * {@code com.sun.tools.javac.tree.EndPosTable}, or null on JDKs that store end positions directly in the tree + * (JDK-8372948, JDK 27 and later). + */ + @Nullable + private static final Class END_POS_TABLE_CLASS = endPosTableClass(); + + /** {@code (JCTree, JCCompilationUnit) -> int}, bound to whichever end position API this JDK has. */ + private static final MethodHandle GET_END_POSITION = getEndPositionHandle(); + + @Nullable + private static Class endPosTableClass() { + try { + return Class.forName("com.sun.tools.javac.tree.EndPosTable"); + } catch (ClassNotFoundException e) { + return null; + } + } + + private static MethodHandle getEndPositionHandle() { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + try { + if (END_POS_TABLE_CLASS == null) { + // (tree, unit) -> tree.getEndPosition() + return MethodHandles.dropArguments( + lookup.findVirtual(JCTree.class, "getEndPosition", MethodType.methodType(int.class)), + 1, + JCCompilationUnit.class); + } + // (tree, unit) -> tree.getEndPosition(unit.endPositions) + return MethodHandles.filterArguments( + lookup.findVirtual( + JCTree.class, "getEndPosition", MethodType.methodType(int.class, END_POS_TABLE_CLASS)), + 1, + lookup.findVarHandle(JCCompilationUnit.class, "endPositions", END_POS_TABLE_CLASS) + .toMethodHandle(VarHandle.AccessMode.GET)); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException("Unsupported javac end position API", e); + } } /** Returns the source text for the node. */ From 9e0ac600f02813f9c872e75666c306ac189242af Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Mon, 12 Oct 2026 12:44:18 +0000 Subject: [PATCH 2/3] Format var declarations on JDK 27 (JDK-8268850) JDK 27 gives var declarations a VarTypeTree type instead of null, which the visitors scanned to nothing ("expected token: 'var'"). Check for var before the declared type, as google-java-format#1342 does; the binding-pattern check compares the kind by name because Tree.Kind.VAR_TYPE is new in JDK 27. Fixes #1799 --- .../com/palantir/javaformat/java/JavaInputAstVisitor.java | 6 +++--- .../javaformat/java/java14/Java14InputAstVisitor.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 01d25c8df..b8cd8b0b3 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -3551,15 +3551,15 @@ int declareOne( { builder.open(ZERO); { - if (typeWithDims.isPresent() && typeWithDims.get().node != null) { + if (isVar) { + token("var"); + } else if (typeWithDims.isPresent() && typeWithDims.get().node != null) { scan(typeWithDims.get().node, null); int totalDims = dims.size(); builder.open(plusFour); maybeAddDims(dims); builder.close(); baseDims = totalDims - dims.size(); - } else if (isVar) { - token("var"); } else { scan(type, null); } diff --git a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java index 519fe966b..857710343 100644 --- a/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java +++ b/palantir-java-format/src/main/java/com/palantir/javaformat/java/java14/Java14InputAstVisitor.java @@ -118,7 +118,7 @@ private void visitBindingPattern(ModifiersTree modifiers, Tree type, Name name) if (modifiers != null) { builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty())); } - if (type == null) { + if (type == null || type.getKind().name().equals("VAR_TYPE")) { token("var"); } else { scan(type, null); From ba755dec2cdb319304aa6af7ec10692e18f39297 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Mon, 19 Oct 2026 23:39:25 +0000 Subject: [PATCH 3/3] Register the end-position API for the native image Trees reaches EndPosTable, JCTree.getEndPosition(EndPosTable) and JCCompilationUnit.endPositions through method and var handles, which native-image cannot resolve without metadata; google-java-format needed the same three entries (google/google-java-format@ff157e3e46). --- .../native-image/reachability-metadata.json | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/palantir-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json b/palantir-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json index c40f18794..80111428a 100644 --- a/palantir-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json +++ b/palantir-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json @@ -151,6 +151,28 @@ } ] }, + { + "type": "com.sun.tools.javac.tree.EndPosTable" + }, + { + "type": "com.sun.tools.javac.tree.JCTree", + "methods": [ + { + "name": "getEndPosition", + "parameterTypes": [ + "com.sun.tools.javac.tree.EndPosTable" + ] + } + ] + }, + { + "type": "com.sun.tools.javac.tree.JCTree$JCCompilationUnit", + "fields": [ + { + "name": "endPositions" + } + ] + }, { "type": "java.io.Serializable" },