Add ignoredAnnotations ptoperty to unusedPrivateMethod rule
This commit is contained in:
@@ -1227,6 +1227,12 @@ public class Something {
|
||||
}
|
||||
```
|
||||
|
||||
**This rule has the following properties:**
|
||||
|
||||
|Name|Default Value|Description|
|
||||
|----|-------------|-----------|
|
||||
|ignoredAnnotations|[java.lang.Deprecated]|Fully qualified names of the annotation types that should be ignored by this rule|
|
||||
|
||||
**Use this rule by referencing it:**
|
||||
``` xml
|
||||
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod" />
|
||||
|
||||
@@ -10,17 +10,21 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.ast.AccessNode;
|
||||
import net.sourceforge.pmd.lang.java.ast.TypeNode;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.ClassScope;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
|
||||
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
||||
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
|
||||
import net.sourceforge.pmd.properties.StringMultiProperty;
|
||||
|
||||
/**
|
||||
* This rule detects private methods, that are not used and can therefore be
|
||||
@@ -28,6 +32,17 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
|
||||
*/
|
||||
public class UnusedPrivateMethodRule extends AbstractJavaRule {
|
||||
|
||||
private static final StringMultiProperty IGNORED_ANNOTATIONS_DESCRIPTOR
|
||||
= StringMultiProperty.named("ignoredAnnotations")
|
||||
.desc("Fully qualified names of the annotation types that should be ignored by this rule")
|
||||
.defaultValues("java.lang.Deprecated")
|
||||
.build();
|
||||
|
||||
|
||||
public UnusedPrivateMethodRule() {
|
||||
definePropertyDescriptor(IGNORED_ANNOTATIONS_DESCRIPTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visit each method declaration.
|
||||
*
|
||||
@@ -46,7 +61,7 @@ public class UnusedPrivateMethodRule extends AbstractJavaRule {
|
||||
.getMethodDeclarations();
|
||||
for (MethodNameDeclaration mnd : findUnique(methods)) {
|
||||
List<NameOccurrence> occs = methods.get(mnd);
|
||||
if (!privateAndNotExcluded(mnd)) {
|
||||
if (!privateAndNotExcluded(mnd) || hasNeglectAnnotation(mnd)) {
|
||||
continue;
|
||||
}
|
||||
if (occs.isEmpty()) {
|
||||
@@ -107,4 +122,26 @@ public class UnusedPrivateMethodRule extends AbstractJavaRule {
|
||||
&& !node.hasImageEqualTo("writeObject") && !node.hasImageEqualTo("readResolve")
|
||||
&& !node.hasImageEqualTo("writeReplace");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given node is annotated with annotation in the set.
|
||||
* The node should be method name declaration.
|
||||
*
|
||||
* @param node
|
||||
* the node to check
|
||||
* @return <code>true</code> if the annotation has been found
|
||||
*/
|
||||
protected boolean hasNeglectAnnotation(MethodNameDeclaration node) {
|
||||
Node parent = node.getNode().jjtGetParent();
|
||||
List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
|
||||
for (ASTAnnotation annotation : annotations) {
|
||||
TypeNode n = (TypeNode) annotation.jjtGetChild(0);
|
||||
for (String annotationName : getProperty(IGNORED_ANNOTATIONS_DESCRIPTOR)) {
|
||||
if (TypeHelper.isA(n, annotationName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1552,4 +1552,16 @@ public class Something {
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#907 Add IgnoredAnnotations property to UnusedPrivateMethod</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
@Deprecated
|
||||
private bar() {
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
||||
|
||||
Reference in New Issue
Block a user