Added check for trailing comma in array initialization. (module ArrayTrailingComma, request 696301)

This commit is contained in:
Oleg Sukhodolsky 2003-05-29 03:30:44 +00:00
parent 45ce38623f
commit 55512ae033
7 changed files with 178 additions and 0 deletions

View File

@ -22,6 +22,9 @@
<!--Left menu-->
<td class="menu" valign="top">
<ul>
<li>
<a href="#ArrayTrailingComma">ArrayTrailingComma</a>
</li>
<li>
<a href="#AvoidInlineConditionals">AvoidInlineConditionals</a>
</li>
@ -63,6 +66,45 @@
<!--Content-->
<td class="content" valign="top" align="left">
<!-- --> <a name="ArrayTrailingComma"></a> <h2>ArrayTrailingComma</h2> <h4>Description</h4>
<p class="body">
Checks if array initialization contains optional trailing comma.
</p>
<pre class="body" >
int[] a = new int[]
{
1,
2,
3,
};
</pre>
<p class="body">
But it allows do not add comma if both left and right curlys
on the same line.
</p>
<pre class="body">
return new int[] { 0 };
</pre>
<p class="body">
Rationale: Putting this comma in make is easier to change the
order of the elements or add new elements on the end.
</p>
<h4>Examples</h4>
<p class="body">
To configure the check:
</p>
<pre class="body">
&lt;module name=&quot;ArrayTrailingComma&quot;/&gt;
</pre>
<h4>Package</h4>
<p class="body">
com.puppycrawl.tools.checkstyle.checks
</p>
<h4>Parent Module</h4>
<p class="body">
<a href="config.html#treewalker">TreeWalker</a>
</p>
<!-- --> <a name="AvoidInlineConditionals"></a> <h2>AvoidInlineConditionals</h2> <h4>Description</h4>
<p class="body">
Detects inline conditionals.

View File

@ -58,6 +58,9 @@
738205, 738388). Example listeners perform verbose and Commons Logging
audits (request 737584).</li>
<li class="body">Added check for trailing comma in array
initialization. (module ArrayTrailingComma, request
696301)</li>
</ul>
<p class="body">

View File

@ -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;
/**
* <p>
* Checks if array initialization contains optional trailing comma.
* </p>
* <p>
* Rationale: Putting this comma in make is easier to change the
* order of the elements or add new elements on the end.
* </p>
* <p>
* An example of how to configure the check is:
* </p>
* <pre>
* &lt;module name="ArrayTrailingComma"/&gt;
* </pre>
* @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");
}
}
}

View File

@ -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.

View File

@ -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[] {
};
}

View File

@ -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));

View File

@ -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);
}
}