2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 16:49:39 +02:00
|
|
|
``++Object.finalize()++`` is called by the Garbage Collector at some point after the object becomes unreferenced.
|
|
|
|
|
|
|
|
|
|
|
|
In general, overloading ``++Object.finalize()++`` is a bad idea because:
|
|
|
|
|
|
|
|
* The overload may not be called by the Garbage Collector.
|
|
|
|
* Users are not expected to call ``++Object.finalize()++`` and will get confused.
|
|
|
|
|
|
|
|
But beyond that it's a terrible idea to name a method "finalize" if it doesn't actually override ``++Object.finalize()++``.
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
public int finalize(int someParameter) { // Noncompliant
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,java]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
public int someBetterName(int someParameter) { // Compliant
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Rename this method to avoid any possible confusion with Object.finalize() or [remove the parameters|give it a "void" return type].
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== on 1 Aug 2013, 13:21:52 Freddy Mallet wrote:
|
|
|
|
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-264
|
|
|
|
|
|
|
|
=== on 1 Aug 2013, 23:28:32 Ann Campbell wrote:
|
|
|
|
The description says that the lower-level finalize won't be called, but that's not what my reading indicates...?
|
|
|
|
|
|
|
|
=== on 2 Aug 2013, 06:26:50 Dinesh Bolkensteyn wrote:
|
|
|
|
How many rules do we need on Object.finalize()?
|
|
|
|
|
|
|
|
|
|
|
|
We already have 3, and I think that having 5 is really too much to cover a feature that in practice is never used...
|
|
|
|
|
|
|
|
|
|
|
|
It probably would be better to see how those 2 new rules could be fit into the existing 3 rules use cases
|
|
|
|
|
|
|
|
=== on 2 Aug 2013, 07:38:10 Dinesh Bolkensteyn wrote:
|
|
|
|
Methods should not be named 'finalize'
|
|
|
|
|
|
|
|
=== on 2 Aug 2013, 08:25:35 Freddy Mallet wrote:
|
|
|
|
Just to make it clear, in such case we're really talking about overloading and not about overriding.
|
|
|
|
|
|
|
|
=== on 2 Feb 2015, 20:57:10 Sébastien Gioria wrote:
|
|
|
|
CERT Secure Coding MET12-J
|
|
|
|
|
|
|
|
=== on 3 Feb 2015, 20:39:26 Ann Campbell wrote:
|
|
|
|
I see this as a different issue [~sebastien.gioria].
|
|
|
|
|
|
|
|
=== on 16 Jun 2015, 11:47:56 Ann Campbell wrote:
|
|
|
|
CodePro: Finalize Method Definition
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|