Skip to content

Commit 404143c

Browse files
esilkensenrnveach
authored andcommitted
Issue #6301: ArrayTypeStyle support for method definitions
1 parent 96483bd commit 404143c

File tree

6 files changed

+111
-14
lines changed

6 files changed

+111
-14
lines changed

src/it/java/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/ArrayTypeStyleTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public void testArrayTypeStyle() throws Exception {
4141
"15:44: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
4242
"21:20: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
4343
"22:23: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
44-
"41:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
45-
"42:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
44+
"41:33: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
45+
"46:36: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
46+
"52:27: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
47+
"62:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
48+
"63:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
4649
};
4750

4851
final Configuration checkConfig = getModuleConfig("ArrayTypeStyle");

src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4832nocstylearray/InputArrayTypeStyle.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,35 @@ public class Test
2929

3030
public Test[]
3131
getTests()
32-
{ // we shouldn't check methods because there is no alternatives.
32+
{
3333
return null;
3434
}
3535

3636
public Test[] getNewTest() //ok
3737
{
3838
return null;
3939
}
40+
41+
public Test getOldTest()[] //warn
42+
{
43+
return null;
44+
}
45+
46+
public Test getOldTests()[][] //warn
47+
{
48+
return null;
49+
}
50+
51+
public Test[]
52+
getMoreTests()[] //warn
53+
{
54+
return null;
55+
}
56+
57+
public Test[][] getTests2() //ok
58+
{
59+
return null;
60+
}
4061
}
4162
int[] array[] = new int [2][2]; //warn
4263
int array2[][][] = new int[3][3][3]; //warn

src/main/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheck.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
/**
2828
* Checks the style of array type definitions.
2929
* Some like Java-style: {@code public static void main(String[] args)}
30-
* and some like C-style: public static void main(String args[])
30+
* and some like C-style: {@code public static void main(String args[])}.
3131
*
32-
* <p>By default the Check enforces Java style.
32+
* <p>By default the Check enforces Java style.</p>
33+
*
34+
* <p>This check strictly enforces only Java style for method return types
35+
* regardless of the value for 'javaStyle'. For example, {@code byte[] getData()}.
36+
* This is because C doesn't compile methods with array declarations on the name.</p>
3337
*/
3438
@StatelessCheck
3539
public class ArrayTypeStyleCheck extends AbstractCheck {
@@ -61,17 +65,18 @@ public int[] getRequiredTokens() {
6165
@Override
6266
public void visitToken(DetailAST ast) {
6367
final DetailAST typeAST = ast.getParent();
64-
if (typeAST.getType() == TokenTypes.TYPE
65-
// Do not check method's return type.
66-
// We have no alternatives here.
67-
&& typeAST.getParent().getType() != TokenTypes.METHOD_DEF) {
68+
if (typeAST.getType() == TokenTypes.TYPE) {
6869
final DetailAST variableAST = typeAST.getNextSibling();
6970
if (variableAST != null) {
70-
final boolean isJavaStyle =
71-
variableAST.getLineNo() > ast.getLineNo()
71+
final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
72+
final boolean isJavaStyle = variableAST.getLineNo() > ast.getLineNo()
7273
|| variableAST.getColumnNo() - ast.getColumnNo() > -1;
7374

74-
if (isJavaStyle != javaStyle) {
75+
// force all methods to be Java style (see note in top Javadoc)
76+
final boolean isMethodViolation = isMethod && !isJavaStyle;
77+
final boolean isVariableViolation = !isMethod && isJavaStyle != javaStyle;
78+
79+
if (isMethodViolation || isVariableViolation) {
7580
log(ast, MSG_KEY);
7681
}
7782
}

src/test/java/com/puppycrawl/tools/checkstyle/checks/ArrayTypeStyleCheckTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public void testJavaStyleOn()
5454
"14:23: " + getCheckMessage(MSG_KEY),
5555
"15:18: " + getCheckMessage(MSG_KEY),
5656
"21:44: " + getCheckMessage(MSG_KEY),
57+
"45:33: " + getCheckMessage(MSG_KEY),
58+
"50:36: " + getCheckMessage(MSG_KEY),
59+
"56:29: " + getCheckMessage(MSG_KEY),
5760
};
5861
verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected);
5962
}
@@ -69,6 +72,9 @@ public void testJavaStyleOff()
6972
"17:39: " + getCheckMessage(MSG_KEY),
7073
"23:18: " + getCheckMessage(MSG_KEY),
7174
"31:20: " + getCheckMessage(MSG_KEY),
75+
"45:33: " + getCheckMessage(MSG_KEY),
76+
"50:36: " + getCheckMessage(MSG_KEY),
77+
"56:29: " + getCheckMessage(MSG_KEY),
7278
};
7379
verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected);
7480
}

src/test/resources/com/puppycrawl/tools/checkstyle/checks/arraytypestyle/InputArrayTypeStyle.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,35 @@ public class Test
3333

3434
public Test[]
3535
getTests()
36-
{ // we shouldn't check methods because there is no alternatives.
36+
{
3737
return null;
3838
}
3939

4040
public Test[] getNewTest()
4141
{
4242
return null;
4343
}
44+
45+
public Test getOldTest()[]
46+
{
47+
return null;
48+
}
49+
50+
public Test getOldTests()[][]
51+
{
52+
return null;
53+
}
54+
55+
public Test[]
56+
getMoreTests()[][]
57+
{
58+
return null;
59+
}
60+
61+
public Test[][] getTests2()
62+
{
63+
return null;
64+
}
4465
}
4566
public static void foo(java.util.Collection < ? extends InputArrayTypeStyle[] > collection) {}
4667
}

src/xdocs/config_misc.xml

+42-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@
2828
<p>
2929
Checks the style of array type definitions. Some like Java style:
3030
<code>public static void main(String[] args)</code> and some like
31-
C style: public static void main(String args[])
31+
C style: <code>public static void main(String args[])</code>.
32+
</p>
33+
<p>
34+
By default the Check enforces Java style.
35+
</p>
36+
<p>
37+
This check strictly enforces only Java style for method return types
38+
regardless of the value for 'javaStyle'. For example, <code>byte[] getData()</code>.
39+
This is because C doesn't compile methods with array declarations on the name.
3240
</p>
3341
</subsection>
3442

@@ -60,7 +68,23 @@
6068
<source>
6169
&lt;module name=&quot;ArrayTypeStyle&quot;/&gt;
6270
</source>
71+
<p>
72+
Example:
73+
</p>
74+
<source>
75+
public class MyClass {
76+
int[] nums; // OK
77+
String strings[]; // violation
78+
79+
char[] toCharArray() { // OK
80+
return null;
81+
}
6382

83+
byte getData()[] { // violation
84+
return null;
85+
}
86+
}
87+
</source>
6488
<p>
6589
To configure the check to enforce C style:
6690
</p>
@@ -69,6 +93,23 @@
6993
&lt;property name=&quot;javaStyle&quot; value=&quot;false&quot;/&gt;
7094
&lt;/module&gt;
7195
</source>
96+
<p>
97+
Example:
98+
</p>
99+
<source>
100+
public class MyClass {
101+
int[] nums; // violation
102+
String strings[]; // OK
103+
104+
char[] toCharArray() { // OK
105+
return null;
106+
}
107+
108+
byte getData()[] { // violation
109+
return null;
110+
}
111+
}
112+
</source>
72113
</subsection>
73114

74115
<subsection name="Example of Usage" id="ArrayTypeStyle_Example_of_Usage">

0 commit comments

Comments
 (0)