diff --git a/docs/config_coding.html b/docs/config_coding.html index ef9acd856..a2c8aa909 100644 --- a/docs/config_coding.html +++ b/docs/config_coding.html @@ -22,6 +22,9 @@

diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheck.java new file mode 100644 index 000000000..bcc55a814 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheck.java @@ -0,0 +1,67 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.api.DetailAST; + +/** + *

+ * Checks if array initialization contains optional trailing comma. + *

+ *

+ * Rationale: Putting this comma in make is easier to change the + * order of the elements or add new elements on the end. + *

+ *

+ * An example of how to configure the check is: + *

+ *
+ * <module name="ArrayTrailingComma"/>
+ * 
+ * @author o_sukhodolsky + */ +public class ArrayTrailingCommaCheck extends Check +{ + /** @see Check */ + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.ARRAY_INIT}; + } + + /** @see Check */ + public void visitToken(DetailAST aArrayInit) + { + DetailAST rcurly = aArrayInit.findFirstToken(TokenTypes.RCURLY); + + // if curlys are on the same line + // or array is empty then check nothing + if (aArrayInit.getLineNo() == rcurly.getLineNo() + || aArrayInit.getChildCount() == 1) + { + return; + } + + DetailAST prev = rcurly.getPreviousSibling(); + if (prev.getType() != TokenTypes.COMMA) { + log(rcurly.getLineNo(), "array.trailing.comma"); + } + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties index abe0052bc..70f9613c0 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -95,3 +95,5 @@ indentation.error={0} at indentation level {1} not at correct indentation, {2} indentation.child.error={0} child at indentation level {1} not at correct indentation, {2} interface.type=interfaces should describe a type and hence have methods. + +array.trailing.comma=Array should contain trailing comma. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputArrayTrailingComma.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputArrayTrailingComma.java new file mode 100644 index 000000000..fbe35ff4a --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputArrayTrailingComma.java @@ -0,0 +1,41 @@ +package com.puppycrawl.tools.checkstyle; + +public class InputArrayTrailingComma +{ + int[] a1 = new int[] + { + 1, + 2, + 3, + }; + + int[] a2 = new int[] + { + 1, + 2, + 3 + }; + + int[] b1 = new int[] {1, 2, 3,}; + int[] b2 = new int[] {1, 2, 3}; + + int[][] c1 = new int[][] {{1, 2,}, {3, 3,}, {5, 6,},}; + int[][] c2 = new int[][] {{1, 2}, {3, 3,}, {5, 6,}}; + + int[][] d1 = new int[][] + { + {1, 2,}, + {3, 3,}, + {5, 6,}, + }; + int[][] d2 = new int[][] + { + {1, + 2}, + {3, 3,}, + {5, 6,} + }; + + int[] e1 = new int[] { + }; +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java index fe5e2766b..089b3172e 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/AllTests.java @@ -4,6 +4,7 @@ import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterTest; import com.puppycrawl.tools.checkstyle.api.DetailASTTest; import com.puppycrawl.tools.checkstyle.checks.AvoidInlineConditionalsCheckTest; import com.puppycrawl.tools.checkstyle.checks.AvoidStarImportTest; +import com.puppycrawl.tools.checkstyle.checks.ArrayTrailingCommaCheckTest; import com.puppycrawl.tools.checkstyle.checks.ConstantNameCheckTest; import com.puppycrawl.tools.checkstyle.checks.DesignForExtensionCheckTest; import com.puppycrawl.tools.checkstyle.checks.DoubleCheckedLockingCheckTest; @@ -89,6 +90,7 @@ public class AllTests { suite.addTest(new TestSuite(AvoidInlineConditionalsCheckTest.class)); suite.addTest(new TestSuite(AvoidNestedBlocksCheckTest.class)); suite.addTest(new TestSuite(AvoidStarImportTest.class)); + suite.addTest(new TestSuite(ArrayTrailingCommaCheckTest.class)); suite.addTest(new TestSuite(ConfigurationLoaderTest.class)); suite.addTest(new TestSuite(ConstantNameCheckTest.class)); suite.addTest(new TestSuite(DesignForExtensionCheckTest.class)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheckTest.java new file mode 100644 index 000000000..13550195f --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/ArrayTrailingCommaCheckTest.java @@ -0,0 +1,21 @@ +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + +public class ArrayTrailingCommaCheckTest + extends BaseCheckTestCase +{ + public void testDefault() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(ArrayTrailingCommaCheck.class); + final String[] expected = { + "17: Array should contain trailing comma.", + "34: Array should contain trailing comma.", + "37: Array should contain trailing comma.", + }; + verify(checkConfig, getPath("InputArrayTrailingComma.java"), expected); + } +}