#1430 CommentDefaultAccessModifier triggers on field annotated with @VisibleForTesting

This commit is contained in:
Andreas Dangel
2015-10-17 18:00:37 +02:00
parent 2cff91d187
commit 9df4ff15b5
3 changed files with 42 additions and 2 deletions

View File

@ -7,11 +7,14 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode;
import net.sourceforge.pmd.lang.java.ast.Comment;
@ -89,6 +92,29 @@ public class CommentDefaultAccessModifierRule extends AbstractCommentRule {
// check if the field/method/nested class has a default access modifier
&& decl.isPackagePrivate()
// if is a default access modifier check if there is a comment in this line
&& !interestingLineNumberComments.contains(decl.getBeginLine());
&& !interestingLineNumberComments.contains(decl.getBeginLine())
// that it is not annotated with @VisibleForTesting
&& hasNoVisibleForTestingAnnotation(decl);
}
private boolean hasNoVisibleForTestingAnnotation(AbstractJavaAccessNode decl) {
boolean result = true;
ASTClassOrInterfaceBodyDeclaration parent = decl.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
if (parent != null) {
List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
List<ASTName> names = annotation.findDescendantsOfType(ASTName.class);
for (ASTName name : names) {
if (name.hasImageEqualTo("VisibleForTesting")) {
result = false;
break;
}
}
if (result == false) {
break;
}
}
}
return result;
}
}

View File

@ -145,6 +145,16 @@ public enum TestEnum {
FOO;
public String getName() { return "foo"; }
}
]]></code>
</test-code>
<test-code>
<description>#1430 CommentDefaultAccessModifier triggers on field annotated with @VisibleForTesting</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class CommentDefaultAccessModifier {
@VisibleForTesting void method() {}
}
]]></code>
</test-code>

View File

@ -16,8 +16,12 @@
**Bugfixes:**
* java-comments/CommentDefaultAccessModifier
* [#1430](https://sourceforge.net/p/pmd/bugs/1430/): CommentDefaultAccessModifier triggers on field
annotated with @VisibleForTesting
* java-unusedcode/UnusedPrivateField
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable hides member variable
* [#1428](https://sourceforge.net/p/pmd/bugs/1428/): False positive in UnusedPrivateField when local variable
hides member variable
* General
* [#1425](https://sourceforge.net/p/pmd/bugs/1425/): Invalid XML Characters in Output
* [#1429](https://sourceforge.net/p/pmd/bugs/1429/): Java - Parse Error: Cast in return expression