#1440 NPE in AvoidCallingFinalize

This commit is contained in:
Andreas Dangel
2015-11-13 19:05:24 +01:00
parent ba92715b19
commit 02c49ce881
3 changed files with 31 additions and 12 deletions

View File

@ -13,6 +13,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
public class AvoidCallingFinalizeRule extends AbstractJavaRule {
@ -27,14 +28,9 @@ public class AvoidCallingFinalizeRule extends AbstractJavaRule {
if (name.getImage() == null || !name.getImage().endsWith("finalize")) {
return ctx;
}
MethodScope meth = name.getScope().getEnclosingScope(MethodScope.class);
if (meth.getName().equals("finalize")) {
if (!checkForViolation(name)) {
return ctx;
}
if (checked.contains(meth)) {
return ctx;
}
checked.add(meth);
addViolation(ctx, name);
return ctx;
}
@ -48,15 +44,24 @@ public class AvoidCallingFinalizeRule extends AbstractJavaRule {
if (firstSuffix == null || firstSuffix.getImage() == null || !firstSuffix.getImage().endsWith("finalize")) {
return super.visit(pp, ctx);
}
MethodScope meth = pp.getScope().getEnclosingScope(MethodScope.class);
if (meth.getName().equals("finalize")) {
if (!checkForViolation(pp)) {
return super.visit(pp, ctx);
}
if (checked.contains(meth)) {
return super.visit(pp, ctx);
}
checked.add(meth);
addViolation(ctx, pp);
return super.visit(pp, ctx);
}
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;
}
}

View File

@ -82,4 +82,16 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>#1440 NPE in AvoidCallingFinalize</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class InputFinalize {
{
super.finalize();
}
}
]]></code>
</test-code>
</test-data>

View File

@ -19,6 +19,8 @@
**Bugfixes:**
* java-finalizers/AvoidCallingFinalize
* [#1440](https://sourceforge.net/p/pmd/bugs/1440/): NPE in AvoidCallingFinalize
* java-unusedcode/UnusedPrivateField
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable hides member variable
* General