rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

ReplaceLambdaWithMethodReference breaks with local record definition

Open mbruggmann opened this issue 1 year ago • 0 comments

What version of OpenRewrite are you using?

I am using

  • OpenRewrite 8.34.4
  • rewrite-static-analysis 1.15.0

What is the smallest, simplest way to reproduce the problem?

I can reproduce the issue with this testcase:

  @Test
  void methodReferenceForLocalRecord() {
    rewriteRun(
        spec -> spec.recipe(new ReplaceLambdaWithMethodReference()),
        java(
            """
            package com.helloworld;

            import java.util.stream.Stream;

            public class Main {
              public Stream<String> get() {
                record R(String s) {}
                return Stream.of(new R("hello world"))
                    .map(r -> r.s());
              }
            }""",
            """
            package com.helloworld;

            import java.util.stream.Stream;

            public class Main {
              public Stream<String> get() {
                record R(String s) {}
                return Stream.of(new R("hello world"))
                    .map(R::s);
              }
            }"""));
  }

What did you expect to see?

For the method reference to point to the record name, like R::...

What did you see instead?

The method reference starts with a number, like 1R::.... Possibly because the record is defined inline in the method?

Are you interested in contributing a fix to OpenRewrite?

Sorry, I don't have any spare hack time at the moment.

mbruggmann avatar Sep 09 '24 15:09 mbruggmann