[java] Fix FP with CommentDefaultAccessModifierRule

- Enum constructors should not be reported, even if nested
 - Nested annotation members should not be reported
This commit is contained in:
Juan Martín Sotuyo Dodero
2018-05-29 23:35:51 -03:00
parent 3edb87360a
commit 286ee2906b
2 changed files with 41 additions and 4 deletions

View File

@@ -9,16 +9,21 @@ import java.util.List;
import java.util.Set;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
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.ASTConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
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.ASTTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.AbstractAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessNode;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaNode;
import net.sourceforge.pmd.lang.java.ast.Comment;
import net.sourceforge.pmd.lang.java.rule.documentation.AbstractCommentRule;
import net.sourceforge.pmd.properties.RegexProperty;
@@ -89,10 +94,16 @@ public class CommentDefaultAccessModifierRule extends AbstractCommentRule {
}
private boolean shouldReport(final AbstractJavaAccessNode decl) {
List<ASTClassOrInterfaceDeclaration> parentClassOrInterface = decl
.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
// ignore if is a Interface
return !parentClassOrInterface.isEmpty() && !parentClassOrInterface.get(0).isInterface()
final AbstractAnyTypeDeclaration parentClassOrInterface = decl
.getFirstParentOfType(AbstractAnyTypeDeclaration.class);
final boolean isConcreteClass = parentClassOrInterface instanceof ASTClassOrInterfaceDeclaration
&& !((ASTClassOrInterfaceDeclaration) parentClassOrInterface).isInterface();
final boolean isEnumConstructor = parentClassOrInterface instanceof ASTEnumDeclaration
&& decl instanceof ASTConstructorDeclaration;
// ignore if it's an Interface / Annotation / Enum constructor
return (isConcreteClass || !isEnumConstructor)
// check if the field/method/nested class has a default access
// modifier
&& decl.isPackagePrivate()

View File

@@ -180,6 +180,32 @@ public enum Bar {
ONE, TWO;
Bar() {}
}
]]></code>
</test-code>
<test-code>
<description>Nested Enum constructor with implicit private modifier should not trigger</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
enum Bar {
ONE, TWO;
Bar() {}
}
}
]]></code>
</test-code>
<test-code>
<description>Nested annotation method with default access modifier should not trigger</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@interface Bar {
String baz();
}
}
]]></code>
</test-code>