Skip to content

Commit

Permalink
Issue checkstyle#7161: ENUM_DEF support added for RightCurlyCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
shashvat-kedia authored and romani committed Jan 8, 2020
1 parent 43dd461 commit 95defdd
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 31 deletions.
1 change: 1 addition & 0 deletions config/checkstyle_checks.xml
Expand Up @@ -266,6 +266,7 @@
<property name="tokens" value="LITERAL_IF"/>
<property name="tokens" value="LITERAL_TRY"/>
<property name="tokens" value="ANNOTATION_DEF"/>
<property name="tokens" value="ENUM_DEF"/>
<property name="option" value="alone"/>
</module>
<module name="RightCurly">
Expand Down
Expand Up @@ -81,15 +81,20 @@ public void testRightCurlySameAndLiteralDoDefault() throws Exception {
@Test
public void testRightCurlyAloneOther() throws Exception {
final String[] expected = {
"72:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"97:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"97:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"108:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"108:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"122:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
"122:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 6),
"125:57: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 57),
"148:39: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 39),
"150:61: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 61),
"153:28: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 28),
"163:16: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 16),
"165:30: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 30),
"168:16: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 16),
};

final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
Expand Down
Expand Up @@ -69,7 +69,7 @@ public enum GreetingsEnum
{
HELLO,
GOODBYE
}; //ok
}; //warn

void method2()
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public void fooInnerMethod ()
}} //warn

class EnumContainerAlone {
private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS } // ok
private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS } // warn
}

class WithArraysAlone {
Expand Down Expand Up @@ -159,3 +159,17 @@ class Interface {
public @interface TestAnnotation4 { String someValue();
} //ok
}

enum TestEnum {} //warn

enum TestEnum1 { SOME_VALUE; } //warn

enum TestEnum2 {
SOME_VALUE;} //warn

enum TestEnum3 {
SOME_VALUE;
}

enum TestEnum4 { SOME_VALUE;
}
Expand Up @@ -19,6 +19,7 @@

package com.puppycrawl.tools.checkstyle.checks.blocks;

import java.util.Arrays;
import java.util.Locale;

import com.puppycrawl.tools.checkstyle.DetailAstImpl;
Expand All @@ -34,7 +35,7 @@
* Checks the placement of right curly braces (<code>'}'</code>)
* for if-else, try-catch-finally blocks, while-loops, for-loops,
* method definitions, class definitions, constructor definitions,
* instance, static initialization blocks and annotation definitions.
* instance, static initialization blocks, annotation definitions and enum definitions.
* For right curly brace of expression blocks please follow issue
* <a href="https://github.com/checkstyle/checkstyle/issues/5945">#5945</a>.
* </p>
Expand Down Expand Up @@ -143,6 +144,7 @@ public int[] getAcceptableTokens() {
TokenTypes.STATIC_INIT,
TokenTypes.INSTANCE_INIT,
TokenTypes.ANNOTATION_DEF,
TokenTypes.ENUM_DEF,
};
}

