Skip to content

Commit

Permalink
Issue checkstyle#6440: AnnotationLocation: named parameters must be c…
Browse files Browse the repository at this point in the history
…onsidered parameters
  • Loading branch information
pbludov committed Feb 20, 2019
1 parent 88907c5 commit 88c045a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Expand Up @@ -24,6 +24,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

/**
* Check location of annotation on language elements.
Expand Down Expand Up @@ -302,7 +303,10 @@ else if (annotation.getColumnNo() != correctIndentation && !hasNodeBefore(annota
* @return true if the annotation has parameters.
*/
private static boolean isParameterized(DetailAST annotation) {
return annotation.findFirstToken(TokenTypes.EXPR) != null;
return TokenUtil.findFirstTokenByPredicate(annotation, ast -> {
return ast.getType() == TokenTypes.EXPR
|| ast.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR;
}).isPresent();
}

/**
Expand Down
Expand Up @@ -59,6 +59,7 @@ public void testIncorrect() throws Exception {
final String[] expected = {
"6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"14: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
Expand All @@ -68,13 +69,17 @@ public void testIncorrect() throws Exception {
"37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"45: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
"56: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"73: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"75: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
"88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
"91: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
Expand All @@ -88,6 +93,7 @@ public void testIncorrectAllTokens() throws Exception {
final String[] expected = {
"6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"14: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
Expand All @@ -97,13 +103,17 @@ public void testIncorrectAllTokens() throws Exception {
"37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"45: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
"56: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"73: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"75: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
"88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
"91: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
"100: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
};
Expand Down
Expand Up @@ -11,7 +11,7 @@ class InputAnnotationLocationIncorrect
@MyAnnotation2 @MyAnnotation1(value = "")
public int a;

@MyAnnotation1(value = "") public int b;
@MyAnnotation1(value = "") public int b; //warn

@MyAnnotation2
@MyAnnotation1 //warn
Expand Down Expand Up @@ -42,7 +42,7 @@ class InnerClass
(value = "")
public int a;

@MyAnnotation1(value = "") public int b;
@MyAnnotation1(value = "") public int b; //warn

@MyAnnotation2
@MyAnnotation1 //warn
Expand All @@ -53,7 +53,7 @@ class InnerClass
public int d;

@MyAnnotation2
@MyAnnotation1(value = "") public InnerClass()
@MyAnnotation1(value = "") public InnerClass() //warn
{
// comment
}
Expand All @@ -72,7 +72,7 @@ void foo2() {}
{
@MyAnnotation2 @MyAnnotation1(value = "") public int a;

@MyAnnotation1(value = "") public int b;
@MyAnnotation1(value = "") public int b; //warn

@MyAnnotation2
@MyAnnotation1(value = "")
Expand All @@ -88,7 +88,7 @@ void foo2() {}
@MyAnnotation2 //warn
void foo2() {}

@MyAnnotation1(value = "") void foo42() {}
@MyAnnotation1(value = "") void foo42() {} //warn
};

}
Expand Down

0 comments on commit 88c045a

Please sign in to comment.