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 8aa65e4
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 32 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());
final String annotationFullIdent = getAnnotationFullIdent(annotationNode);
return annotations.contains(annotationFullIdent);
});
result = firstMatchingAnnotation != null;
}

return result;
}

/**
* Gets the full ident text of the annotation AST.
*
* @param annotationNode The annotation AST.
* @return The full ident text.
*/
private static String getAnnotationFullIdent(DetailAST annotationNode) {
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 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 @@ -225,29 +225,6 @@ public void testContainsAnnotationListWithNoAnnotationNode() {
.isFalse();
}

@Test
public void testContainsAnnotationListWithEmptyAnnotationNode() {
final DetailAstImpl ast = new DetailAstImpl();
final DetailAstImpl modifiersAst = create(
TokenTypes.MODIFIERS,
create(
TokenTypes.ANNOTATION,
create(
TokenTypes.DOT,
create(
TokenTypes.IDENT,
"Override")
)
)
);
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")
.that(result)
.isTrue();
}

@Test
public void testContainsAnnotationListWithNoMatchingAnnotation() {
final DetailAstImpl ast = new DetailAstImpl();
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 8aa65e4

Please sign in to comment.