RFR: 8271623: Omit enclosing instance fields from inner classes that don't use it

Liam Miller-Cushon cushon at openjdk.java.net
Tue Aug 3 18:48:04 UTC 2021


On Tue, 3 Aug 2021 06:59:36 GMT, liach <github.com+7806504+liach at openjdk.org> wrote:

>> This change omits the synthetic `this$0` field from inner classes that do not access any enclosing instance state.
>
> I hope we have a test case that ensures the field is not erroneously removed when this nested class doesn't use the field, but another nested class within this nested class does. An example:
> 
> 
> public class Second {
> 
>   int t = 3;
> 
>   public static void main(String... a) {
>     var q = new Second();
>     var p = q.new Third().new Fourth();
>     q.t = 6;
>     p.print();
>   }
>   
>   public class Third {
>     public class Fourth {
>       public void print() {
>         System.out.println(Second.this.t);
>       }
>     }
>   }
> }

@liach thanks, I added another test case with nested inner classes.

The current implementation handles that correctly, because the nested classes are lowered first, so any uses of the enclosing instance's `this$` field will be recorded before it is finished being lowered.

-------------

PR: https://git.openjdk.java.net/jdk/pull/4966


More information about the compiler-dev mailing list