-
Notifications
You must be signed in to change notification settings - Fork 96
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
base: main
Are you sure you want to change the base?
Conversation
That there isn't any method type in that case sounds like a bug in the Java parser to me. |
LombokValueToRecord
should rewrite method references too
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 |
hm... at the moment we cannot do a meaningful implementation that addresses method references. |
Ah no I'd thought from your message above that it does work for |
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. |
Let's see if the tests pass and if so we can merge this addition to the recipe. 🙏🏻 |
There was a problem hiding this 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
src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java
Outdated
Show resolved
Hide resolved
This now fails with
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 |
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. |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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);
}
}
"""
)
);
} |
Unfortunately,
J.MemberReference
does not have a methodType whenthe 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