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 authored and strkkk committed Jul 9, 2022
1 parent 7bc872e commit 2282a7e
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 31 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 = {
"32:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocMethodAllowedAnnotations.java"), expected);
}
Expand Down
Expand Up @@ -291,4 +291,75 @@ public void testInterfaceMemberScopeIsPublic() throws Exception {
expected);
}

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

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

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

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

@Test
public void testQualifiedAnnotation5() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeQualifiedAnnotation5.java"), expected);
}

@Test
public void testMultipleQualifiedAnnotation() throws Exception {
final String[] expected = {
"29:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"38:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeMultipleQualifiedAnnotation.java"), expected);
}

@Test
public void testQualifiedAnnotationWithParameters() throws Exception {
final String[] expected = {
"33:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"37:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"42:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
"50:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(
getPath("InputMissingJavadocTypeQualifiedAnnotationWithParameters.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,6 +28,7 @@ public void allowed1() {}
@ThisIsOkToo
public void allowed2() {}

// violation below 'Missing a Javadoc comment.'
@com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadocmethod.ThisIsOk
public void allowed3() {}

Expand Down
@@ -0,0 +1,47 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = Ann1, AnnClass.Ann3
tokens = INTERFACE_DEF
*/

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

@interface Ann1 { }
@interface Ann2 { }
@interface Ann3 { }

class AnnClass {
public @interface Ann1 { }
public @interface Ann2 { }
public @interface Ann3 { }
}

public class InputMissingJavadocTypeMultipleQualifiedAnnotation {
@Ann1 // ok
@Ann2
@Ann3
public interface A { }

@Ann2 // violation 'Missing a Javadoc comment.'
@Ann3
public interface B { }

@Ann2 // ok
@Ann3
@Ann1
public interface C { }

@AnnClass.Ann1 // violation 'Missing a Javadoc comment.'
@Ann2
@Ann3
public interface D { }

@AnnClass.Ann2 // ok
@Ann2
@AnnClass.Ann3
public interface E { }
}
@@ -0,0 +1,26 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = (default)Generated
tokens = INTERFACE_DEF
*/

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

public class InputMissingJavadocTypeQualifiedAnnotation1 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation 'Missing a Javadoc comment.'
public interface A { }

// violation below 'Missing a Javadoc comment.'
@InputMissingJavadocTypeQualifiedAnnotation1.SomeAnnotation
public interface B { }

@com.puppycrawl.tools.checkstyle.checks.javadoc // violation 'Missing a Javadoc comment.'
.missingjavadoctype.InputMissingJavadocTypeQualifiedAnnotation1.SomeAnnotation
public interface C { }
}
@@ -0,0 +1,26 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = SomeAnnotation
tokens = INTERFACE_DEF
*/

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

public class InputMissingJavadocTypeQualifiedAnnotation2 {
public @interface SomeAnnotation { }

@SomeAnnotation // ok
public interface A { }

// violation below 'Missing a Javadoc comment.'
@InputMissingJavadocTypeQualifiedAnnotation2.SomeAnnotation
public interface B { }

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

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

public class InputMissingJavadocTypeQualifiedAnnotation3 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation 'Missing a Javadoc comment.'
public interface A { }

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

@com.puppycrawl.tools.checkstyle.checks.javadoc // violation 'Missing a Javadoc comment.'
.missingjavadoctype.InputMissingJavadocTypeQualifiedAnnotation3.SomeAnnotation
public interface C { }
}
@@ -0,0 +1,27 @@
/*
MissingJavadocType
scope = (default)public
excludeScope = (default)null
skipAnnotations = com.puppycrawl.tools.checkstyle.checks.javadoc.missingjavadoctype.\
InputMissingJavadocTypeQualifiedAnnotation4.SomeAnnotation
tokens = INTERFACE_DEF
*/

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

public class InputMissingJavadocTypeQualifiedAnnotation4 {
public @interface SomeAnnotation { }

@SomeAnnotation // violation 'Missing a Javadoc comment.'
public interface A { }

// violation below 'Missing a Javadoc comment.'
@InputMissingJavadocTypeQualifiedAnnotation4.SomeAnnotation
public interface B { }

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

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

public class InputMissingJavadocTypeQualifiedAnnotation5 {
public @interface SomeAnnotation { }

@SomeAnnotation // ok
public interface A { }

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

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

0 comments on commit 2282a7e

Please sign in to comment.