Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle ImmutableOf as Select on MethodInvocation #557

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

amishra-u
Copy link
Contributor

@amishra-u amishra-u commented Sep 18, 2024

What's changed?

Fix the bug in the ImmutableOf recipe. The current implementation does not account for the scenario where ImmutableOf is the 'select' property of a MethodInvocation.

Stack trace

"java.lang.IndexOutOfBoundsException: Index: 1
  java.base/java.util.Collections$EmptyList.get(Collections.java:4807)
  org.openrewrite.java.migrate.guava.AbstractNoGuavaImmutableOf$1.isParentTypeDownCast(AbstractNoGuavaImmutableOf.java:149)
  org.openrewrite.java.migrate.guava.AbstractNoGuavaImmutableOf$1.visitMethodInvocation(AbstractNoGuavaImmutableOf.java:71)
  org.openrewrite.java.migrate.guava.AbstractNoGuavaImmutableOf$1.visitMethodInvocation(AbstractNoGuavaImmutableOf.java:68)
  org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3905)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:317)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1365)
  org.openrewrite.java.JavaVisitor.visitMethodInvocation(JavaVisitor.java:911)
  org.openrewrite.java.migrate.guava.AbstractNoGuavaImmutableOf$1.visitMethodInvocation(AbstractNoGuavaImmutableOf.java:115)
  org.openrewrite.java.migrate.guava.AbstractNoGuavaImmutableOf$1.visitMethodInvocation(AbstractNoGuavaImmutableOf.java:68)
  org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3905)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:317)
  ..."

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

Future Work

Ideally I would like to recipe handle the above case also. Will create another PR to handle such scenario.

@timtebeek timtebeek self-requested a review September 18, 2024 08:40
@timtebeek timtebeek added the bug Something isn't working label Sep 18, 2024
Copy link
Contributor

@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 the fix here! I like how the solution is clearer as well;

To handle this case separately you might want to look at cases where the parent method invocation .select matches the immutable method we're after, and work from there.

@timtebeek timtebeek merged commit 53afa8a into openrewrite:main Sep 18, 2024
2 checks passed
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.

2 participants