<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<br>
<blockquote type="cite" cite="mid:CANSoFxsSTpWYhsZkcjE9s7gofgo-ADCAVv14ErrVE8jT2WkY0w@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">The goal is to close off all routes by
which 'this' could end up being passed to code in any
subclass. </div>
</div>
</blockquote>
<br>
any other class outside this compilation unit: subclass, superclass,
random class. This is sometimes called "alien" code -- code this
class does not control. (Note that inner classes in the current
class are OK; it's under this classes control.) <br>
<br>
<blockquote type="cite" cite="mid:CANSoFxsSTpWYhsZkcjE9s7gofgo-ADCAVv14ErrVE8jT2WkY0w@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">Attempt #1...
<div><br>
</div>
<div>A 'this' escape is when, in a non-final class <span style="font-family:monospace">MyClass</span> constructor,
after a <span style="font-family:monospace">super()</span>
call, a reference to the 'this' instance is used, explicitly
or implicitly, in any expression that (as far as the
compiler can tell) might possibly:<br>
</div>
<div>
<ol>
<li>Invoke a non-static method declared in any strict
supertype of <span style="font-family:monospace">MyClass</span></li>
</ol>
</div>
</div>
</div>
</blockquote>
<br>
Declared outside the compilation unit of MyClass<br>
<br>
<br>
<blockquote type="cite" cite="mid:CANSoFxsSTpWYhsZkcjE9s7gofgo-ADCAVv14ErrVE8jT2WkY0w@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">
<div>
<ol>
<li>Otherwise access a field <span style="font-family:monospace"><span style="font-family:arial,sans-serif">or i<span style="font-family:monospace"><span style="font-family:arial,sans-serif">nvoke a
non-static method </span></span>declared in</span></span>
any subclass of <span style="font-family:monospace">MyClass</span></li>
</ol>
</div>
</div>
</div>
</blockquote>
<br>
Plus: <br>
<br>
5. Store `this` to any variable. <br>
<br>
<blockquote type="cite" cite="mid:CANSoFxsSTpWYhsZkcjE9s7gofgo-ADCAVv14ErrVE8jT2WkY0w@mail.gmail.com">
<div dir="ltr">
<div class="gmail_quote">Test case 2: the class below should not
generate a warning. It doesn't because <span style="font-family:monospace">resetBoard()</span> is private
and contains no 'this' escapes.<br>
<div><br>
<div style="margin-left:40px"><span style="font-family:monospace">public class TicTacToe {</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"><br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> private final char[][]
board = new char[3][3];<br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"><br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> public TicTacToe() {</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> this.<span style="font-family:monospace">resetBoard</span>();<br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> }<br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"><br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> private void
resetBoard() {</span></div>
<div style="margin-left:40px"><span style="font-family:monospace">
Stream.of(this.board).forEach(row -> Arrays.fill(row,
' '));<br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> }</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"><br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace"> // more stuff here...<br>
</span></div>
<div style="margin-left:40px"><span style="font-family:monospace">}<br>
</span></div>
</div>
<div><br>
</div>
<div>Test case 3: <span style="font-family:monospace">FilteredSet</span>
does not generate a warning. Instead, we are putting the
"blame" on <span style="font-family:monospace">HashSet</span>.<br>
</div>
<br>
</div>
</div>
</blockquote>
<br>
Here's a more advanced case that would require more sophisticated
data flow analysis, but might not be so bad: <br>
<br>
public class X { <br>
public X() { foo(this); }<br>
<br>
private static void foo(X x) { }<br>
}<br>
<br>
Here, you pass `this` to foo(), but it doesn't do anything bad with
it.<br>
<br>
</body>
</html>