Skip to content

Stop copying comment text through reflection in JavacTokens #286

Done

Description

@abashev

Upstream: google/google-java-format#1546
Kind: port | Priority: low

google-java-format#1546 (merged 2026-11-27, copybara export) removes a per-comment reflection call from the tokenizer wrapper. The code only needs each comment's start and end offsets, but it looked up the private reader field of JavaTokenizer (a lookup that fails on JDK 15 and later), then reflectively called getRawCharacters to build a string just to take its length. Upstream now records the offsets directly, builds the input buffer with getChars into one array, and deletes the unused reflection metadata.

Why it matters to us: our JavacTokens.CommentSavingTokenizer.processComment still contains exactly this pattern (getRawCharactersReflectively, with getDeclaredField("reader") and a fallback after the exception), and it runs for every comment in every formatted file. The change is a small performance and robustness gain, and it drops reflective access into javac internals. Caveat: our CommentWithTextAndPosition keeps an AccessibleReader with the comment text and our code may use that text elsewhere, so the upstream diff does not apply as is; check which of our callers read the text before removing it. No formatting output changes, so no examples.

Affected files in our repo:

  • open-java-format/src/main/java/com/palantir/javaformat/java/JavacTokens.java (CommentSavingTokenizer, CommentWithTextAndPosition, getTokens)

Suggested action: investigate. If nothing needs the comment text from the tokenizer, adopt the idea (offsets only, single buffer); otherwise limit the change to avoiding the failing getDeclaredField("reader") lookup. Related earlier ticket on performance: 2026-11-20-google-stringwrapper-performance.md.


Filed from an upstream-watch ticket after manual review.

Activity

  1. added 2 commits that reference this issue on Nov 28, 2026
    8f48925
    5b0188a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions