Commit 3007ba9
authored
Use recommanded finalize chain pattern when override finalize() method (#16212)
According to the java docs of
[Object.finalize()](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#finalize()),
when override `finalize()` method, the `super.finalize()` should be
placed in the `finally` block:
> Finalizer invocations are not automatically chained, unlike
constructors. If a subclass overrides finalize it must invoke the
superclass finalizer explicitly. To guard against exceptions prematurely
terminating the finalize chain, the subclass should use a try-finally
block to ensure super.finalize() is always invoked. For example,
```
@OverRide
protected void finalize() throws Throwable {
try {
... // cleanup subclass state
} finally {
super.finalize();
}
}
```
As we know, the [constructor
chaining](https://docs.oracle.com/javase/tutorial/java/IandI/super.html)
order is: First the superclass constructor, then the subclass
constructor.
The destruction order has not been explained well in java docs, but we
can take C++ docs as reference:
https://isocpp.org/wiki/faq/dtors#order-dtors-for-members
So when doing finalize(), the order should be reversed: First the
subclass, then the superclass.
This pattern has also been stated in the book [<Effective Java, 2nd
Edition>](https://www.oreilly.com/library/view/effective-java-2nd/9780137150021/)(Item
7):
> You should finalize the subclass in a try block and invoke the
superclass finalizer in the corresponding finally block.
By doing so, it should make the code less error prone, for example:
https://issues.apache.org/jira/browse/AXIS2-4163
Co-authored-by: lao <none>1 parent b918042 commit 3007ba9
8 files changed
Lines changed: 28 additions & 16 deletions
File tree
- buffer/src/main/java/io/netty/buffer
- common/src
- main/java/io/netty/util
- test/java/io/netty/util
- handler/src/main/java/io/netty/handler/ssl
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
345 | 345 | | |
346 | 346 | | |
347 | 347 | | |
348 | | - | |
349 | | - | |
350 | 348 | | |
| 349 | + | |
| 350 | + | |
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
640 | 640 | | |
641 | 641 | | |
642 | 642 | | |
643 | | - | |
644 | | - | |
645 | 643 | | |
646 | 644 | | |
| 645 | + | |
| 646 | + | |
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
484 | 484 | | |
485 | 485 | | |
486 | 486 | | |
487 | | - | |
488 | | - | |
489 | 487 | | |
490 | 488 | | |
491 | 489 | | |
492 | 490 | | |
493 | 491 | | |
494 | 492 | | |
495 | 493 | | |
| 494 | + | |
| 495 | + | |
496 | 496 | | |
497 | 497 | | |
498 | 498 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
322 | 322 | | |
323 | 323 | | |
324 | 324 | | |
325 | | - | |
326 | | - | |
327 | 325 | | |
328 | 326 | | |
329 | 327 | | |
330 | 328 | | |
331 | 329 | | |
| 330 | + | |
| 331 | + | |
332 | 332 | | |
333 | 333 | | |
334 | 334 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
188 | | - | |
189 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
190 | 193 | | |
191 | 194 | | |
192 | 195 | | |
| |||
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
65 | 68 | | |
66 | 69 | | |
Lines changed: 5 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
44 | 47 | | |
45 | 48 | | |
0 commit comments