From 49d5dd23b793466ca3e5335eb0281a9ab1c831a7 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Tue, 3 Dec 2024 14:26:34 -0500 Subject: [PATCH] Fix inlining method with anonymous class and qualified this reference (#1799) * Fix inlining method with anonymous class and qualified this reference - fix SourceAnalyzer to denote a qualified this receiver if the qualifier is a type and also to not denote an anonymous class instantiation for implicit receiver - add new test to InlineMethodTests - fixes #1780 --- .../refactoring/code/SourceAnalyzer.java | 8 +++++- .../TestQualifiedAnonymousReceiver.java | 22 +++++++++++++++ .../TestQualifiedAnonymousReceiver.java | 27 +++++++++++++++++++ .../tests/refactoring/InlineMethodTests.java | 5 ++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_in/TestQualifiedAnonymousReceiver.java create mode 100644 org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_out/TestQualifiedAnonymousReceiver.java diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceAnalyzer.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceAnalyzer.java index 4c5d0103281..fdaea5e67d7 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceAnalyzer.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/SourceAnalyzer.java @@ -268,7 +268,7 @@ public boolean visit(ClassInstanceCreation node) { if (fTypeCounter == 0) { Expression receiver= node.getExpression(); if (receiver == null) { - if (node.resolveTypeBinding().isLocal()) + if (node.resolveTypeBinding().isLocal() && !node.resolveTypeBinding().isAnonymous()) fImplicitReceivers.add(node); } } @@ -327,6 +327,12 @@ public boolean visit(ThisExpression node) { if (fTypeCounter == 0) { fImplicitReceivers.add(node); } + if (node.getQualifier() instanceof Name qualifier) { + IBinding binding= qualifier.resolveBinding(); + if (binding instanceof ITypeBinding) { + fImplicitReceivers.add(node); + } + } return true; } private void addReferencesToName(SimpleName node) { diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_in/TestQualifiedAnonymousReceiver.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_in/TestQualifiedAnonymousReceiver.java new file mode 100644 index 00000000000..258c9d748f1 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_in/TestQualifiedAnonymousReceiver.java @@ -0,0 +1,22 @@ +package receiver_in; + +public class TestQualifiedAnonymousReceiver { + int x = 10; + + void superClassMethod() { + System.out.println(x); + } + + static void callingMethod(TestQualifiedAnonymousReceiver instance) { + /*]*/instance.methodToBeInlined();/*[*/ + } + + void methodToBeInlined() { + int k = 0; + new Object() { + void execute() { + TestQualifiedAnonymousReceiver.this.superClassMethod(); + } + }.execute(); + } +} diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_out/TestQualifiedAnonymousReceiver.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_out/TestQualifiedAnonymousReceiver.java new file mode 100644 index 00000000000..f2f64ae3108 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineMethodWorkspace/TestCases/receiver_out/TestQualifiedAnonymousReceiver.java @@ -0,0 +1,27 @@ +package receiver_out; + +public class TestQualifiedAnonymousReceiver { + int x = 10; + + void superClassMethod() { + System.out.println(x); + } + + static void callingMethod(TestQualifiedAnonymousReceiver instance) { + /*]*/int k = 0; + new Object() { + void execute() { + instance.superClassMethod(); + } + }.execute();/*[*/ + } + + void methodToBeInlined() { + int k = 0; + new Object() { + void execute() { + TestQualifiedAnonymousReceiver.this.superClassMethod(); + } + }.execute(); + } +} diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java index f8dcdede751..0b859a2a073 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineMethodTests.java @@ -955,6 +955,11 @@ public void testExplicitThisMethodReceiver() throws Exception { performReceiverTest(); } + @Test + public void testQualifiedAnonymousReceiver() throws Exception { + performReceiverTest(); + } + @Test public void testThisReceiver() throws Exception { performReceiverTestInlineMethod();