Skip to content

Commit

Permalink
Fix inlining method with anonymous class and qualified this reference (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
jjohnstn authored Dec 3, 2024
1 parent 9c7fbe4 commit 49d5dd2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,11 @@ public void testExplicitThisMethodReceiver() throws Exception {
performReceiverTest();
}

@Test
public void testQualifiedAnonymousReceiver() throws Exception {
performReceiverTest();
}

@Test
public void testThisReceiver() throws Exception {
performReceiverTestInlineMethod();
Expand Down

0 comments on commit 49d5dd2

Please sign in to comment.