Skip to content

Commit ed22a16

Browse files
Jami CogswellJami Cogswell
Jami Cogswell
authored and
Jami Cogswell
committed
Java: exclude overloads of finalize
1 parent f73eda0 commit ed22a16

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

java/ql/src/Violations of Best Practice/Undesirable Calls/DoNotCallFinalize.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Overview
22

3-
Calling `finalize` in application code may cause inconsistent program state or unpredicatable behavior.
3+
Calling `finalize()` in application code may cause inconsistent program state or unpredicatable behavior.
44

55
## Recommendation
66

7-
Avoid calling `finalize` in application code. Allow the JVM to determine a garbage collection schedule instead.
7+
Avoid calling `finalize()` in application code. Allow the JVM to determine a garbage collection schedule instead.
88

99
## Example
1010

@@ -19,7 +19,7 @@ public class Test {
1919

2020
# Implementation Notes
2121

22-
This rule is focused on the use of existing `finalize` invocations rather than attempts to write a custom implementation.
22+
This rule is focused on the use of existing `finalize()` invocations rather than attempts to write a custom implementation.
2323

2424
## References
2525

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @id java/do-not-call-finalize
33
* @previous-id java/do-not-use-finalizers
4-
* @name Do not call `finalize`
5-
* @description Calling `finalize` in application code may cause
4+
* @name Do not call `finalize()`
5+
* @description Calling `finalize()` in application code may cause
66
* inconsistent program state or unpredicatable behavior.
77
* @kind problem
88
* @precision high
@@ -16,13 +16,13 @@ import java
1616

1717
from MethodCall mc
1818
where
19-
mc.getMethod().hasName("finalize") and
20-
// The Java documentation for `finalize` states: "If a subclass overrides
19+
mc.getMethod() instanceof FinalizeMethod and
20+
// The Java documentation for `finalize()` states: "If a subclass overrides
2121
// `finalize` it must invoke the superclass finalizer explicitly". Therefore,
22-
// we do not alert on `super.finalize` calls that occur within a callable
22+
// we do not alert on `super.finalize()` calls that occur within a callable
2323
// that overrides `finalize`.
2424
not exists(Callable caller, FinalizeMethod fm | caller = mc.getCaller() |
2525
caller.(Method).overrides(fm) and
2626
mc.getQualifier() instanceof SuperAccess
2727
)
28-
select mc, "Call to 'finalize'."
28+
select mc, "Call to 'finalize()'."
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
category: newQuery
33
---
4-
* Added a new quality query, `java/do-not-call-finalize`, to detect calls to `finalize`.
4+
* Added a new quality query, `java/do-not-call-finalize`, to detect calls to `finalize()`.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
| Test.java:4:9:4:23 | finalize(...) | Call to 'finalize'. |
2-
| Test.java:25:9:25:33 | finalize(...) | Call to 'finalize'. |
1+
| Test.java:4:9:4:23 | finalize(...) | Call to 'finalize()'. |

java/ql/test/query-tests/DoNotCallFinalize/Test.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void f1() throws Throwable {
1010

1111
@Override
1212
protected void finalize() throws Throwable {
13-
// COMPLIANT: If a subclass overrides `finalize`
13+
// COMPLIANT: If a subclass overrides `finalize()`
1414
// it must invoke the superclass finalizer explicitly.
1515
super.finalize();
1616
}
@@ -20,9 +20,9 @@ protected void finalize(String s) throws Throwable {
2020
System.out.println(s);
2121
}
2222

23-
// NON_COMPLIANT: call to overload of `finalize`
23+
// COMPLIANT: call to overload of `finalize`
2424
void f2() throws Throwable {
25-
this.finalize("overload"); // $ Alert
25+
this.finalize("overload");
2626
}
2727

2828
}

0 commit comments

Comments
 (0)