rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Use caller reference in UseMethod

Open fmodesto opened this issue 8 months ago • 4 comments

What's changed?

Modified UsesMethod to track the actual invocation target type, rather than just the method's declaring type.

Example Case:

class A { public void foo() {} }
interface I { void foo(); }
class B extends A implements I {}

class Test {
  void test() {
    new B().foo();
  }
}

The declaring type of the method foo is A. But that misses matches on the interface I

What's your motivation?

  • https://github.com/openrewrite/rewrite/issues/5376

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

This has a downstream regression in rewrite-static-analysis in CompareEnumsWithEqualityOperator. UseMethod matches for the exact java.lang.Enum equals(java.lang.Object) without overrides. This change would make the actual enum to use equals: com.pkg.MyEnum equals(java.lang.Object).

Checklist

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

fmodesto avatar May 06 '25 20:05 fmodesto

Thanks for the help! Copying the failure from the logs here to make it easier to follow up:

JavadocTest > methodFoundInSuperclassBecauseSuperclassFieldsTakesPresenceOverInterfaceFields() FAILED
    org.opentest4j.AssertionFailedError: expected: <SomeParent2> but was: <Test>
        at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145)
        at app//org.openrewrite.java.tree.JavadocTest.lambda$methodFoundInSuperclassBecauseSuperclassFieldsTakesPresenceOverInterfaceFields$4(JavadocTest.java:1262)
        at app//org.openrewrite.java.tree.JavadocTest$$Lambda$1064/0x00000001005bb440.accept0(Unknown Source)
        at app//org.openrewrite.internal.ThrowingConsumer.accept(ThrowingConsumer.java:26)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:576)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:125)
        at app//org.openrewrite.java.tree.JavadocTest.methodFoundInSuperclassBecauseSuperclassFieldsTakesPresenceOverInterfaceFields(JavadocTest.java:1234)

https://github.com/openrewrite/rewrite/blob/b110d3a97aefe4109b316b909a3392f1af07f18e/rewrite-java-tck/src/main/java/org/openrewrite/java/tree/JavadocTest.java#L1232-L1266

timtebeek avatar May 06 '25 20:05 timtebeek

Great to see! Linking this discussion here as I imagine we'd need that visit(memberRef.getContaining(), compareTo.getContaining()); reintroduced here before a merge:

  • https://github.com/openrewrite/rewrite/pull/5394#discussion_r2077413969

timtebeek avatar May 07 '25 11:05 timtebeek

Causes failures in rewrite-static-analysis

fmodesto avatar May 07 '25 19:05 fmodesto

Not sure about this one, the issue is in rewrite-static-analysis CompareEnumsWithEqualityOperator UseMethod matches for the exact java.lang.Enum equals(java.lang.Object) without overrides, now it comes as the.actual.Enum equals(java.lang.Object)

fmodesto avatar May 07 '25 20:05 fmodesto

This PR is stale because it has been open for 90 days with no activity. Remove stale label or comment or this will be closed in two weeks. PRs may be reopened when there is renewed interest.

github-actions[bot] avatar Sep 15 '25 04:09 github-actions[bot]