Skip to content

Commit

Permalink
Issue checkstyle#6301: ArrayTypeStyle support for method definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
esilkensen committed Feb 17, 2019
1 parent 4161929 commit 4fae23f
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
Expand Up @@ -41,8 +41,11 @@ public void testArrayTypeStyle() throws Exception {
"15:44: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"21:20: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"22:23: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"41:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"42:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"41:33: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"46:36: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"52:27: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"62:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"63:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
};

final Configuration checkConfig = getModuleConfig("ArrayTypeStyle");
Expand Down
Expand Up @@ -29,14 +29,35 @@ public class Test

public Test[]
getTests()
{ // we shouldn't check methods because there is no alternatives.
{
return null;
}

public Test[] getNewTest() //ok
{
return null;
}

public Test getOldTest()[] //warn
{
return null;
}

public Test getOldTests()[][] //warn
{
return null;
}

public Test[]
getMoreTests()[] //warn
{
return null;
}

public Test[][] getTests2() //ok
{
return null;
}
}
int[] array[] = new int [2][2]; //warn
int array2[][][] = new int[3][3][3]; //warn
Expand Down
Expand Up @@ -27,9 +27,16 @@
/**
* Checks the style of array type definitions.
* Some like Java-style: {@code public static void main(String[] args)}
* and some like C-style: public static void main(String args[])
* and some like C-style: {@code public static void main(String args[])}.
*
* <p>By default the Check enforces Java style.
* <p>By default the Check enforces Java style.</p>
*
* <p>This check also enforces Java style for method return types.</p>
* <pre>
* public byte getData()[] // violation (if javaStyle == true)
*
* public char[] toCharArray() // ok
* </pre>
*/
@StatelessCheck
public class ArrayTypeStyleCheck extends AbstractCheck {
Expand Down Expand Up @@ -61,17 +68,18 @@ public int[] getRequiredTokens() {
@Override
public void visitToken(DetailAST ast) {
final DetailAST typeAST = ast.getParent();
if (typeAST.getType() == TokenTypes.TYPE
// Do not check method's return type.
// We have no alternatives here.
&& typeAST.getParent().getType() != TokenTypes.METHOD_DEF) {
if (typeAST.getType() == TokenTypes.TYPE) {
final DetailAST variableAST = typeAST.getNextSibling();
if (variableAST != null) {
final boolean isJavaStyle =
variableAST.getLineNo() > ast.getLineNo()
final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
final boolean isJavaStyle = variableAST.getLineNo() > ast.getLineNo()
|| variableAST.getColumnNo() - ast.getColumnNo() > -1;

if (isJavaStyle != javaStyle) {
// only check method return types when javaStyle = true
final boolean isMethodViolation = isMethod && !isJavaStyle && javaStyle;
final boolean isVariableViolation = !isMethod && isJavaStyle != javaStyle;

if (isMethodViolation || isVariableViolation) {
log(ast, MSG_KEY);
}
}
Expand Down
Expand Up @@ -54,6 +54,9 @@ public void testJavaStyleOn()
"14:23: " + getCheckMessage(MSG_KEY),
"15:18: " + getCheckMessage(MSG_KEY),
"21:44: " + getCheckMessage(MSG_KEY),
"45:33: " + getCheckMessage(MSG_KEY),
"50:36: " + getCheckMessage(MSG_KEY),
"56:29: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected);
}
Expand Down
Expand Up @@ -33,14 +33,35 @@ public class Test

public Test[]
getTests()
{ // we shouldn't check methods because there is no alternatives.
{
return null;
}

public Test[] getNewTest()
{
return null;
}

public Test getOldTest()[]
{
return null;
}

public Test getOldTests()[][]
{
return null;
}

public Test[]
getMoreTests()[][]
{
return null;
}

public Test[][] getTests2()
{
return null;
}
}
public static void foo(java.util.Collection < ? extends InputArrayTypeStyle[] > collection) {}
}
38 changes: 37 additions & 1 deletion src/xdocs/config_misc.xml
Expand Up @@ -28,7 +28,10 @@
<p>
Checks the style of array type definitions. Some like Java style:
<code>public static void main(String[] args)</code> and some like
C style: public static void main(String args[])
C style: <code>public static void main(String args[])</code>.
</p>
<p>
This check also enforces Java style for method return types.
</p>
</subsection>

Expand Down Expand Up @@ -60,7 +63,23 @@
<source>
&lt;module name=&quot;ArrayTypeStyle&quot;/&gt;
</source>
<p>
Example:
</p>
<source>
public class MyClass {
int[] nums; // OK
String strings[]; // violation

char[] toCharArray() { // OK
return null;
}

byte getData()[] { // violation
return null;
}
}
</source>
<p>
To configure the check to enforce C style:
</p>
Expand All @@ -69,6 +88,23 @@
&lt;property name=&quot;javaStyle&quot; value=&quot;false&quot;/&gt;
&lt;/module&gt;
</source>
<p>
Example:
</p>
<source>
public class MyClass {
int[] nums; // violation
String strings[]; // OK

char[] toCharArray() { // OK (method return types not checked for C style)
return null;
}

byte getData()[] { // OK (method return types not checked for C style)
return null;
}
}
</source>
</subsection>

<subsection name="Example of Usage" id="ArrayTypeStyle_Example_of_Usage">
Expand Down

0 comments on commit 4fae23f

Please sign in to comment.