Skip to content

Commit

Permalink
Issue checkstyle#3210: fix compact_no_array in AnnotationUseStyleCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
strkkk committed Jun 14, 2019
1 parent a1546b7 commit 4c799ff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Expand Up @@ -63,8 +63,6 @@
* Using the {@code ElementStyle.COMPACT_NO_ARRAY} style is less verbose.
* It is similar to the {@code ElementStyle.COMPACT} style but single value arrays are flagged.
* With annotations a single value array does not need to be placed in an array initializer.
* This style can only be used when there is an element called 'value' which is either
* the sole element or all other elements have default values.
* </p>
* <p>
* The ending parenthesis are optional when using annotations with no elements.
Expand Down Expand Up @@ -159,7 +157,7 @@ public enum ElementStyle {
COMPACT,

/**
* Compact example.]
* Compact example
*
* <pre>@SuppressWarnings("unchecked")</pre>.
*/
Expand Down Expand Up @@ -428,28 +426,24 @@ private void checkCompactNoArrayStyle(final DetailAST annotation) {
final DetailAST arrayInit =
annotation.findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT);

final int valuePairCount =
annotation.getChildCount(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR);

//in compact style with one value
if (arrayInit != null
&& arrayInit.getChildCount(TokenTypes.EXPR) == 1) {
log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT_NO_ARRAY);
}
//in expanded style with one value and the correct element name
else if (valuePairCount == 1) {
final DetailAST valuePair =
annotation.findFirstToken(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR);
final DetailAST nestedArrayInit =
valuePair.findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT);

if (nestedArrayInit != null
&& ANNOTATION_ELEMENT_SINGLE_NAME.equals(
valuePair.getFirstChild().getText())
//in expanded style with pairs
else {
DetailAST ast = annotation.getFirstChild();
while (ast != null) {
final DetailAST nestedArrayInit =
ast.findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT);
if (nestedArrayInit != null
&& nestedArrayInit.getChildCount(TokenTypes.EXPR) == 1) {
log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT_NO_ARRAY);
log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE,
ElementStyle.COMPACT_NO_ARRAY);
}
ast = ast.getNextSibling();
}
}
}
Expand Down
Expand Up @@ -82,9 +82,13 @@ public void testClosingParensValueOf() {
public void testDefault() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"13: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"24: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"30: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
"33: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
Expand Down Expand Up @@ -180,8 +184,12 @@ public void testStyleCompactNoArray() throws Exception {
checkConfig.addAttribute("elementStyle", "COMPACT_NO_ARRAY");
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"4: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"24: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
Expand Down Expand Up @@ -263,6 +271,8 @@ public void testTrailingArrayIgnore() throws Exception {
final String[] expected = {
"9: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"27: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"33: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
};

verify(checkConfig, getPath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
Expand Down
2 changes: 0 additions & 2 deletions src/xdocs/config_annotation.xml
Expand Up @@ -391,8 +391,6 @@ public int foo() { ... }
It is similar to the <code>ElementStyle.COMPACT</code> style but single value arrays
are flagged.
With annotations a single value array does not need to be placed in an array initializer.
This style can only be used when there is an element called 'value' which is either
the sole element or all other elements have default values.
</p>
<p>
The ending parenthesis are optional when using annotations with no elements.
Expand Down

0 comments on commit 4c799ff

Please sign in to comment.