Loosening requirements for super() invocation
Archie Cobbs
archie.cobbs at gmail.com
Wed Jan 4 20:27:13 UTC 2023
On Wed, Jan 4, 2023 at 1:23 PM Brian Goetz <brian.goetz at oracle.com> wrote:
>
> for things like the ArrayList constructors that call add, this is
> fundamentally dangerous and probably wants a note.
>
> Agreed... except ArrayList is a bad example :) ArrayList(Collection c)
actually does not invoke add() or addAll(). Instead it invokes c.toArray().
> Also, since the last post I found and fixed a few bugs, which revealed
> more 'this' escapes. Here are the new numbers, which have gone up a bit:
>
>
> Are there interesting examples of the new form that we didn’t detect
> before, that would be educational to share?
>
Not really - the increase was just due to a dumb bug in which unqualified
method invocations like "foo()" were not always being caught.
In general though, from what I can tell, most leaks are pretty much what
you might expect. Many of them fall into the bucket of "Yes this is a leak,
but it's unlikely to cause trouble in practice." But there are definitely
some "dangerous" ones too, such as HashSet, etc.
The most common pattern for leaks is invoking some non-static method in the
constructor. For example, lots of classes invoke some kind of init() method
or whatever. Basically the constructor needs to do some work, and for
whatever reason (some part of) that work has been abstracted out into a
separate method or lives in a superclass.
For example, lots of Swing widget constructors invoke methods like
setLayout(), setBackground(), setBorder(), etc.
Several exception classes invoke initCause() (and of course Throwable is
guilty of invoking fillInStackTrace(), which is not final).
Several classes invoke CleanerFactory.cleaner().register(this, ...).
In short this warning, like a lot of the other lint warnings, seems to fall
into the category that you're likely to get false positives, but that's
hopefully worth it in exchange for a new category of automated bug
detection.
What are the next steps?
-Archie
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230104/ef192a53/attachment-0001.htm>
More information about the amber-dev
mailing list