Skip to content

LombokValueToRecord should rewrite method references too #469

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

holgpar
Copy link
Contributor

@holgpar holgpar commented May 1, 2024

Unfortunately, J.MemberReference does not have a methodType when
the member reference is through an object.

When it is throught a class (to a static method) it works as expected...

What's changed?

What's your motivation?

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

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

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

@knutwannheden
Copy link
Contributor

knutwannheden commented May 1, 2024

That there isn't any method type in that case sounds like a bug in the Java parser to me.

@timtebeek timtebeek added the enhancement New feature or request label May 2, 2024
@timtebeek timtebeek changed the title try to rewrite method references LombokValueToRecord should rewrite method references too May 2, 2024
@timtebeek timtebeek added the bug Something isn't working label May 2, 2024
@timtebeek
Copy link
Member

Can we separate these cases into separate tests to get these changes partially through already? Then we can separately work on the type issues, with the failing test here marked as @ExpectedToFail.

@holgpar
Copy link
Contributor Author

holgpar commented May 2, 2024

hm... at the moment we cannot do a meaningful implementation that addresses method references.
So are you asking for a test case to reproduce the issue?

@timtebeek
Copy link
Member

Ah no I'd thought from your message above that it does work for ClassName::methodName, but not for someInstance::methodName, and as such that we can at the very least try to cover the first case already, and add a separate unit test for the second case annotated with @ExpectedToFail (or @Disabled). If I misunderstood then please ignore; we'll need that proper fix anyway as well.

@holgpar
Copy link
Contributor Author

holgpar commented May 2, 2024

I just mentioned the static case to give you guys a clue on what might be wrong... It does not help for this recipe. Sorry for causing confusion.

@timtebeek
Copy link
Member

Let's see if the tests pass and if so we can merge this addition to the recipe. 🙏🏻

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java
    • lines 27-28

@timtebeek
Copy link
Member

This now fails with

LombokValueToRecordTest > methodReferences() FAILED
    org.opentest4j.AssertionFailedError: [Unexpected result in "example/A.java":
diff --git a/example/A.java b/example/A.java
index 044b9dd..f63ce43 100644
--- a/example/A.java
+++ b/example/A.java
@@ -4,13 +4,23 @@ 

 public record A(
   String test) {
+
+}
+
+class B {
+
+
+  public static String classMethod() {
+      return "foo";
+  }
+
 }

 class Using {

    Supplier<String> usingMethodReference() {
       A a = new A("foo");
-      return a::test;
+      return a::getTest;
   }

 }
\ No newline at end of file

Copy link
Contributor

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 7 days.

@github-actions github-actions bot added the Stale label Jul 14, 2025
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek removed the Stale label Jul 16, 2025
@timtebeek
Copy link
Member

Had a brief look to see if I could replicate any type information issues, but these tests (now?) pass:

    @MinimumJava17
    @Test
    void recordMethodReference() {
        rewriteRun(
          java(
            """
              import java.util.List;
              import java.util.stream.Stream;
              
              record Person(String name, int age) {}
              
              class Test {
                  void test() {
                      List<Person> people = List.of(
                          new Person("Alice", 30),
                          new Person("Bob", 25),
                          new Person("Charlie", 35)
                      );
                      
                      // Method reference to record accessor methods
                      List<String> names = people.stream()
                          .map(Person::name)
                          .toList();
                          
                      List<Integer> ages = people.stream()
                          .map(Person::age)
                          .toList();
                          
                      // Method reference with instance
                      Person person = new Person("David", 40);
                      java.util.function.Supplier<String> nameSupplier = person::name;
                      java.util.function.Supplier<Integer> ageSupplier = person::age;
                      
                      // Constructor reference
                      java.util.function.BiFunction<String, Integer, Person> personConstructor = Person::new;
                      Person newPerson = personConstructor.apply("Eve", 28);
                  }
              }
              """
          )
        );
    }

    @MinimumJava17
    @Test
    void lombokValueClass() {
        rewriteRun(
          java(
            """
              import java.util.List;
              import java.util.stream.Stream;
              
              @lombok.Value
              class Person {
                  String name;
                  int age;
              }
              
              class Test {
                  void test() {
                      List<Person> people = List.of(
                          new Person("Alice", 30),
                          new Person("Bob", 25),
                          new Person("Charlie", 35)
                      );
              
                      // Method reference to accessor methods
                      List<String> names = people.stream()
                          .map(Person::getName)
                          .toList();
              
                      List<Integer> ages = people.stream()
                          .map(Person::getAge)
                          .toList();
              
                      // Method reference with instance
                      Person person = new Person("David", 40);
                      java.util.function.Supplier<String> nameSupplier = person::getName;
                      java.util.function.Supplier<Integer> ageSupplier = person::getAge;
              
                      // Constructor reference
                      java.util.function.BiFunction<String, Integer, Person> personConstructor = Person::new;
                      Person newPerson = personConstructor.apply("Eve", 28);
                  }
              }
              """
          )
        );
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

LombokValueToRecord results in broken if method references are used
3 participants