#1440 NPE in AvoidCallingFinalize
This commit is contained in:
@ -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.ast.ASTPrimarySuffix;
|
||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
|
import net.sourceforge.pmd.lang.java.symboltable.MethodScope;
|
||||||
|
import net.sourceforge.pmd.lang.symboltable.ScopedNode;
|
||||||
|
|
||||||
public class AvoidCallingFinalizeRule extends AbstractJavaRule {
|
public class AvoidCallingFinalizeRule extends AbstractJavaRule {
|
||||||
|
|
||||||
@ -27,14 +28,9 @@ public class AvoidCallingFinalizeRule extends AbstractJavaRule {
|
|||||||
if (name.getImage() == null || !name.getImage().endsWith("finalize")) {
|
if (name.getImage() == null || !name.getImage().endsWith("finalize")) {
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
MethodScope meth = name.getScope().getEnclosingScope(MethodScope.class);
|
if (!checkForViolation(name)) {
|
||||||
if (meth.getName().equals("finalize")) {
|
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
if (checked.contains(meth)) {
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
checked.add(meth);
|
|
||||||
addViolation(ctx, name);
|
addViolation(ctx, name);
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
@ -48,15 +44,24 @@ public class AvoidCallingFinalizeRule extends AbstractJavaRule {
|
|||||||
if (firstSuffix == null || firstSuffix.getImage() == null || !firstSuffix.getImage().endsWith("finalize")) {
|
if (firstSuffix == null || firstSuffix.getImage() == null || !firstSuffix.getImage().endsWith("finalize")) {
|
||||||
return super.visit(pp, ctx);
|
return super.visit(pp, ctx);
|
||||||
}
|
}
|
||||||
MethodScope meth = pp.getScope().getEnclosingScope(MethodScope.class);
|
if (!checkForViolation(pp)) {
|
||||||
if (meth.getName().equals("finalize")) {
|
|
||||||
return super.visit(pp, ctx);
|
return super.visit(pp, ctx);
|
||||||
}
|
}
|
||||||
if (checked.contains(meth)) {
|
|
||||||
return super.visit(pp, ctx);
|
|
||||||
}
|
|
||||||
checked.add(meth);
|
|
||||||
addViolation(ctx, pp);
|
addViolation(ctx, pp);
|
||||||
return super.visit(pp, ctx);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,18 @@ 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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
**Bugfixes:**
|
**Bugfixes:**
|
||||||
|
|
||||||
|
* java-finalizers/AvoidCallingFinalize
|
||||||
|
* [#1440](https://sourceforge.net/p/pmd/bugs/1440/): NPE in AvoidCallingFinalize
|
||||||
* java-unusedcode/UnusedPrivateField
|
* java-unusedcode/UnusedPrivateField
|
||||||
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable hides member variable
|
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable hides member variable
|
||||||
* General
|
* General
|
||||||
|
Reference in New Issue
Block a user