Skip to content
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

Allow calling extension methods on this as receiver #3338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Rawi01 marked this conversation as resolved.
Show resolved Hide resolved
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) {
}
}
}