|
27 | 27 | /**
|
28 | 28 | * Checks the style of array type definitions.
|
29 | 29 | * 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[])}. |
31 | 31 | *
|
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> |
33 | 37 | */
|
34 | 38 | @StatelessCheck
|
35 | 39 | public class ArrayTypeStyleCheck extends AbstractCheck {
|
@@ -61,17 +65,18 @@ public int[] getRequiredTokens() {
|
61 | 65 | @Override
|
62 | 66 | public void visitToken(DetailAST ast) {
|
63 | 67 | 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) { |
68 | 69 | final DetailAST variableAST = typeAST.getNextSibling();
|
69 | 70 | 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() |
72 | 73 | || variableAST.getColumnNo() - ast.getColumnNo() > -1;
|
73 | 74 |
|
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) { |
75 | 80 | log(ast, MSG_KEY);
|
76 | 81 | }
|
77 | 82 | }
|
|
0 commit comments