Skip to content

Return an J.Identifier instead of a MemberReference when containing is null#5614

Merged
sambsnyd merged 1 commit into
mainfrom
prevent-empty-containing-memberreference
Jun 12, 2025
Merged

Return an J.Identifier instead of a MemberReference when containing is null#5614
sambsnyd merged 1 commit into
mainfrom
prevent-empty-containing-memberreference

Conversation

@Laurens-W

Copy link
Copy Markdown
Contributor

What's changed?

When containing is null we now return an identifier instead of a MemberReference where the containing element is null

What's your motivation?

After #5535 we were seeing some

org.openrewrite.internal.RecipeRunException: java.lang.NullPointerException: Cannot invoke "org.openrewrite.java.tree.JRightPadded.getElement()" because "this.containing" is null

because the LST would get changed here

T t = right.getElement();
if (t instanceof J) {
//noinspection unchecked
t = visitAndCast((J) right.getElement(), p);
}
setCursor(getCursor().getParent());
if (t == null) {
//noinspection ConstantConditions
return null;
}

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

@jkschneider

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 Jun 12, 2025
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 12, 2025
@Laurens-W Laurens-W added the bug Something isn't working label Jun 12, 2025
qualifier.getPrefix(),
Markers.EMPTY,
JRightPadded.build(qualifier == null ? null : qualifier.withPrefix(Space.EMPTY)),
JRightPadded.build(qualifier.withPrefix(Space.EMPTY)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We'd considered returning an empty identifier here but this already happens for example with this piece of JavaDoc:

/**
 * {@link #n} refers to Inner
 */

so there would be no trivial way to determine whether to place the # or not, both are valid JavaDoc

@Laurens-W Laurens-W changed the title Return an identifier instead of a memberreference when containing is null Return an J.Identifier instead of a MemberReference when containing is null Jun 12, 2025
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jun 12, 2025
@sambsnyd sambsnyd merged commit 6b02819 into main Jun 12, 2025
2 checks passed
@sambsnyd sambsnyd deleted the prevent-empty-containing-memberreference branch June 12, 2025 21:04
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants