From 41acf0bd49684e84a67f3b7e7a75e5d9a0a9fc06 Mon Sep 17 00:00:00 2001 From: Demian Banakh Date: Thu, 26 Jan 2023 22:19:49 +0100 Subject: [PATCH] Allow calling extension methods on `this` as receiver --- AUTHORS | 1 + .../javac/handlers/HandleExtensionMethod.java | 7 ++++++- .../after-delombok/ExtensionMethodThis.java | 14 ++++++++++++++ .../after-ecj/ExtensionMethodThis.java | 19 +++++++++++++++++++ .../resource/before/ExtensionMethodThis.java | 17 +++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/transform/resource/after-delombok/ExtensionMethodThis.java create mode 100644 test/transform/resource/after-ecj/ExtensionMethodThis.java create mode 100644 test/transform/resource/before/ExtensionMethodThis.java diff --git a/AUTHORS b/AUTHORS index 50f9e10eec..736e955531 100755 --- a/AUTHORS +++ b/AUTHORS @@ -13,6 +13,7 @@ Christoph Dreis DaveLaw Dave Brosius Dawid Rusin +Demian Banakh Denis Stepanov Emil Lundberg Enrique da Costa Cambio diff --git a/src/core/lombok/javac/handlers/HandleExtensionMethod.java b/src/core/lombok/javac/handlers/HandleExtensionMethod.java index af03d00085..58e9ce3412 100644 --- a/src/core/lombok/javac/handlers/HandleExtensionMethod.java +++ b/src/core/lombok/javac/handlers/HandleExtensionMethod.java @@ -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 resolution = new JavacResolution(methodCallNode.getContext()).resolveMethodMember(methodCallNode); JCTree resolvedMethodCall = resolution.get(methodCall); @@ -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; + } } } diff --git a/test/transform/resource/after-delombok/ExtensionMethodThis.java b/test/transform/resource/after-delombok/ExtensionMethodThis.java new file mode 100644 index 0000000000..be49af1aa4 --- /dev/null +++ b/test/transform/resource/after-delombok/ExtensionMethodThis.java @@ -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) { + } + } +} diff --git a/test/transform/resource/after-ecj/ExtensionMethodThis.java b/test/transform/resource/after-ecj/ExtensionMethodThis.java new file mode 100644 index 0000000000..93df61d454 --- /dev/null +++ b/test/transform/resource/after-ecj/ExtensionMethodThis.java @@ -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() { + } +} diff --git a/test/transform/resource/before/ExtensionMethodThis.java b/test/transform/resource/before/ExtensionMethodThis.java new file mode 100644 index 0000000000..7f66a08a9d --- /dev/null +++ b/test/transform/resource/before/ExtensionMethodThis.java @@ -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) { + } + } +}