forked from phoedos/pmd
[java] NullPointerException in rule ProperCloneImplementation
This commit is contained in:
@@ -25,17 +25,17 @@ public class ProperCloneImplementationRule extends AbstractJavaRule {
|
||||
if (!"clone".equals(node.getName()) || node.getArity() > 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
ASTBlock block = node.getFirstChildOfType(ASTBlock.class);
|
||||
if (block == null) {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
String enclosingClassName = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class).getSimpleName();
|
||||
if (blockHasAllocations(block, enclosingClassName)) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -43,10 +43,14 @@ public class ProperCloneImplementationRule extends AbstractJavaRule {
|
||||
List<ASTAllocationExpression> allocations = block.findDescendantsOfType(ASTAllocationExpression.class);
|
||||
for (ASTAllocationExpression alloc : allocations) {
|
||||
ASTClassOrInterfaceType type = alloc.getFirstChildOfType(ASTClassOrInterfaceType.class);
|
||||
if (type.hasImageEqualTo(enclosingClassName)) {
|
||||
if (typeHasImage(type, enclosingClassName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean typeHasImage(ASTClassOrInterfaceType type, String image) {
|
||||
return type != null && type.hasImageEqualTo(image);
|
||||
}
|
||||
}
|
||||
|
@@ -50,6 +50,19 @@ public class Bar {
|
||||
Foo f = new Foo();
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>ok, should not throw NullPointerException while processing primitive array allocation</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
void clone() {
|
||||
super.clone();
|
||||
byte[] array = new byte[6];
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user