Loosening requirements for super() invocation

Brian Goetz brian.goetz at oracle.com
Wed Nov 9 16:07:27 UTC 2022



>
> Yes I think we're saying the same thing - passing 'this' to any 
> "alien" code is a leak.
>
> I'm just pointing out that the damage doesn't actually occur until 
> if/when the 'this' is actually used to access state in a subclass. But 
> that's just a technical point and doesn't ultimately change the kinds 
> of expressions we have to watch out for.

This is similar to the story with heap pollution; the damage of putting 
an `Integer` into a `List<String>` doesn't happen until you try to take 
it out.  We warn at the points where pollution *might* happen.

> Declared outside the compilation unit of MyClass
>
> Agreed... that formulation is simpler. But of course it would need a 
> caveat for subclasses within the same compilation unit, e.g.:
>
> public class Example {
>
>     public static abstract class A {
>
>         public A() {
>             System.out.println(this.foo());
>         }
>
>         public abstract int foo();
>     }
>
>     public static class B extends A {
>
>         private final int x;
>
>         public B(int x) {
>             this.x = x;
>         }
>
>         public int foo() {
>             return this.x;
>         }
>     }
>
>     public static void main(String[] args) {
>         new B(123);     // prints "0"
>     }
> }

All these classes are in the same compilation unit, though it's possible 
you may not want to get fancy enough with the analysis to simulate 
virtual dispatch (though you could, since its all in the same file.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221109/110ab004/attachment.htm>


More information about the amber-dev mailing list