import java.util.*; import org.jetbrains.annotations.*; class ThisAsVariable { void instanceOf() { if (this instanceof Iterable) { return; } if (this instanceof Collection) { System.out.println("Impossible"); } } static class FieldsLocality { int a = 10; int b = getFoo(); // static: cannot write to 'a', 'a' is not flushed int c = a > 5 ? 6 : 7; int d = getBar(); // may write to 'a' int e = a > 5 ? 6 : 7; native static int getFoo(); native int getBar(); } static class FieldsLeakThroughLambda { int x = 5; FieldsLeakThroughLambda() { if (x > 5) { System.out.println("Impossible"); } Runnable r = () -> System.out.println("Hello"); r.run(); if (x > 5) { System.out.println("Impossible"); } Runnable r2 = () -> changeX(); if (x > 5) { System.out.println("Impossible"); } r2.run(); if (x > 5) { System.out.println("Possible"); } } void changeX() { x = 6; } } static class LocalityPropagation { final int[] data = {5,6,7}; LocalityPropagation() { if(data[0] < 0) { System.out.println("Impossible"); } if(data.length > 3) { System.out.println("Impossible"); } FieldsLocality.getFoo(); // cannot update 'data' as it's not leaking new FieldsLocality().getBar(); // cannot update 'data' as it's not leaking if(data[0] < 0) { System.out.println("Impossible"); } doSmth(); // may update 'data', but cannot replace it with another array as it's final if(data[0] < 0) { System.out.println("Possible"); } if(data.length > 3) { System.out.println("Still impossible"); } } native void doSmth(); } static class PassNullable { @Nullable final String s; PassNullable(@Nullable String _s) { s = _s; if (s != null) { Runnable r = new Runnable() { public void run() { System.out.println(s.trim()); } }; r.run(); } else { Runnable r = new Runnable() { public void run() { System.out.println(s.trim()); } }; r.run(); } } } }