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

UseLambdaForFunctionalInterface should exclude methods with type paramters #309

Closed
mbruggmann opened this issue Jul 3, 2024 · 3 comments · Fixed by #310
Closed

UseLambdaForFunctionalInterface should exclude methods with type paramters #309

mbruggmann opened this issue Jul 3, 2024 · 3 comments · Fixed by #310
Labels
bug Something isn't working

Comments

@mbruggmann
Copy link
Contributor

We are using OpenRewrite v8.28.1 and rewrite-static-analysis v1.10.1.

In our codebase, we have an interface with a single method using a type parameter. Something like this:

public interface I {
  <T> List<T> call();
}

Which is then implemented in other places using an anonymous class like so:

final I i = new I() {
  @Override
  public <T> List<T> call() {...}
};

The UseLambdaForFunctionalInterface recipe replaces such inline definitions of I with a lambda (final I i = () -> ...;). That will not compile because of the type argument being lost. I would expect the recipe to bail out and not introduce a lambda in this case.

I can reproduce it using this testcase:

  @Test
  void dontUseLambdaForMethodWithTypeParameter() {
    rewriteRun(
        spec -> spec.recipe(new UseLambdaForFunctionalInterface()),
        java(
            // can't transform to lambda because of the type argument of I#call()
            """
            package com.helloworld;

            import java.util.List;

            public interface I {
              <T> List<T> call();
            }

            class Hello {
              public void hello() {
                final I i = new I() {
                  @Override
                  public <T> List<T> call() {
                    return null;
                  }
                };
                final List<String> list = i.call();
              }
            }
            """));
  }
}
@mbruggmann mbruggmann added the bug Something isn't working label Jul 3, 2024
@timtebeek
Copy link
Contributor

Thanks for the runnable example @mbruggmann ; I've had a brief look locally with these minor changes to your test.

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/309")
void dontUseLambdaForMethodWithTypeParameter() {
    //language=java
    rewriteRun(
      spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(
        """
          package com.helloworld;

          import java.util.List;

          public interface I {
            <T> List<T> call();
          }
          """
      )),
      java(
        // can't transform to lambda because of the type argument of I#call()
        """
          package com.helloworld;

          import java.util.List;

          class Hello {
            public void hello() {
              final I i = new I() {
                @Override
                public <T> List<T> call() {
                  return null;
                }
              };
              final List<String> list = i.call();
            }
          }
          """
      )
    );
}

Did you already explore any potential fixes for this recipe?

timtebeek added a commit that referenced this issue Jul 3, 2024
@timtebeek
Copy link
Contributor

I've pushed up the above test to 045bd00 if you want to have a look.

Earlier we had handled another case differently by introducing casting; perhaps good to compare:

@mbruggmann
Copy link
Contributor Author

Thanks @timtebeek!
I haven't had a chance to look at possible fixes, but wanted to make sure we capture the issue at least.

mbruggmann added a commit to mbruggmann/rewrite-static-analysis that referenced this issue Jul 4, 2024
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 a pull request may close this issue.

2 participants