diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java index 53ac3a7e95..974e244369 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/comments/CommentDefaultAccessModifierRule.java @@ -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 annotations = parent.findChildrenOfType(ASTAnnotation.class); + for (ASTAnnotation annotation : annotations) { + List names = annotation.findDescendantsOfType(ASTName.class); + for (ASTName name : names) { + if (name.hasImageEqualTo("VisibleForTesting")) { + result = false; + break; + } + } + if (result == false) { + break; + } + } + } + return result; + } } \ No newline at end of file diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml index 7df25e71f7..4b8474bef8 100755 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/comments/xml/CommentDefaultAccessModifier.xml @@ -145,6 +145,16 @@ public enum TestEnum { FOO; public String getName() { return "foo"; } +} + ]]> + + + + #1430 CommentDefaultAccessModifier triggers on field annotated with @VisibleForTesting + 0 + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 56f66485c2..c34ce5cd9a 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -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