Skip to content

Commit

Permalink
Issue #11736: Support qualified annotation names for MissingJavadocTy…
Browse files Browse the repository at this point in the history
…peCheck
  • Loading branch information
stoyanK7 committed Jul 4, 2022
1 parent a6b980f commit cbb051d
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 12 deletions.
Expand Up @@ -123,20 +123,37 @@ public static boolean containsAnnotation(DetailAST ast, Set<String> annotations)

if (!annotations.isEmpty()) {
final DetailAST firstMatchingAnnotation = findFirstAnnotation(ast, annotationNode -> {
DetailAST identNode = annotationNode.findFirstToken(TokenTypes.IDENT);
if (identNode == null) {
identNode = annotationNode.findFirstToken(TokenTypes.DOT)
.findFirstToken(TokenTypes.IDENT);
}

return annotations.contains(identNode.getText());
return doesAnnotationMatch(annotationNode, annotations);
});
result = firstMatchingAnnotation != null;
}

return result;
}

/**
* Checks if annotation is found in the set of provided annotations.
*
* @param annotations A collection of annotations to look through.
* @param annotationNode Annotation to look for.
* @return {@code true} if the annotation is contained in the set.
*/
private static boolean doesAnnotationMatch(DetailAST annotationNode, Set<String> annotations) {
final DetailAST identNode = annotationNode.findFirstToken(TokenTypes.IDENT);
final String annotationString;

// If no `IDENT` is found, then we have a `DOT` -> more than 1 qualifier
if (identNode == null) {
final DetailAST dotNode = annotationNode.findFirstToken(TokenTypes.DOT);
annotationString = FullIdent.createFullIdent(dotNode).getText();
}
else {
annotationString = identNode.getText();
}

return annotations.contains(annotationString);
}

/**
* Checks if the AST is annotated with {@code Override} or
* {@code java.lang.Override} annotation.
Expand Down
Expand Up @@ -82,7 +82,9 @@ public void newTest() throws Exception {

@Test
public void allowedAnnotationsTest() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
final String[] expected = {
"31:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocMethodAllowedAnnotations.java"), expected);
}
Expand Down
Expand Up @@ -291,4 +291,45 @@ public void testInterfaceMemberScopeIsPublic() throws Exception {
expected);
}

@Test
public void testFullyQualifiedAnnotation1() throws Exception {
final String[] expected = {
"16:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"19:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeFullyQualifiedAnnotation1.java"), expected);
}

@Test
public void testFullyQualifiedAnnotation2() throws Exception {
final String[] expected = {
"19:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeFullyQualifiedAnnotation2.java"), expected);
}

@Test
public void testFullyQualifiedAnnotation3() throws Exception {
final String[] expected = {
"16:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"22:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeFullyQualifiedAnnotation3.java"), expected);
}

@Test
public void testFullyQualifiedAnnotation4() throws Exception {
final String[] expected = {
"17:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"20:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeFullyQualifiedAnnotation4.java"), expected);
}

}
Expand Up @@ -226,7 +226,7 @@ public void testContainsAnnotationListWithNoAnnotationNode() {
}

@Test
public void testContainsAnnotationListWithEmptyAnnotationNode() {
public void testContainsAnnotationListWithIncompleteAnnotationNode() {
final DetailAstImpl ast = new DetailAstImpl();
final DetailAstImpl modifiersAst = create(
TokenTypes.MODIFIERS,
Expand All @@ -243,9 +243,9 @@ public void testContainsAnnotationListWithEmptyAnnotationNode() {
ast.addChild(modifiersAst);
final Set<String> annotations = Set.of("Override");
final boolean result = AnnotationUtil.containsAnnotation(ast, annotations);
assertWithMessage("The dot-ident variation should also work")
assertWithMessage("The dot-ident variation should not work")
.that(result)
.isTrue();
.isFalse();
}

@Test
Expand Down
Expand Up @@ -28,7 +28,7 @@ public void allowed1() {}
@ThisIsOkToo
public void allowed2() {}

@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadocmethod.ThisIsOk
@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadocmethod.ThisIsOk // violation
public void allowed3() {}

@Override
Expand Down
@@ -0,0 +1,25 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = (default)Generated
tokens = INTERFACE_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype;

public class InputMissingJavadocTypeFullyQualifiedAnnotation1 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation
public interface A { }

@InputMissingJavadocTypeFullyQualifiedAnnotation1.SomeAnnotation // violation
public interface B { }

@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype. // violation
InputMissingJavadocTypeFullyQualifiedAnnotation1.SomeAnnotation
public interface C { }
}
@@ -0,0 +1,25 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = SomeAnnotation
tokens = INTERFACE_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype;

public class InputMissingJavadocTypeFullyQualifiedAnnotation2 {
public @interface SomeAnnotation { }

@SomeAnnotation // ok
public interface A { }

@InputMissingJavadocTypeFullyQualifiedAnnotation2.SomeAnnotation // violation
public interface B { }

@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype. // violation
InputMissingJavadocTypeFullyQualifiedAnnotation2.SomeAnnotation
public interface C { }
}
@@ -0,0 +1,25 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = InputMissingJavadocTypeFullyQualifiedAnnotation3.SomeAnnotation
tokens = INTERFACE_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype;

public class InputMissingJavadocTypeFullyQualifiedAnnotation3 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation
public interface A { }

@InputMissingJavadocTypeFullyQualifiedAnnotation3.SomeAnnotation // ok
public interface B { }

@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype. // violation
InputMissingJavadocTypeFullyQualifiedAnnotation3.SomeAnnotation
public interface C { }
}
@@ -0,0 +1,26 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype.\
InputMissingJavadocTypeFullyQualifiedAnnotation4.SomeAnnotation
tokens = INTERFACE_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype;

public class InputMissingJavadocTypeFullyQualifiedAnnotation4 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation
public interface A { }

@InputMissingJavadocTypeFullyQualifiedAnnotation4.SomeAnnotation // violation
public interface B { }

@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype. // ok
InputMissingJavadocTypeFullyQualifiedAnnotation4.SomeAnnotation
public interface C { }
}

0 comments on commit cbb051d

Please sign in to comment.