Skip to content

Commit

Permalink
Issue checkstyle#4425: fixed IllegalType for array
Browse files Browse the repository at this point in the history
  • Loading branch information
sergileon committed Sep 12, 2017
1 parent 12f412b commit 95656fe
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 8 deletions.
49 changes: 43 additions & 6 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/FullIdent.java
Expand Up @@ -55,9 +55,17 @@ private FullIdent() {
* @return a {@code FullIdent} value
*/
public static FullIdent createFullIdent(DetailAST ast) {
final FullIdent ident = new FullIdent();
extractFullIdent(ident, ast);
return ident;
return createFullIdentMaybeArrayDeclare(ast, false);
}

/**
* Creates a new FullIdent starting from the specified node.
* FullIdent will include array, if array contained.
* @param ast the node to start from
* @return a {@code FullIdent} value
*/
public static FullIdent createFullIdentWithArrayDeclare(DetailAST ast) {
return createFullIdentMaybeArrayDeclare(ast, true);
}

/**
Expand Down Expand Up @@ -98,19 +106,48 @@ public String toString() {
return String.join("", elements) + "[" + lineNo + "x" + columnNo + "]";
}

/**
* Creates a new FullIdent starting from the child of the specified node.
* FullIdent can include array.
* @param ast the parent node from where to start from
* @param isArrayDeclare the FullIdent will be include array
* @return a {@code FullIdent} value
*/
private static FullIdent createFullIdentMaybeArrayDeclare(DetailAST ast,
boolean isArrayDeclare) {
final FullIdent ident = new FullIdent();
extractFullIdent(ident, ast, isArrayDeclare);
return ident;
}

/**
* Recursively extract a FullIdent.
*
* @param full the FullIdent to add to
* @param ast the node to recurse from
* @param isArrayDeclare the FullIdent will be include array
*/
private static void extractFullIdent(FullIdent full, DetailAST ast) {
private static void extractFullIdent(FullIdent full, DetailAST ast,
boolean isArrayDeclare) {
if (ast != null) {
if (ast.getType() == TokenTypes.DOT) {
extractFullIdent(full, ast.getFirstChild());
extractFullIdent(full, ast.getFirstChild(), isArrayDeclare);
full.append(".");
extractFullIdent(
full, ast.getFirstChild().getNextSibling());
full, ast.getFirstChild().getNextSibling(), isArrayDeclare);
}
else if (isArrayDeclare && ast.getParent() != null
&& ast.getParent().getType() == TokenTypes.ARRAY_DECLARATOR) {
if (ast.getType() == TokenTypes.ARRAY_DECLARATOR) {
extractFullIdent(full, ast.getParent(), true);
full.append(ast.getNextSibling());
full.append(ast);
}
else {
full.append(ast);
extractFullIdent(full, ast.getParent(), true);
full.append(ast.getNextSibling());
}
}
else {
full.append(ast);
Expand Down
Expand Up @@ -329,7 +329,7 @@ private static boolean isStarImport(DetailAST importAst) {
*/
private void checkClassName(DetailAST ast) {
final DetailAST type = ast.findFirstToken(TokenTypes.TYPE);
final FullIdent ident = CheckUtils.createFullType(type);
final FullIdent ident = CheckUtils.createFullTypeWithArrayDeclare(type);

if (isMatchingClassName(ident.getText())) {
log(ident.getLineNo(), ident.getColumnNo(),
Expand Down
Expand Up @@ -90,6 +90,21 @@ public static FullIdent createFullType(DetailAST typeAST) {
return fullType;
}

/**
* Creates {@code FullIdent} for given type node with array if it contained.
* @param typeAST a type node.
* @return {@code FullIdent} for given type, with array if it contained.
*/
public static FullIdent createFullTypeWithArrayDeclare(final DetailAST typeAST) {
DetailAST ast = typeAST;

while (ast.findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null) {
ast = ast.findFirstToken(TokenTypes.ARRAY_DECLARATOR);
}
return FullIdent.createFullIdentWithArrayDeclare(ast.getFirstChild());

}

/**
* Tests whether a method definition AST defines an equals covariant.
* @param ast the method definition AST to test.
Expand Down
Expand Up @@ -48,6 +48,57 @@ public void testNonValidCoordinatesWithZero() {
Assert.assertEquals("Invalid full indent", "MyTest.MyTestik[15x14]", fullIdent.toString());
}

@Test
public void testWithArrayCreateFullIdentWithArrayDeclare() {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.ARRAY_DECLARATOR);
ast.setColumnNo(18);
ast.setLineNo(15);
ast.setText("[");

final DetailAST ast1 = new DetailAST();
ast1.setType(TokenTypes.LITERAL_INT);
ast1.setColumnNo(14);
ast1.setLineNo(15);
ast1.setText("int");

final DetailAST ast2 = new DetailAST();
ast2.setType(TokenTypes.RBRACK);
ast2.setText("]");

final DetailAST ast3 = new DetailAST();
ast3.setType(TokenTypes.ARRAY_DECLARATOR);
ast3.setText("[");

final DetailAST ast4 = new DetailAST();
ast4.setType(TokenTypes.RBRACK);
ast4.setText("]");

final DetailAST ast5 = new DetailAST();
ast5.setType(TokenTypes.TYPE);
ast5.setText("TYPE");

ast5.addChild(ast3);
ast.addChild(ast1);
ast3.addChild(ast);
ast1.setNextSibling(ast2);
ast.setNextSibling(ast4);
final FullIdent indent = FullIdent.createFullIdentWithArrayDeclare(ast1);
Assert.assertEquals("Invalid full indent", "int[][][15x14]", indent.toString());
}

@Test
public void testWithOutArrayCreateFullIdentWithArrayDeclare() {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.LITERAL_INT);
ast.setColumnNo(14);
ast.setLineNo(15);
ast.setText("int");

final FullIdent indent = FullIdent.createFullIdentWithArrayDeclare(ast);
Assert.assertEquals("Invalid full indent", "int[15x14]", indent.toString());
}

private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.DOT);
Expand Down
Expand Up @@ -135,14 +135,18 @@ public void testSameFileNameFalsePositive() throws Exception {
@Test
public void testSameFileNameGeneral() throws Exception {
checkConfig.addAttribute("illegalClassNames",
"List, InputIllegalTypeGregorianCalendar, java.io.File, ArrayList");
"List, InputIllegalTypeGregorianCalendar, "
+ "java.io.File, ArrayList, Boolean, Integer[], int[][]");
final String[] expected = {
"10:5: " + getCheckMessage(MSG_KEY, "InputIllegalTypeGregorianCalendar"),
"16:23: " + getCheckMessage(MSG_KEY, "InputIllegalTypeGregorianCalendar"),
"24:9: " + getCheckMessage(MSG_KEY, "List"),
"25:9: " + getCheckMessage(MSG_KEY, "java.io.File"),
"27:5: " + getCheckMessage(MSG_KEY, "java.util.List"),
"28:13: " + getCheckMessage(MSG_KEY, "ArrayList"),
"29:13: " + getCheckMessage(MSG_KEY, "Boolean"),
"30:13: " + getCheckMessage(MSG_KEY, "Integer[]"),
"31:13: " + getCheckMessage(MSG_KEY, "int[][]"),
};
verify(checkConfig, getPath("InputIllegalTypeSameFileName.java"), expected);
}
Expand Down
Expand Up @@ -350,6 +350,15 @@ public void testParseClassNames() {
assertEquals("Result is not expected", expected, result);
}

@Test
public void testCreateFullTypeWithArrayDeclare() throws Exception {
final DetailAST arrayTypeNode = getNodeFromFile(TokenTypes.VARIABLE_DEF)
.getNextSibling().getFirstChild().getNextSibling();

assertEquals("Invalid full type", "int[][14x14]",
CheckUtils.createFullTypeWithArrayDeclare(arrayTypeNode).toString());
}

private DetailAST getNodeFromFile(int type) throws Exception {
return getNode(parseFile(new File(getPath("InputCheckUtilsTest.java"))), type);
}
Expand Down
Expand Up @@ -26,4 +26,7 @@ private void foo() {
}
java.util.List<Integer> list = new ArrayList<>(); //WARNING
private ArrayList<String> values;
private Boolean x;// WARNING Boolean
private Integer[] x1;// WARNING Integer[]
private int[][] x2;// WARNING int[][]
}

0 comments on commit 95656fe

Please sign in to comment.