Skip to content

Commit

Permalink
Issue checkstyle#6301: ArrayTypeStyle support method definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
esilkensen committed Feb 16, 2019
1 parent 4161929 commit 93ee62e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Expand Up @@ -41,8 +41,9 @@ 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:16: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
"47:19: " + getCheckMessage(ArrayTypeStyleCheck.class, MSG_KEY),
};

final Configuration checkConfig = getModuleConfig("ArrayTypeStyle");
Expand Down
Expand Up @@ -29,14 +29,19 @@ 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;
}
}
int[] array[] = new int [2][2]; //warn
int array2[][][] = new int[3][3][3]; //warn
Expand Down
Expand Up @@ -61,17 +61,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) {
// force all methods to be Java style (C functions can't return arrays)
final boolean isMethodViolation = isMethod && !isJavaStyle;
final boolean isVariableViolation = !isMethod && isJavaStyle != javaStyle;

if (isMethodViolation || isVariableViolation) {
log(ast, MSG_KEY);
}
}
Expand Down
Expand Up @@ -54,6 +54,7 @@ public void testJavaStyleOn()
"14:23: " + getCheckMessage(MSG_KEY),
"15:18: " + getCheckMessage(MSG_KEY),
"21:44: " + getCheckMessage(MSG_KEY),
"45:33: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected);
}
Expand All @@ -69,6 +70,7 @@ public void testJavaStyleOff()
"17:39: " + getCheckMessage(MSG_KEY),
"23:18: " + getCheckMessage(MSG_KEY),
"31:20: " + getCheckMessage(MSG_KEY),
"45:33: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputArrayTypeStyle.java"), expected);
}
Expand Down
Expand Up @@ -33,14 +33,19 @@ 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 static void foo(java.util.Collection < ? extends InputArrayTypeStyle[] > collection) {}
}

0 comments on commit 93ee62e

Please sign in to comment.