@@ -91,7 +91,7 @@ public void run(StringBuilder sb) {
9191 }
9292
9393 @ Test
94- void shouldReturnEmptyReceiverTypeForUnqualifiedCall () {
94+ void shouldResolveReceiverTypeForUnqualifiedCallToEnclosingClass () {
9595 final var source = JavaFileObjects .forSourceString (
9696 "build.codemodel.jdk.example.Caller" , """
9797 package build.codemodel.jdk.example;
@@ -119,6 +119,46 @@ public void run() {
119119 final var invocation = (MethodInvocation ) stmt .expression ();
120120
121121 assertThat (invocation .methodName ()).isEqualTo ("bar" );
122- assertThat (invocation .receiverType ()).isEmpty ();
122+ assertThat (invocation .receiverType ()).isPresent ();
123+ assertThat (invocation .receiverType ().get ()).isInstanceOf (NamedTypeUsage .class );
124+ final var receiverTypeName = ((NamedTypeUsage ) invocation .receiverType ().get ()).typeName ();
125+ assertThat (receiverTypeName .name ().toString ()).isEqualTo ("Caller" );
126+ }
127+
128+ @ Test
129+ void shouldResolveReceiverTypeForUnqualifiedCallInInnerClass () {
130+ final var source = JavaFileObjects .forSourceString (
131+ "build.codemodel.jdk.example.Outer" , """
132+ package build.codemodel.jdk.example;
133+ public class Outer {
134+ public class Inner {
135+ public void helper() {}
136+ public void run() {
137+ helper();
138+ }
139+ }
140+ }
141+ """ );
142+
143+ final var codeModel = JdkInitializerTests .runInternal (
144+ new JdkInitializer (List .of (), List .of (), List .of (source )));
145+
146+ final var innerTypeName = codeModel .getNameProvider ()
147+ .getTypeName (Optional .empty (), "build.codemodel.jdk.example.Outer.Inner" );
148+ final var innerDescriptor = codeModel .getTypeDescriptor (innerTypeName ).orElseThrow ();
149+
150+ final var run = innerDescriptor .traits (MethodDescriptor .class )
151+ .filter (m -> m .methodName ().name ().toString ().equals ("run" ))
152+ .findFirst ().orElseThrow ();
153+
154+ final var body = run .getTrait (MethodBodyDescriptor .class ).orElseThrow ().body ();
155+ final var stmt = (ExpressionStatement ) body .statements ().findFirst ().orElseThrow ();
156+ final var invocation = (MethodInvocation ) stmt .expression ();
157+
158+ assertThat (invocation .methodName ()).isEqualTo ("helper" );
159+ assertThat (invocation .receiverType ()).isPresent ();
160+ assertThat (invocation .receiverType ().get ()).isInstanceOf (NamedTypeUsage .class );
161+ final var receiverTypeName = ((NamedTypeUsage ) invocation .receiverType ().get ()).typeName ();
162+ assertThat (receiverTypeName .name ().toString ()).isEqualTo ("Inner" );
123163 }
124164}
0 commit comments