Fixing some false positives Xavier pointed out

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4888 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-12-16 01:35:13 +00:00
parent b24c8fe053
commit 958c6bf91a
2 changed files with 74 additions and 61 deletions

View File

@ -65,6 +65,17 @@ ok, implements interface which extends Cloneable
<code><![CDATA[
public class FooX extends test.net.sourceforge.pmd.rules.typeresolution.rules.xml.MyInterface {
void clone() {}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
ok, extends superclass AND implements cloneable
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo extends Bar implements Cloneable{
void clone() {}
}
]]></code>
</test-code>

View File

@ -30,69 +30,71 @@ import java.util.List;
*/
public class CloneMethodMustImplementCloneable extends AbstractRule {
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
ASTImplementsList impl = (ASTImplementsList) node.getFirstChildOfType(ASTImplementsList.class);
if (impl != null && impl.jjtGetParent().equals(node)) {
for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) impl.jjtGetChild(ix);
if (type.getType() == null) {
if ("Cloneable".equals(type.getImage())) {
return data;
}
} else {
List implementors = Arrays.asList(type.getType().getInterfaces());
if (implementors.contains(Cloneable.class)) {
return data;
}
}
}
}
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0).getClass().equals(ASTExtendsList.class)) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) ((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0);
Class clazz = type.getType();
while (clazz != null && !clazz.equals(Object.class)) {
if (Arrays.asList(clazz.getInterfaces()).contains(Cloneable.class)) {
return data;
}
clazz = clazz.getSuperclass();
}
}
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
ASTImplementsList impl = (ASTImplementsList) node.getFirstChildOfType(ASTImplementsList.class);
if (impl != null && impl.jjtGetParent().equals(node)) {
for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) impl.jjtGetChild(ix);
if (type.getType() == null) {
if ("Cloneable".equals(type.getImage())) {
return data;
}
} else if (type.getType().equals(Cloneable.class)) {
return data;
} else {
List implementors = Arrays.asList(type.getType().getInterfaces());
if (implementors.contains(Cloneable.class)) {
return data;
}
}
}
}
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0).getClass().equals(ASTExtendsList.class)) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) ((SimpleNode) node.jjtGetChild(0)).jjtGetChild(0);
Class clazz = type.getType();
if (clazz != null && clazz.equals(Cloneable.class)) {
return data;
}
while (clazz != null && !Object.class.equals(clazz)) {
if (Arrays.asList(clazz.getInterfaces()).contains(Cloneable.class)) {
return data;
}
clazz = clazz.getSuperclass();
}
}
return super.visit(node, data);
}
return super.visit(node, data);
}
public Object visit(ASTMethodDeclaration node, Object data) {
public Object visit(ASTMethodDeclaration node, Object data) {
if (node.isFinal()) {
List blocks = node.findChildrenOfType(ASTBlock.class);
if (blocks.size() == 1) {
blocks = node.findChildrenOfType(ASTBlockStatement.class);
if (blocks.size() == 1) {
ASTBlockStatement block = (ASTBlockStatement) blocks.get(0);
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) block
.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (type != null && type.getType() != null && type.getNthParent(9).equals(node)
&& type.getType().equals(CloneNotSupportedException.class)) {
return data;
} else if (type != null && type.getType() == null
&& "CloneNotSupportedException".equals(type.getImage())) {
return data;
}
}
}
}
return super.visit(node, data);
}
if (node.isFinal()) {
List blocks = node.findChildrenOfType(ASTBlock.class);
if (blocks.size() == 1) {
blocks = node.findChildrenOfType(ASTBlockStatement.class);
if (blocks.size() == 1) {
ASTBlockStatement block = (ASTBlockStatement) blocks.get(0);
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) block.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (type != null && type.getType() != null && type.getNthParent(9).equals(node) && type.getType().equals(CloneNotSupportedException.class)) {
return data;
} else if (type != null && type.getType() == null && "CloneNotSupportedException".equals(type.getImage())) {
return data;
}
}
}
}
return super.visit(node, data);
}
public Object visit(ASTMethodDeclarator node, Object data) {
if (!"clone".equals(node.getImage())) {
return data;
}
int countParams = ((ASTFormalParameters) node.jjtGetChild(0)).jjtGetNumChildren();
if (countParams != 0) {
return data;
}
addViolation(data, node);
return data;
}
public Object visit(ASTMethodDeclarator node, Object data) {
if (!"clone".equals(node.getImage())) {
return data;
}
int countParams = ((ASTFormalParameters) node.jjtGetChild(0)).jjtGetNumChildren();
if (countParams != 0) {
return data;
}
addViolation(data, node);
return data;
}
}