From 5b0188aa47b3fe402b132d8f39eb4f986d9a83a7 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Sat, 28 Nov 2026 12:19:48 +0300 Subject: [PATCH] Stop copying comment text through reflection in JavacTokens CommentSavingTokenizer looked up JavaTokenizer's reader field for every comment, a field javac has not had since JDK 15, where JavaTokenizer became a UnicodeReader. Each lookup threw a NoSuchFieldException, and the fallback then called getRawCharacters on the tokenizer itself, reflectively. The copy it made became an AccessibleReader and then a String, read only for its length. A comment now keeps its start and end, and getTokens takes them as they are; getText, which only toString calls, reads the lexer's buffer. A comment of another class would mean a bug in JavacTokens, and getTokens throws an IllegalStateException that says so rather than lose the comment. The native image's metadata loses the three entries that were there for the reflection. Ports google-java-format#1546. Fixes #286. --- .../native-image/reachability-metadata.json | 23 ----- .../palantir/javaformat/java/JavacTokens.java | 84 ++++++++----------- 2 files changed, 36 insertions(+), 71 deletions(-) diff --git a/open-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json b/open-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json index 5fde3bc33..30c8a88c4 100644 --- a/open-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json +++ b/open-java-format-native/src/main/resources/META-INF/native-image/reachability-metadata.json @@ -66,9 +66,6 @@ "type": "com.google.common.collect.SingletonImmutableList", "allDeclaredFields": true }, - { - "type": "com.palantir.javaformat.java.JavacTokens$CommentSavingTokenizer" - }, { "type": "com.palantir.javaformat.java.Replacement", "allDeclaredFields": true, @@ -140,26 +137,6 @@ } ] }, - { - "type": "com.sun.tools.javac.parser.JavaTokenizer", - "fields": [ - { - "name": "reader" - } - ] - }, - { - "type": "com.sun.tools.javac.parser.UnicodeReader", - "methods": [ - { - "name": "getRawCharacters", - "parameterTypes": [ - "int", - "int" - ] - } - ] - }, { "type": "com.sun.tools.javac.tree.EndPosTable" }, diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavacTokens.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavacTokens.java index 55071ce4b..3c2317cd9 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavacTokens.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavacTokens.java @@ -24,7 +24,6 @@ import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; import com.sun.tools.javac.parser.Tokens.Token; import com.sun.tools.javac.parser.Tokens.TokenKind; -import com.sun.tools.javac.parser.UnicodeReader; import com.sun.tools.javac.util.Context; import java.util.ArrayList; import java.util.List; @@ -84,15 +83,18 @@ public static List getTokens(String source, Context context, Set getTokens(String source, Context context, Set