[java] Replace XPath ProperCloneImplementation with Java Rule
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
|
||||
package net.sourceforge.pmd.lang.java.rule.errorprone;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTBlock;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
|
||||
public class ProperCloneImplementationRule extends AbstractJavaRule {
|
||||
|
||||
public ProperCloneImplementationRule() {
|
||||
addRuleChainVisit(ASTMethodDeclaration.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTMethodDeclaration node, Object data) {
|
||||
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;
|
||||
}
|
||||
|
||||
private boolean blockHasAllocations(ASTBlock block, String enclosingClassName) {
|
||||
List<ASTAllocationExpression> allocations = block.findDescendantsOfType(ASTAllocationExpression.class);
|
||||
for (ASTAllocationExpression alloc : allocations) {
|
||||
ASTClassOrInterfaceType type = alloc.getFirstChildOfType(ASTClassOrInterfaceType.class);
|
||||
if (type.hasImageEqualTo(enclosingClassName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -2719,27 +2719,12 @@ public class Foo { // perfect, both methods provided
|
||||
language="java"
|
||||
since="1.4"
|
||||
message="Object clone() should be implemented with super.clone()"
|
||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
||||
class="net.sourceforge.pmd.lang.java.rule.errorprone.ProperCloneImplementationRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#propercloneimplementation">
|
||||
<description>
|
||||
Object clone() should be implemented with super.clone().
|
||||
</description>
|
||||
<priority>2</priority>
|
||||
<properties>
|
||||
<property name="version" value="2.0"/>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//MethodDeclaration
|
||||
[@Name = 'clone']
|
||||
[@Arity = 0]
|
||||
[Block
|
||||
[.//AllocationExpression[ClassOrInterfaceType[@Image = ancestor::ClassOrInterfaceDeclaration[1]/@SimpleName]]]
|
||||
]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
class Foo{
|
||||
|
Reference in New Issue
Block a user