diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ArrayInitHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ArrayInitHandler.java index 79f1e009e..a842ba35f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ArrayInitHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ArrayInitHandler.java @@ -105,7 +105,8 @@ public class ArrayInitHandler extends BlockParentHandler // new int[] {1, 2, // 3}; - final IndentLevel expectedIndent = super.getChildrenExpectedLevel(); + final IndentLevel expectedIndent = + new IndentLevel(getLevel(), getIndentCheck().getArrayInitIndent()); final int firstLine = getFirstLine(Integer.MAX_VALUE, getListChild()); if (hasCurlys() && (firstLine == getLCurly().getLineNo())) { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java index 19bc6a5cb..078382691 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java @@ -122,6 +122,9 @@ public class IndentationCheck extends Check /** how far throws should be indented when on next line */ private int mThrowsIndentationAmount = DEFAULT_INDENTATION; + /** how much to indent an array initialisation when on next line */ + private int mArrayInitIndentationAmount = DEFAULT_INDENTATION; + /** handlers currently in use */ private final FastStack mHandlers = FastStack.newInstance(); @@ -214,6 +217,26 @@ public class IndentationCheck extends Check return this.mThrowsIndentationAmount; } + /** + * Set the array initialisation indentation level. + * + * @param aArrayInitIndent the array initialisation indentation level + */ + public void setArrayInitIndent(int aArrayInitIndent) + { + mArrayInitIndentationAmount = aArrayInitIndent; + } + + /** + * Get the array initialisation indentation level. + * + * @return the array initialisation indentation level + */ + public int getArrayInitIndent() + { + return this.mArrayInitIndentationAmount; + } + /** * Log an error message. * diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index 7bf5615f7..144571251 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -188,11 +188,25 @@ public class IndentationCheckTest extends BaseCheckTestSupport verify(c, fname, expected); } + @Test + public void testValidArrayInitDefaultIndentWithChecker() + throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + final Checker c = createChecker(checkConfig); + final String fname = getPath("indentation/InputValidArrayInitDefaultIndent.java"); + final String[] expected = { + }; + verify(c, fname, expected); + } + @Test public void testValidArrayInitWithChecker() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("arrayInitIndent", Integer.valueOf(8).toString()); + final Checker c = createChecker(checkConfig); final String fname = getPath("indentation/InputValidArrayInitIndent.java"); final String[] expected = { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitDefaultIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitDefaultIndent.java new file mode 100644 index 000000000..8167db4e0 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitDefaultIndent.java @@ -0,0 +1,105 @@ +/* + * InputValidArrayInitIndent.java + * + * Created on December 9, 2002, 9:57 PM + */ + +package com.puppycrawl.tools.checkstyle.indentation; + +public class InputValidArrayInitIndent { + + private static char[] sHexChars = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + int[] array = new int[] {1, 2, 3}; + + int[] array2 = new int[] { + 1, 2, 3 + }; + + int[] array3 = new int[] { + 1, + 2, + 3 + }; + + int[] array4 = new int[] + { + 1, + 2, + 3 + }; + + int[] array5 = new int[] + {1, 2, 3}; + + // check nesting on first line + int[] array6 = new int[] { 1, 2, + 3 + }; + + int[] array6a = new int[] { 1, 2, + 3, 4}; + + // nesting + int[] array7 = new int[] { + 1, 2, + 3 + }; + + String[][] mStuff = new String[][] { + { "key", "value" } + }; + + String[][] mStuff1 = new String[][] + { + { "key", "value" } + }; + + int[] array8 = new int[] { }; + + int[] array9 = new int[] { + }; + + int[][] array10 = new int[][] { + new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, + }; + + int[][] array10b + = new int[][] { + new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, + }; + + private void func1(int[] arg) { + + char[] sHexChars2 = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + char[] sHexChars3 = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + + char[] sHexChars4 = + { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + + } + + /** Creates a new instance of InputValidArrayInitIndent */ + public InputValidArrayInitIndent() { + + func1(new int[] {1, 2}); + func1(new int[] {}); + func1(new int[] { + 1, 2, 3 + }); + } + +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitIndent.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitIndent.java index 54833c4ba..c95deb49f 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitIndent.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputValidArrayInitIndent.java @@ -13,27 +13,27 @@ package com.puppycrawl.tools.checkstyle.indentation; public class InputValidArrayInitIndent { private static char[] sHexChars = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int[] array = new int[] {1, 2, 3}; int[] array2 = new int[] { - 1, 2, 3 + 1, 2, 3 }; int[] array3 = new int[] { - 1, - 2, - 3 + 1, + 2, + 3 }; int[] array4 = new int[] { - 1, - 2, - 3 + 1, + 2, + 3 }; int[] array5 = new int[] @@ -42,7 +42,7 @@ public class InputValidArrayInitIndent { // check nesting on first line int[] array6 = new int[] { 1, 2, - 3 + 3 }; int[] array6a = new int[] { 1, 2, @@ -50,17 +50,17 @@ public class InputValidArrayInitIndent { // nesting int[] array7 = new int[] { - 1, 2, - 3 + 1, 2, + 3 }; String[][] mStuff = new String[][] { - { "key", "value" } + { "key", "value" } }; String[][] mStuff1 = new String[][] { - { "key", "value" } + { "key", "value" } }; int[] array8 = new int[] { }; @@ -69,32 +69,32 @@ public class InputValidArrayInitIndent { }; int[][] array10 = new int[][] { - new int[] { 1, 2, 3}, - new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, }; int[][] array10b = new int[][] { - new int[] { 1, 2, 3}, - new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, + new int[] { 1, 2, 3}, }; private void func1(int[] arg) { char[] sHexChars2 = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] sHexChars3 = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] sHexChars4 = { - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; } @@ -106,7 +106,7 @@ public class InputValidArrayInitIndent { func1(new int[] {1, 2}); func1(new int[] {}); func1(new int[] { - 1, 2, 3 + 1, 2, 3 }); } diff --git a/src/xdocs/config_misc.xml b/src/xdocs/config_misc.xml index 28b260529..d20e28b40 100755 --- a/src/xdocs/config_misc.xml +++ b/src/xdocs/config_misc.xml @@ -809,6 +809,12 @@ messages.properties: Key 'ok' missing. Integer 4 + + arrayInitIndent + how much to indent an array initialisation when on next line + Integer + 4 +