[java] NullPointerException in rule ProperCloneImplementation

This commit is contained in:
Mykhailo Palahuta
2020-07-14 13:02:34 +03:00
parent 1ca84d56c1
commit 782cb9d262
2 changed files with 21 additions and 4 deletions

View File

@@ -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);
}
}

View File

@@ -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>