Skip to content

Commit

Permalink
Merge pull request #3410 from Rawi01/extensionmethod-in-lambda
Browse files Browse the repository at this point in the history
Delay replacement of extension methods in arguments
  • Loading branch information
rzwitserloot authored Aug 3, 2023
2 parents 4d6e8f3 + 4edd8a4 commit 32e5fb5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/lombok/javac/handlers/HandleExtensionMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ public void replace() {

@Override
public Void visitMethodInvocation(final MethodInvocationTree tree, final Void p) {
super.visitMethodInvocation(tree, p);
scan(tree.getTypeArguments(), p);
scan(tree.getMethodSelect(), p);
handleMethodCall((JCMethodInvocation) tree);
scan(tree.getArguments(), p);
return null;
}

Expand Down Expand Up @@ -181,6 +183,7 @@ private void handleMethodCall(final JCMethodInvocation methodCall) {
JCTree resolvedReceiver = resolution.get(receiver);
if (resolvedReceiver == null || resolvedReceiver.type == null) return;
Type receiverType = resolvedReceiver.type;
if (receiverType.isErroneous()) return;

// Skip static method access
Symbol sym = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// version 8:
import java.util.function.Function;

public class ExtensionMethodInLambda {
public void testSimple() {
String test = "test";
test = ExtensionMethodInLambda.Extensions.map(test, s -> ExtensionMethodInLambda.Extensions.reverse(s));
}

public void testSameName() {
String test = "test";
test = ExtensionMethodInLambda.Extensions.map(test, s -> s.trim());
}

public void testArgumentOfInvalidMethod() {
String test = "test";
test.invalid(s -> s.reverse());
}


static class Extensions {
public static <T, R> R map(T value, Function<T, R> mapper) {
return mapper.apply(value);
}

public static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}

public static String trim(Integer integer) {
return "0";
}
}
}
33 changes: 33 additions & 0 deletions test/transform/resource/after-ecj/ExtensionMethodInLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.function.Function;
import lombok.experimental.ExtensionMethod;
public @ExtensionMethod(value = ExtensionMethodInLambda.Extensions.class) class ExtensionMethodInLambda {
static class Extensions {
Extensions() {
super();
}
public static <T, R>R map(T value, Function<T, R> mapper) {
return mapper.apply(value);
}
public static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}
public static String trim(Integer integer) {
return "0";
}
}
public ExtensionMethodInLambda() {
super();
}
public void testSimple() {
String test = "test";
test = ExtensionMethodInLambda.Extensions.map(test, (<no type> s) -> ExtensionMethodInLambda.Extensions.reverse(s));
}
public void testSameName() {
String test = "test";
test = ExtensionMethodInLambda.Extensions.map(test, (<no type> s) -> s.trim());
}
public void testArgumentOfInvalidMethod() {
String test = "test";
test.invalid((<no type> s) -> s.reverse());
}
}
35 changes: 35 additions & 0 deletions test/transform/resource/before/ExtensionMethodInLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// version 8:
import java.util.function.Function;
import lombok.experimental.ExtensionMethod;

@ExtensionMethod(value = ExtensionMethodInLambda.Extensions.class)
public class ExtensionMethodInLambda {
public void testSimple() {
String test = "test";
test = test.map(s -> s.reverse());
}

public void testSameName() {
String test = "test";
test = test.map(s -> s.trim());
}

public void testArgumentOfInvalidMethod() {
String test = "test";
test.invalid(s -> s.reverse());
}

static class Extensions {
public static <T, R> R map(T value, Function<T, R> mapper) {
return mapper.apply(value);
}

public static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}

public static String trim(Integer integer) {
return "0";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19 The method invalid((<no type> s) -> {}) is undefined for the type String
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String

0 comments on commit 32e5fb5

Please sign in to comment.