Skip to content

fix: filter out methods not invoked from an identifier#595

Merged
timtebeek merged 5 commits into
openrewrite:mainfrom
pdelagrave:fix-572
Jun 17, 2025
Merged

fix: filter out methods not invoked from an identifier#595
timtebeek merged 5 commits into
openrewrite:mainfrom
pdelagrave:fix-572

Conversation

@pdelagrave
Copy link
Copy Markdown
Contributor

@pdelagrave pdelagrave commented Jun 16, 2025

What's changed?

UseCollectionInterfaces.visitMethodInvocation() will now only proceed if the method was invoked on an expression being an identifier. Anonymous objects don't have an identifier so it doesn't make sense to try to migrate their identifier from a concrete class to an interface.

What's your motivation?

UseCollectionInterfaces.visitMethodInvocation() was crashing trying to update the identifier type of an identifier-less anonymous object method invocations.

Anyone you would like to review specifically?

@JohannisK who worked on adding the override UseCollectionInterfaces.visitMethodInvocation() 2 months ago.

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

Yes, see next section

Have you considered any alternatives or workarounds?

The (only) change at line 156 is:

if (mi.getSelect() != null && mi.getSelect().getType() != null && mi.getSelect() instanceof J.Identifier) {

could have been:

if (mi.getSelect() != null && mi.getSelect().getType() != null && !(mi.getSelect() instanceof J.NewClass)) {

The added test case based from the bug report did crash specifically because of the visitMethodInvocation() was processing the invocation even if it was for an invocation on an anonymous object which didn't make sense in that context. So skipping it by checking if the expression is exactly that could make sense, but that leave a lot of other expression type that could also not make sense in that context.

Specifying that we only want to process expressions that are an identifier narrows down possibilities a lot and might avoid other bugs.

But I'd like a 2nd opinion as even if so far it looks OK and all the tests are passing, this might narrow down when visitMethodInvocation() runs too much?

Any additional context

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

`UseCollectionInterfaces.visitMethodInvocation()` was crashing trying to update the identifier type of an identifier-less anonymous object method invocations.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 16, 2025
@pdelagrave pdelagrave moved this from In Progress to Ready to Review in OpenRewrite Jun 16, 2025
@timtebeek timtebeek added the bug Something isn't working label Jun 16, 2025
@timtebeek
Copy link
Copy Markdown
Member

Thanks @pdelagrave ! I've made a small change to pull forward the instanceof to where we had a null check previously, as that's then implied by the instanceof. I've also added a new test case that we also didn't cover before, but would be affected by the approach you've taken here, so figured good to add into the mix here and see that fixed as well. Would you be ok to tag team on that?

Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks for working through this one! Good to see the issue resolved and a new case covered.

@timtebeek timtebeek merged commit 4ad2ea5 into openrewrite:main Jun 17, 2025
2 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 17, 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.

UseCollectionInterfaces might fail with UnsupportedOperationException

2 participants