Skip to content

Commit

Permalink
Allow calling extension methods on this as receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
dembanakh committed Mar 26, 2023
1 parent b80ff39 commit 41acf0b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Christoph Dreis <[email protected]>
DaveLaw <[email protected]>
Dave Brosius <[email protected]>
Dawid Rusin <[email protected]>
Demian Banakh <[email protected]>
Denis Stepanov <[email protected]>
Emil Lundberg <[email protected]>
Enrique da Costa Cambio <[email protected]>
Expand Down
7 changes: 6 additions & 1 deletion src/core/lombok/javac/handlers/HandleExtensionMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ private void handleMethodCall(final JCMethodInvocation methodCall) {
JCExpression receiver = receiverOf(methodCall);
String methodName = methodNameOf(methodCall);

if ("this".equals(receiver.toString()) || "this".equals(methodName) || "super".equals(methodName)) return;
if ("this".equals(receiver.toString()) && isImplicitReceiver(methodCall)) return;
if ("this".equals(methodName) || "super".equals(methodName)) return;
Map<JCTree, JCTree> resolution = new JavacResolution(methodCallNode.getContext()).resolveMethodMember(methodCallNode);

JCTree resolvedMethodCall = resolution.get(methodCall);
Expand Down Expand Up @@ -224,5 +225,9 @@ private JCExpression receiverOf(final JCMethodInvocation methodCall) {
return ((JCFieldAccess) methodCall.meth).selected;
}
}

private boolean isImplicitReceiver(final JCMethodInvocation methodCall) {
return methodCall.meth instanceof JCIdent;
}
}
}
14 changes: 14 additions & 0 deletions test/transform/resource/after-delombok/ExtensionMethodThis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ExtensionMethodThis {
public void test() {
ExtensionMethodThis.Extensions.hello(this);
hello();
}

private void hello() {
}

static class Extensions {
public static void hello(ExtensionMethodThis self) {
}
}
}
19 changes: 19 additions & 0 deletions test/transform/resource/after-ecj/ExtensionMethodThis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import lombok.experimental.ExtensionMethod;
@ExtensionMethod(ExtensionMethodThis.Extensions.class) class ExtensionMethodThis {
static class Extensions {
Extensions() {
super();
}
public static void hello(ExtensionMethodThis self) {
}
}
ExtensionMethodThis() {
super();
}
public void test() {
ExtensionMethodThis.Extensions.hello(this);
hello();
}
private void hello() {
}
}
17 changes: 17 additions & 0 deletions test/transform/resource/before/ExtensionMethodThis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lombok.experimental.ExtensionMethod;

@ExtensionMethod(ExtensionMethodThis.Extensions.class)
class ExtensionMethodThis {
public void test() {
this.hello();
hello();
}

private void hello() {
}

static class Extensions {
public static void hello(ExtensionMethodThis self) {
}
}
}

0 comments on commit 41acf0b

Please sign in to comment.