From a123763c92761d644fed6010c05ff01cece025ae Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 13 Aug 2014 00:23:37 +0400 Subject: [PATCH] Left curly update. Force line break #247 --- .../checks/blocks/LeftCurlyCheck.java | 44 ++++++++++++ .../checks/blocks/messages.properties | 3 + .../checks/blocks/LeftCurlyCheckTest.java | 26 +++++++ .../InputLeftCurlyLineBreakAfter.java | 72 +++++++++++++++++++ src/xdocs/config_blocks.xml | 14 ++++ 5 files changed, 159 insertions(+) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/InputLeftCurlyLineBreakAfter.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheck.java index d5b4d46dd..c0f34d1f9 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheck.java @@ -59,9 +59,18 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck; * value="nlow"/> <property name="maxLineLength" value="120"/> < * /module> * + *

+ * An example of how to configure the check to validate enum definitions: + *

+ *
+ * <module name="LeftCurly">
+ *      <property name="ignoreEnums" value="false"/>
+ * </module>
+ * 
* * @author Oliver Burn * @author lkuehne + * @author maxvetrenko * @version 1.0 */ public class LeftCurlyCheck @@ -73,6 +82,9 @@ public class LeftCurlyCheck /** TODO: replace this ugly hack **/ private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH; + /** If true, Check will ignore enums*/ + private boolean mIgnoreEnums = true; + /** * Creates a default instance and sets the policy to EOL. */ @@ -270,6 +282,9 @@ public class LeftCurlyCheck log(aBrace.getLineNo(), aBrace.getColumnNo(), "line.previous", "{"); } + if (!hasLineBreakAfter(aBrace)) { + log(aBrace.getLineNo(), aBrace.getColumnNo(), "line.break.after"); + } } else if (getAbstractOption() == LeftCurlyOption.NLOW) { if (aStartToken.getLineNo() == aBrace.getLineNo()) { @@ -291,4 +306,33 @@ public class LeftCurlyCheck } } } + + /** + * Checks if left curly has line break after. + * @param aLeftCurly + * Left curly token. + * @return + * True, left curly has line break after. + */ + private boolean hasLineBreakAfter(DetailAST aLeftCurly) + { + DetailAST nextToken = null; + if (aLeftCurly.getType() == TokenTypes.SLIST) { + nextToken = aLeftCurly.getFirstChild(); + } + else { + if (aLeftCurly.getParent().getParent().getType() == TokenTypes.ENUM_DEF) + { + if (!mIgnoreEnums) { + nextToken = aLeftCurly.getNextSibling(); + } + } + } + if (nextToken != null && nextToken.getType() != TokenTypes.RCURLY) { + if (aLeftCurly.getLineNo() == nextToken.getLineNo()) { + return false; + } + } + return true; + } } diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/blocks/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/blocks/messages.properties index dd72f142f..42b68e3af 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/blocks/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/blocks/messages.properties @@ -9,3 +9,6 @@ line.same=''{0}'' should be on the same line. needBraces=''{0}'' construct must use '''{}'''s. +line.break.after='''{''' should have line break after. + + diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheckTest.java index 6033b71bf..9d6b8b091 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/LeftCurlyCheckTest.java @@ -134,6 +134,7 @@ public class LeftCurlyCheckTest extends BaseCheckTestSupport "63:9: '{' should be on the previous line.", "83:5: '{' should be on the previous line.", "89:5: '{' should be on the previous line.", + "97:19: '{' should have line break after.", "106:1: '{' should be on the previous line.", "109:9: '{' should be on the previous line.", "118:1: '{' should be on the previous line.", @@ -197,4 +198,29 @@ public class LeftCurlyCheckTest extends BaseCheckTestSupport }; verify(mCheckConfig, getPath("InputLeftCurlyAnnotations.java"), expected); } + + @Test + public void testLineBreakAfter() throws Exception + { + mCheckConfig.addAttribute("option", LeftCurlyOption.EOL.toString()); + mCheckConfig.addAttribute("maxLineLength", "100"); + final String[] expected = { + "9:1: '{' should be on the previous line.", + "12:5: '{' should be on the previous line.", + "16:9: '{' should be on the previous line.", + "18:13: '{' should be on the previous line.", + "20:17: '{' should be on the previous line.", + "26:22: '{' should have line break after.", + "28:17: '{' should be on the previous line.", + "35:33: '{' should have line break after.", + "36:21: '{' should have line break after.", + "39:29: '{' should have line break after.", + "39:34: '{' should have line break after.", + "45:37: '{' should have line break after.", + "53:5: '{' should be on the previous line.", + "54:19: '{' should have line break after.", + "64:1: '{' should be on the previous line.", + }; + verify(mCheckConfig, getPath("InputLeftCurlyLineBreakAfter.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/InputLeftCurlyLineBreakAfter.java b/src/test/resources/com/puppycrawl/tools/checkstyle/InputLeftCurlyLineBreakAfter.java new file mode 100644 index 000000000..7aa6d759d --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/InputLeftCurlyLineBreakAfter.java @@ -0,0 +1,72 @@ +package com.puppycrawl.tools.checkstyle; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.ArrayList; +import java.util.List; + +class InputLeftCurlyOther +{ + /** @see test method **/ + int foo() throws InterruptedException + { + int x = 1; + int a = 2; + while (true) + { + try + { + if (x > 0) + { + break; + } + else if (x < 0) { + ; + } + else { break; } + switch (a) + { + case 0: + break; + default: + break; + } + } + catch (Exception e) { break; } + finally { break; } + } + + synchronized (this) { do { x = 2; } while (x == 2); } + + synchronized (this) { + do {} while (x == 2); + } + + for (int k = 0; k < 1; k++) { String innerBlockVariable = ""; } + + for (int k = 0; k < 1; k++) {} + } + + static { int x = 1; } + + void method2() + { + if (flag) { System.err.println("foo"); } + } +} + +class Absent_CustomFieldSerializer { + + public static void serialize() {} +} + +class Absent_CustomFieldSerializer +{ + public Absent_CustomFieldSerializer() {} +} + +class EmptyClass {} + +interface EmptyInterface {} + +enum KnownOrder { KNOWN_ORDER, UNKNOWN_ORDER } \ No newline at end of file diff --git a/src/xdocs/config_blocks.xml b/src/xdocs/config_blocks.xml index 6743f4192..dc65783b2 100644 --- a/src/xdocs/config_blocks.xml +++ b/src/xdocs/config_blocks.xml @@ -116,6 +116,12 @@ left curly brace policy eol + + ignoreEnums + If true, Check will ignore enums + boolean + true + maxLineLength maximum number of characters in a line @@ -174,6 +180,14 @@ <module name="LeftCurly"> <property name="option" value="nl"/> <property name="tokens" value="CLASS_DEF,INTERFACE_DEF"/> +</module> + +

+ An example of how to configure the check to validate enum definitions: +

+ +<module name="LeftCurly"> + <property name="ignoreEnums" value="false"/> </module>