Escape this analysis does not consider anonymous class/local class

Remi Forax forax at univ-mlv.fr
Mon Oct 2 07:14:41 UTC 2023


Hello,
The compiler does not emit a warning when the access to "this" is buried inside an anonymous class

public class EscapeThis3 {
  public EscapeThis3() {
    new Object() {
      void hidden() {
        for (var field : EscapeThis3.this.getClass().getFields()) {   // calling getClass() is I believe Ok here
          try {
            System.out.println(field.get(EscapeThis3.this));    // but not Ok here !
          } catch (IllegalAccessException e) {
            throw new AssertionError(e);
          }
        }
      }
    }.hidden();
  }
}

or a local class

public class EscapeThis3Bis {
  public EscapeThis3Bis() {
    class Hidden {
      void hidden() {
        for (var field : EscapeThis3Bis.this.getClass().getFields()) {
          try {
            System.out.println(field.get(EscapeThis3Bis.this));
          } catch (IllegalAccessException e) {
            throw new AssertionError(e);
          }
        }
      }
    }
    new Hidden().hidden();
  }
}

regards,
Rémi


More information about the compiler-dev mailing list