Skip to content

Issue3365#4956

Merged
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:issue3365
Jan 20, 2026
Merged

Issue3365#4956
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:issue3365

Conversation

@jlerbsc

@jlerbsc jlerbsc commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #3365 .

Problem

When replacing a type name in nested generics like Set<Pair<String, String>>, the LexicalPreservingPrinter fails to apply the change. The original type name remains in the output instead of the new one.

Root Cause

The printer updates the wrong part of the code tree. It regenerates the immediate parent of the changed type, but the formatting information is actually stored at a higher level (the entire variable declaration). This mismatch causes the change to be ignored.

Solution

Before regenerating code, the system now identifies which part of the tree actually owns the formatting information for the changed element. If this owner differs from the immediate parent, it regenerates the owner instead. This ensures changes are properly reflected in the output.

Detail

Implements TokenOwnerDetector to correctly identify which AST node owns
tokens during replacements. Previously, LPP assigned tokens by position,
causing nested types like Set<Pair<String, String>> to fail updates.

Changes:

  • Add TokenOwnerDetector with TypeOwnerStrategy for type contexts
  • Add Optionals utility for Java 8 Optional.or() emulation
  • Integrate detection into LexicalPreservingPrinter.Observer
  • Handle variables, parameters, methods, classes, expressions
  • Workaround for multiple variable declarations (int a, b)
  • Add 48 unit tests covering all contexts and edge cases

The fix uses a non-invasive approach: only affects node replacements,
preserves existing behavior for 80% of cases, enables future root cause
fix by serving as specification for correct token assignment.

@codecov

codecov Bot commented Jan 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.59574% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.637%. Comparing base (a7fe71a) to head (babcc9c).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...printer/lexicalpreservation/TypeOwnerStrategy.java 75.247% 12 Missing and 13 partials ⚠️
...rinter/lexicalpreservation/TokenOwnerDetector.java 79.166% 3 Missing and 2 partials ⚠️
...in/java/com/github/javaparser/utils/Optionals.java 71.428% 2 Missing ⚠️
.../lexicalpreservation/LexicalPreservingPrinter.java 88.888% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #4956       +/-   ##
===============================================
+ Coverage     58.574%   58.637%   +0.063%     
  Complexity      2564      2564               
===============================================
  Files            696       699        +3     
  Lines          39980     40121      +141     
  Branches        7274      7314       +40     
===============================================
+ Hits           23418     23526      +108     
- Misses         13611     13628       +17     
- Partials        2951      2967       +16     
Flag Coverage Δ
AlsoSlowTests 58.637% <76.595%> (+0.063%) ⬆️
javaparser-core 58.637% <76.595%> (+0.063%) ⬆️
javaparser-symbol-solver 58.637% <76.595%> (+0.063%) ⬆️
jdk-10 58.209% <76.595%> (+0.064%) ⬆️
jdk-11 58.208% <76.595%> (+0.064%) ⬆️
jdk-12 58.205% <76.595%> (+0.062%) ⬆️
jdk-13 58.208% <76.595%> (+0.067%) ⬆️
jdk-14 58.440% <76.595%> (+0.064%) ⬆️
jdk-15 58.440% <76.595%> (+0.064%) ⬆️
jdk-16 58.412% <76.595%> (+0.064%) ⬆️
jdk-17 58.564% <76.595%> (+0.063%) ⬆️
jdk-18 58.564% <76.595%> (+0.063%) ⬆️
jdk-8 58.044% <76.595%> (+0.067%) ⬆️
jdk-9 58.206% <76.595%> (+0.064%) ⬆️
macos-latest 58.630% <76.595%> (+0.063%) ⬆️
ubuntu-latest 58.625% <76.595%> (+0.063%) ⬆️
windows-latest 58.620% <76.595%> (+0.063%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../lexicalpreservation/LexicalPreservingPrinter.java 74.788% <88.888%> (+0.274%) ⬆️
...in/java/com/github/javaparser/utils/Optionals.java 71.428% <71.428%> (ø)
...rinter/lexicalpreservation/TokenOwnerDetector.java 79.166% <79.166%> (ø)
...printer/lexicalpreservation/TypeOwnerStrategy.java 75.247% <75.247%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 05e4ee0...babcc9c. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlerbsc jlerbsc merged commit d2ed341 into javaparser:master Jan 20, 2026
34 of 35 checks passed
@jlerbsc jlerbsc added this to the next release milestone Jan 20, 2026
@jlerbsc jlerbsc added PR: Changed A PR that changes implementation without changing behaviour (e.g. performance) PR: Fixed A PR that offers a fix or correction and removed PR: Changed A PR that changes implementation without changing behaviour (e.g. performance) labels Jan 20, 2026
@jlerbsc jlerbsc deleted the issue3365 branch January 20, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Fixed A PR that offers a fix or correction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LexicalPreservingPrinter does not update parameters to collections.

1 participant