Skip to content

[java] AvoidCallingFinalize detects some false positives #2578

@wuchiuwong

Description

@wuchiuwong

Affects PMD Version:
6.22.0

Rule:
AvoidCallingFinalize

Description:
AvoidCallingFinalize detects some false positives, some are not even method calls.
This rule is implemented in pmd-java-6.22.0-sources.jar!\net\sourceforge\pmd\lang\java\rule\errorprone\AvoidCallingFinalizeRule.java,
The following piece of code is used to detect the call of finalize():

private boolean checkForViolation(ScopedNode node) {
    MethodScope meth = node.getScope().getEnclosingScope(MethodScope.class);
	if (meth != null && "finalize".equals(meth.getName())) {
		return false;
	}
	if (meth != null && checked.contains(meth)) {
		return false;
	}
	if (meth != null) {
		checked.add(meth);
	}
	return true;
}

This code is to find calling of finalize() by name, which causes some false positives to be detected

Code Sample demonstrating the issue:
Example 1:

public boolean finalCommand() 
{
	return finalize;	// This line is marked as an error
}

Example 2:

public LispCommand(boolean finalCommand, SourceLayout layout)
{
	this.finalize = finalCommand;	// This line is marked as an error
	this.layout = layout;
}

Example 3:

_coloringMethod.finalize(raster);	// This line is marked as an error

Expected outcome:

false-positive

Running PMD through:
CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions