Skip to content

Conversation

@Laurens-W
Copy link
Contributor

@Laurens-W Laurens-W commented Aug 14, 2025

What's changed?

Improve the parsing of records to match closer with the Java compiler output

What's your motivation?

We've seen annotations with different targets end up on different elements in the compiler, but this is not reflected in the LST

Any additional context

Remaining TODOs
- [ ] Expand the logic around mapping the members, as currently we only consider VariableTree when parsing a record
- [ ] Add a marker to the LST so we can preserve the original order of the annotations
- [ ] Adjust the JavaPrinter so that we become print idempotent again
Based on Knut's guidance I've simplified the approach to not require drastic changes to the printer

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@Laurens-W Laurens-W self-assigned this Aug 14, 2025
@Laurens-W Laurens-W added bug Something isn't working parser labels Aug 14, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 14, 2025
@Laurens-W Laurens-W marked this pull request as ready for review August 18, 2025 11:10
github-actions[bot]

This comment was marked as resolved.

Comment on lines 886 to 906
public J visitLiteral(LiteralTree node, Space fmt) {
cursor(endPos(node));
int endPos = endPos(node);
Object value = node.getValue();
String valueSource = source.substring(((JCLiteral) node).getStartPosition(), endPos(node));

if (endPos == Position.NOPOS) {
if (typeMapping.primitive(((JCLiteral) node).typetag) == JavaType.Primitive.String) {
int quote = source.substring(cursor).startsWith("\"\"\"") ? 3 : 1;
if (quote == 3) {
endPos = cursor + quote + source.indexOf("\"\"\"", cursor + quote) + quote;
} else {
endPos = cursor + quote + value.toString().length() + quote;
}
} else {
endPos = cursor + indexOf(source.substring(cursor),
ch -> Character.isWhitespace(ch) || ",;)]}+-*/%=!<>&|^?:.".indexOf(ch) != -1
);
}
}

cursor(endPos);
String valueSource = source.substring(((JCLiteral) node).getStartPosition(), endPos);
Copy link
Contributor Author

@Laurens-W Laurens-W Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this had to change at all is because the endpos table is computed in this step

com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> jcCompilationUnits = compiler.parseFiles((List<JavaFileObject>) (List<?>) inputFileObjects, true);

and the removal of unapplicable annotations is done here
annotate.unblockAnnotations(); // also flushes once unblocked
without updating that endpos table, so we can no longer reliable get that information

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Aug 20, 2025
@timtebeek
Copy link
Member

@Laurens-W should any of the improvements here now be replicated into the Java 25 parser since this wasn't merged yet?

@Laurens-W
Copy link
Contributor Author

@Laurens-W should any of the improvements here now be replicated into the Java 25 parser since this wasn't merged yet?

Yes, they probably should, I'll update the PR!

@timtebeek timtebeek added the java label Sep 3, 2025
@timtebeek timtebeek merged commit 87e9a68 into main Sep 3, 2025
2 checks passed
@timtebeek timtebeek deleted the record-annotation branch September 3, 2025 23:00
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Sep 3, 2025
@tubbynl
Copy link

tubbynl commented Sep 4, 2025

tnx for the hard work; i'll retest openrewrite/rewrite-spring#714 when this get's released :)

@tubbynl
Copy link

tubbynl commented Oct 6, 2025

re-tested with rewrite-maven-plugin:6.19.0 and rewrite-spring:6.15.0 and can confirm it now parses this correctly and does not yield errors :)

@Laurens-W
Copy link
Contributor Author

Thanks for reporting back @tubbynl!
If you find any other issues feel free to report them 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java parser

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants