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

Methods with generic arguments are not matched #5047

Open
triceo opened this issue Feb 15, 2025 · 3 comments
Open

Methods with generic arguments are not matched #5047

triceo opened this issue Feb 15, 2025 · 3 comments
Assignees
Labels
bug Something isn't working parser-java

Comments

@triceo
Copy link

triceo commented Feb 15, 2025

Discussed in #5038

In my Java code, I have an interface X<A> with the following method:

default <Y_ extends Y<Y_>> @NonNull Z<A, Y_> doSomething(@NonNull Y_ y);

I am trying to write a pattern that would match this method. I have this, which should work, but doesn't:

fully.qualified.X doSomething(fully.qualified.Y)

The pattern suddenly starts to match when I use the specific implementation of Y (let's call it YImpl) which I actually use in the test for the recipe:

fully.qualified.X doSomething(fully.qualified.YImpl)

To provide a more explicit example:

  • Consider a method like default <Number_ extends Number> @NonNull Set<Number_> doSomething(@NonNull Number_ number);. Let's assume this method is declared on an interface com.github.MyInterface.
  • Further consider we're migrating source code like this: myInterfaceImpl.doSomething(BigDecimal.ONE);
  • This pattern will not match the code: com.github.MyInterface doSomething(java.lang.Number)
  • This pattern will match: com.github.MyInterface doSomething(java.math.BigDecimal)

The expected result is that the first pattern will match too.

I'm on rewrite-recipe-bom 3.1.0 and OpenJDK 21 (Temurin).
It is possible that the @NonNull annotations have no effect and can be removed; I have not tried that.

@triceo triceo changed the title Matching a generic method doesn't work correctly Methods with generic arguments are not matched Feb 15, 2025
@timtebeek timtebeek added bug Something isn't working parser-java labels Feb 15, 2025
@timtebeek timtebeek moved this to Backlog in OpenRewrite Feb 15, 2025
@timtebeek
Copy link
Contributor

Thanks for logging the issue as well @triceo . @greg-at-moderne FYI: this might factor in with the work you're looking at for Refaster as well: if the type information incorrectly is pegged to the actual type in method invocations, then matching could fail there as well.

@greg-at-moderne greg-at-moderne self-assigned this Feb 15, 2025
@greg-at-moderne
Copy link
Contributor

Thanks for the detailed report. Assigning myself to have a look next week.

Let me know if you would like to work towards the fix.

@knutwannheden
Copy link
Contributor

I don't know the details here, but I think it was done this way on purpose so that the method invocation captures the actual types that get substituted for the type parameters. But I've also stumbled over this problem in the past in other contexts and I think it could be useful for the JavaType.Method object of a method invocation with type parameters to reference the JavaType.Method of the method declaration. That would then allow the MethodMatcher to work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-java
Projects
Status: Backlog
Development

No branches or pull requests

4 participants