Expand Down Expand Up @@ -337,6 +339,15 @@ private static boolean hasLineBreakBefore(DetailAST rightCurly) {
*/
private static final class Details {

/**
* Token types that identify tokens that will never have SLIST in their AST.
*/
private static final int[] TOKENS_WITH_NO_CHILD_SLIST = {
TokenTypes.CLASS_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.ANNOTATION_DEF,
};

/** Right curly. */
private final DetailAST rcurly;
/** Left curly. */
Expand Down Expand Up @@ -455,15 +466,15 @@ private static Details getDetailsForIfElse(DetailAST ast) {

/**
* Collects validation details for CLASS_DEF, METHOD DEF, CTOR_DEF, STATIC_INIT,
* INSTANCE_INIT and ANNOTATION_DEF.
* INSTANCE_INIT, ANNOTATION_DEF and ENUM_DEF.
* @param ast a {@code DetailAST} value
* @return an object containing all details to make a validation
*/
private static Details getDetailsForOthers(DetailAST ast) {
DetailAST rcurly = null;
final DetailAST lcurly;
final int tokenType = ast.getType();
if (tokenType == TokenTypes.CLASS_DEF || tokenType == TokenTypes.ANNOTATION_DEF) {
if (isTokenWithNoChildSlist(tokenType)) {
final DetailAST child = ast.getLastChild();
lcurly = child.getFirstChild();
rcurly = child.getLastChild();
Expand All @@ -478,6 +489,16 @@ private static Details getDetailsForOthers(DetailAST ast) {
return new Details(lcurly, rcurly, getNextToken(ast), true);
}

/**
* Tests whether the provided tokenType will never have a SLIST as child in its AST.
* Like CLASS_DEF, ANNOTATION_DEF etc.
* @param tokenType the tokenType to test against.
* @return weather provided tokenType is definition token.
*/
private static boolean isTokenWithNoChildSlist(int tokenType) {
return Arrays.stream(TOKENS_WITH_NO_CHILD_SLIST).anyMatch(token -> token == tokenType);
}

/**
* Collects validation details for loops' tokens.
* @param ast a {@code DetailAST} value
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/google_checks.xml
Expand Up @@ -93,7 +93,7 @@
<property name="option" value="alone"/>
<property name="tokens"
value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT,
INSTANCE_INIT, ANNOTATION_DEF"/>
INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF"/>
</module>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
Expand Down
Expand Up @@ -67,12 +67,14 @@ public void testSame() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.SAME.toString());
checkConfig.addAttribute("tokens", "LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, "
+ "LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO");
+ "LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, "
+ "ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"25:17: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 17),
"28:17: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 17),
"40:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_SAME, "}", 13),
"86:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"93:27: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
"188:9: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 9),
"189:53: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 53),
Expand All @@ -93,7 +95,7 @@ public void testSameDoesNotComplainForNonMultilineConstructs() throws Exception
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.SAME.toString());
checkConfig.addAttribute("tokens", "LITERAL_DO, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT,"
+ "INSTANCE_INIT, CLASS_DEF, METHOD_DEF, CTOR_DEF, ANNOTATION_DEF");
+ "INSTANCE_INIT, CLASS_DEF, METHOD_DEF, CTOR_DEF, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputRightCurlySame.java"), expected);
}
Expand Down Expand Up @@ -123,8 +125,10 @@ public void testAlone() throws Exception {
public void testNewLine() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, CTOR_DEF, ANNOTATION_DEF");
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, CTOR_DEF, "
+ "ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"86:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"111:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"111:6: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 6),
"122:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
Expand All @@ -143,8 +147,10 @@ public void testNewLine() throws Exception {
public void testShouldStartLine2() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.ALONE_OR_SINGLELINE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, ANNOTATION_DEF");
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, "
+ "ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"86:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"111:6: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 6),
"122:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"122:6: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 6),
Expand Down Expand Up @@ -183,7 +189,7 @@ public void testNullPointerException() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, "
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF");
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputRightCurlyEmptyAbstractMethod.java"), expected);
}
Expand All @@ -194,7 +200,7 @@ public void testWithAnnotations() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, "
+ "LITERAL_IF, LITERAL_ELSE, CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, "
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF");
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"8:77: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 77),
"11:65: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 65),
Expand Down Expand Up @@ -276,7 +282,7 @@ public void testAloneOrSingleLine() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE_OR_SINGLELINE.toString());
checkConfig.addAttribute("tokens", "LITERAL_TRY, LITERAL_CATCH, LITERAL_FINALLY, "
+ "LITERAL_IF, LITERAL_ELSE, CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, "
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF");
+ "LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"60:26: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 26),
"74:42: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 42),
Expand Down Expand Up @@ -460,7 +466,7 @@ public void testOptionAlone() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, LITERAL_IF, LITERAL_ELSE, "
+ "LITERAL_DO, LITERAL_WHILE, LITERAL_FOR, STATIC_INIT, "
+ "INSTANCE_INIT, ANNOTATION_DEF");
+ "INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"7:15: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 15),
"8:21: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 21),
Expand Down Expand Up @@ -490,7 +496,7 @@ public void testOptionAloneOrSingleLine() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE_OR_SINGLELINE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, LITERAL_IF, LITERAL_ELSE, "
+ "LITERAL_DO, LITERAL_WHILE, LITERAL_FOR, STATIC_INIT, "
+ "INSTANCE_INIT, ANNOTATION_DEF");
+ "INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"12:26: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 26),
"21:37: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 37),
Expand All @@ -508,14 +514,17 @@ public void testBlocksEndingWithSemiOptionSame() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.SAME.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, "
+ "CTOR_DEF, ANNOTATION_DEF");
+ "CTOR_DEF, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"14:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"19:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"25:9: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 9),
"33:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 29),
"39:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"42:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"49:20: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 20),
"55:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"58:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
};
verify(checkConfig, getPath("InputRightCurlySameBlocksWithSemi.java"), expected);
}
Expand All @@ -525,7 +534,7 @@ public void testBlocksEndingWithSemiOptionAlone() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, "
+ "CTOR_DEF, ANNOTATION_DEF");
+ "CTOR_DEF, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"11:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
"14:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
Expand All @@ -538,6 +547,11 @@ public void testBlocksEndingWithSemiOptionAlone() throws Exception {
"39:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"42:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"44:61: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 61),
"46:19: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 19),
"49:20: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 20),
"51:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"55:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"58:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
};
verify(checkConfig, getPath("InputRightCurlyAloneBlocksWithSemi.java"), expected);
}
Expand All @@ -548,17 +562,56 @@ public void testBlocksEndingWithSemiOptionAloneOrSingleLine() throws Exception {
checkConfig.addAttribute("option",
RightCurlyOption.ALONE_OR_SINGLELINE.toString());
checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF, "
+ "CTOR_DEF, ANNOTATION_DEF");
+ "CTOR_DEF, ANNOTATION_DEF, ENUM_DEF");
final String[] expected = {
"14:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"19:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"25:9: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 9),
"33:29: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 29),
"39:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"42:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"49:20: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 20),
"55:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
"58:5: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 5),
};
verify(checkConfig,
getPath("InputRightCurlyAloneOrSingleLineBlocksWithSemi.java"), expected);
}

@Test
public void testNewTokensAlone() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "ENUM_DEF");
final String[] expected = {
"11:19: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 19),
"14:20: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 20),
"16:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
};
verify(checkConfig, getPath("InputRightCurlyAloneNewTokens.java"), expected);
}

@Test
public void testNewTokensAloneOrSingleLine() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option",
RightCurlyOption.ALONE_OR_SINGLELINE.toString());
checkConfig.addAttribute("tokens", "ENUM_DEF");
final String[] expected = {
"14:20: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 20),
};
verify(checkConfig, getPath("InputRightCurlyAloneOrSingleLineNewTokens.java"), expected);
}

@Test
public void testNewTokensSame() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RightCurlyCheck.class);
checkConfig.addAttribute("option", RightCurlyOption.SAME.toString());
checkConfig.addAttribute("tokens", "ENUM_DEF");
final String[] expected = {
"14:20: " + getCheckMessage(MSG_KEY_LINE_BREAK_BEFORE, "}", 20),
};
verify(checkConfig, getPath("InputRightCurlySameNewTokens.java"), expected);
}

}
@@ -1,7 +1,7 @@
/*
* Config:
* option = alone
* tokens = CLASS_DEF, METHOD_DEF, CTOR_DEF, ANNOTATION_DEF
* tokens = CLASS_DEF, METHOD_DEF, CTOR_DEF, ANNOTATION_DEF, ENUM_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;
Expand Down Expand Up @@ -43,4 +43,18 @@ public void testMethod11() {

public @interface TestAnnotation9 { String someValue(); }; //violation

enum TestEnum{}; //violation

enum TestEnum1{
SOME_VALUE;}; //violation

enum TestEnum2 { SOME_VALUE; }; //violation

enum TestEnum3{
SOME_VALUE;
}; //violation

enum TestEnum4{ SOME_VALUE;
}; //violation

}
@@ -0,0 +1,24 @@
/*
* Config:
* option = alone
* tokens = ENUM_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

public class InputRightCurlyAloneNewTokens {

enum TestEnum{} //violation

enum TestEnum1{
SOME_VALUE;} //violation

enum TestEnum2 { SOME_VALUE; } //violation

enum TestEnum3{
SOME_VALUE;
}

enum TestEnum4{ SOME_VALUE;
}
}

0 comments on commit 95defdd

Please sign in to comment.