Skip to content

Commit

Permalink
Issue checkstyle#3546: add lambda in a rightcurlycheck
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimP committed Jan 9, 2017
1 parent bcd5297 commit 57a7b94
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
Expand Up @@ -42,7 +42,8 @@
* {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH},
* {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY},
* {@link TokenTypes#LITERAL_IF LITERAL_IF},
* {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE}.
* {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
* {@link TokenTypes#LAMBDA LAMBDA}.
* Other acceptable tokens are:
* {@link TokenTypes#CLASS_DEF CLASS_DEF},
* {@link TokenTypes#METHOD_DEF METHOD_DEF},
Expand All @@ -52,6 +53,7 @@
* {@link TokenTypes#LITERAL_DO LITERAL_DO}.
* {@link TokenTypes#STATIC_INIT STATIC_INIT}.
* {@link TokenTypes#INSTANCE_INIT INSTANCE_INIT}.
* {@link TokenTypes#LAMBDA LAMBDA}.
* </p>
* <p>
* <b>shouldStartLine</b> - does the check need to check
Expand Down Expand Up @@ -143,6 +145,7 @@ public int[] getDefaultTokens() {
TokenTypes.LITERAL_FINALLY,
TokenTypes.LITERAL_IF,
TokenTypes.LITERAL_ELSE,
TokenTypes.LAMBDA,
};
}

Expand All @@ -162,6 +165,7 @@ public int[] getAcceptableTokens() {
TokenTypes.LITERAL_DO,
TokenTypes.STATIC_INIT,
TokenTypes.INSTANCE_INIT,
TokenTypes.LAMBDA,
};
}

Expand Down Expand Up @@ -203,14 +207,11 @@ public void visitToken(DetailAST ast) {
private static String validate(Details details, RightCurlyOption bracePolicy,
boolean shouldStartLine, String targetSourceLine) {
final DetailAST rcurly = details.rcurly;
final DetailAST lcurly = details.lcurly;
final DetailAST nextToken = details.nextToken;
final boolean shouldCheckLastRcurly = details.shouldCheckLastRcurly;
String violation = "";

if (bracePolicy == RightCurlyOption.SAME
&& !hasLineBreakBefore(rcurly)
&& lcurly.getLineNo() != rcurly.getLineNo()) {
if (shouldBeLineBreackBefore(bracePolicy, details)) {
violation = MSG_KEY_LINE_BREAK_BEFORE;
}
else if (shouldCheckLastRcurly) {
Expand All @@ -221,7 +222,9 @@ else if (shouldCheckLastRcurly) {
else if (shouldBeOnSameLine(bracePolicy, details)) {
violation = MSG_KEY_LINE_SAME;
}
else if (shouldBeAloneOnLine(bracePolicy, details)) {
else if (shouldBeAloneOnLine(bracePolicy, details)
|| shouldBeAloneInLambda(bracePolicy, details)
|| shouldBeAloneOrSLine(bracePolicy, details)) {
violation = MSG_KEY_LINE_ALONE;
}
else if (shouldStartLine && !isOnStartOfLine(details, targetSourceLine)) {
Expand All @@ -230,6 +233,20 @@ else if (shouldStartLine && !isOnStartOfLine(details, targetSourceLine)) {
return violation;
}

/**
* Checks that before a right curly should be linebreack.
* @param bracePolicy options for placing the right curly brace.
* @param details Details for validation
* @return true if before a right curly should be linebreack.
*/
private static boolean shouldBeLineBreackBefore(RightCurlyOption bracePolicy, Details details) {
final DetailAST rcurly = details.rcurly;
final DetailAST lcurly = details.lcurly;
return bracePolicy == RightCurlyOption.SAME
&& !hasLineBreakBefore(rcurly)
&& lcurly.getLineNo() != rcurly.getLineNo();
}

/**
* Checks that a right curly should be on the same line as the next statement.
* @param bracePolicy option for placing the right curly brace
Expand All @@ -248,16 +265,41 @@ private static boolean shouldBeOnSameLine(RightCurlyOption bracePolicy, Details
* @return true if a right curly should be alone on a line.
*/
private static boolean shouldBeAloneOnLine(RightCurlyOption bracePolicy, Details details) {
final int tockenLambda = details.lcurly.getParent().getType();
return bracePolicy == RightCurlyOption.ALONE
&& tockenLambda != TokenTypes.LAMBDA
&& !isAloneOnLine(details)
&& !isEmptyBody(details.lcurly)
|| bracePolicy == RightCurlyOption.ALONE_OR_SINGLELINE
&& !isEmptyBody(details.lcurly);
}

/**
* Checks that a right curly should be alone on a line or allows single-line format of block.
* ShouldBeAloneSLine = shouldBeAloneOrSingleLine it is for skip check "is longer than 100 char"
* @param bracePolicy option for placing the right curly brace
* @param details Details for validation
* @return true if a right curly should be alone on a line or single-line format of block.
*/
private static boolean shouldBeAloneOrSLine(RightCurlyOption bracePolicy, Details details) {
return bracePolicy == RightCurlyOption.ALONE_OR_SINGLELINE
&& !isAloneOnLine(details)
&& !isSingleLineBlock(details)
&& !isAnonInnerClassInit(details.lcurly)
&& !isEmptyBody(details.lcurly);
}

/**
* Checks that a right curly should be only with semi on a line in the tocken is LAMBDA.
* @param bracePolicy option for placing the right curly brace
* @param details Details for validation
* @return true if after right curly should be with semi on line in the tocken is LAMBDA.
*/
private static boolean shouldBeAloneInLambda(RightCurlyOption bracePolicy, Details details) {
final int tockenLambda = details.lcurly.getParent().getType();
return bracePolicy == RightCurlyOption.ALONE
&& tockenLambda == TokenTypes.LAMBDA
&& !isAloneInLambda(details);
}

/**
* Whether right curly brace starts target source line.
* @param details Details of right curly brace for validation
Expand Down Expand Up @@ -374,9 +416,14 @@ private static Details getDetails(DetailAST ast) {
rcurly = lcurly.getLastChild();
}
break;
case TokenTypes.LAMBDA:
lcurly = ast.getLastChild();
rcurly = lcurly.getLastChild();
nextToken = getNextToken(ast);
break;
default:
// ATTENTION! We have default here, but we expect case TokenTypes.METHOD_DEF,
// TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_WHILE, only.
// TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_WHILE, TokenTypes.LAMBDA only.
// It has been done to improve coverage to 100%. I couldn't replace it with
// if-else-if block because code was ugly and didn't pass pmd check.

Expand Down Expand Up @@ -443,6 +490,25 @@ private static boolean hasLineBreakBefore(DetailAST rightCurly) {
|| rightCurly.getLineNo() != previousToken.getLineNo();
}

/**
* Checks if right curly is alone on line in tocken tke lambda.
* @param details for validation.
* @return true, if right curly is alone whith SEMI on line.
*/
private static boolean isAloneInLambda(Details details) {
final DetailAST lcurly = details.lcurly;
final DetailAST rcurly = details.rcurly;
final int lcurlyNo = lcurly.getLineNo();
final int rcurlyNo = rcurly.getLineNo();
if (rcurly.getPreviousSibling() == null) {
return rcurlyNo != lcurlyNo;
}
else {
return rcurlyNo != lcurlyNo
&& rcurlyNo != rcurly.getPreviousSibling().getLineNo();
}
}

/**
* Structure that contains all details for validation.
*/
Expand Down
Expand Up @@ -259,6 +259,18 @@ public void testAloneOrSingleLine() throws Exception {
verify(checkConfig, getPath("InputRightCurlyAloneOrSingleline.java"), expected);
}

@Test
public void testLambda() throws Exception {
checkConfig.addAttribute("option", RightCurlyOption.ALONE.toString());
checkConfig.addAttribute("tokens", "LAMBDA");
final String[] expected = {
"14:72: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 72),
"17:48: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 48),
"24:33: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 33),
};
verify(checkConfig, getPath("InputRightCurlyAloneInLambda.java"), expected);
}

@Test
public void testCatchWithoutFinally() throws Exception {
final String[] expected = {
Expand Down
@@ -0,0 +1,29 @@
package com.puppycrawl.tools.checkstyle.checks.blocks;

/*
This is test class for token LAMBDA.
*/
public class InputRightCurlyAloneInLambda {

static Runnable r1 = () -> {
String.valueOf("Test rightCurly one!");
};

static Runnable r2 = () -> String.valueOf("Test rightCurly two!");

static Runnable r3 = () -> {String.valueOf("Test rightCurly two!");}; //violation

static Runnable r4 = () -> {
String.valueOf("Test rightCurly two!");}; //violation

static Runnable r5 = () ->
{
String.valueOf("Test rightCurly four!");
};

static Runnable r6 = () -> {};

static Runnable r7 = () ->
{
};
}
8 changes: 6 additions & 2 deletions src/xdocs/config_blocks.xml
Expand Up @@ -799,7 +799,9 @@ for(int i = 0; i &lt; 10; value.incrementValue()); // OK
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_INIT">STATIC_INIT</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INSTANCE_INIT">INSTANCE_INIT</a>.</td>
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INSTANCE_INIT">INSTANCE_INIT</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA">LAMBDA</a>.</td>

<td><a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_TRY">LITERAL_TRY</a>,
Expand All @@ -810,7 +812,9 @@ for(int i = 0; i &lt; 10; value.incrementValue()); // OK
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">LITERAL_IF</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE">LITERAL_ELSE</a>.</td>
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE">LITERAL_ELSE</a>,
<a
href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAMBDA">LAMBDA</a>.</td>
</tr>
</table>
</subsection>
Expand Down

0 comments on commit 57a7b94

Please sign in to comment.