Repository navigation
Stop copying comment text through reflection in JavacTokens - #287
Merged
abashev merged 1 commit intoNov 28, 2026
Merged
Conversation
abashev
force-pushed
the
abashev/stop-copying-comment-text-through-reflection-in
branch
from
November 28, 2026 09:45
380774a to
8f48925
Compare
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.
abashev
force-pushed
the
abashev/stop-copying-comment-text-through-reflection-in
branch
from
November 28, 2026 10:12
8f48925 to
5b0188a
Compare
abashev
deleted the
abashev/stop-copying-comment-text-through-reflection-in
branch
November 28, 2026 16:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #286, a port of google-java-format#1546. Based on
21.X.X; the one other open pull request, #271, touches neither file.CommentSavingTokenizer.processCommentlooked upJavaTokenizer'sreaderfield for every comment. javac has had no such field since JDK 15, whereJavaTokenizerbecame aUnicodeReader, so on every JDK we run on the lookup threw aNoSuchFieldExceptionbefore the fallback calledgetRawCharacters(int, int)on the tokenizer itself, reflectively.-Xlog:exceptions=infoon the command line counts 119 of them forjava/util/ArrayList.java, one per comment and one for the//EOFthe lexer is fed. The copy then went into anAccessibleReaderand aString, andgetTokensread nothing of it but its length.JavacTokens. A comment keeps its start, end and style, andgetTokenstakes the range as it is.getText, which onlytoStringcalls, reads the lexer's buffer.AccessibleReaderhas no use left and goes. Upstream still callssuper.processComment, which copies the characters into aBasicComment; our tokenizer never did, so it copies nothing now. In the javac of JDK 21 and of JDK 27 every comment the tokenizer makes comes fromprocessComment,///comments included. A comment of another class would mean a bug inJavacTokens, andgetTokensthrows anIllegalStateExceptionthat says so. Skipping such a comment instead would lose it: its text would become part of the whitespace before the next token, which the formatter drops.reachability-metadata.jsonloses the three entries that were there for the reflection:JavacTokens$CommentSavingTokenizer, the fieldJavaTokenizer.readerandUnicodeReader.getRawCharacters(int, int).Allocation and time
JavacTokens.getTokensonjava/util/ArrayList.java, 200 calls a round in one thread, on Temurin 21.0.12:Once the JIT has warmed up the time is the same; only the first round is faster. Formatting the whole file takes about 17 ms and allocates about 32 MB either way.
Output
The old and the new
allJarformattedjava.base/javaandjdk.compilerfrom thesrc.zipof each JDK in place: 1,795 files on JDK 21 and 1,969 on JDK 27, 17 of which have///comments.diff -rfinds no difference, and the command line printed nothing to stderr either time. The native binary, built with GraalVM 25, formats the JDK 21 files as the old jar does.Tests
./gradlew :open-java-format:checkpasses on JDK 21, 1,439 tests;:open-java-format:test -PjavaRuntime=27passes, 2,357 tests. No formatting changes, so no new examples.