1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26
27 /**
28 * <p>
29 * Checks the style of array type definitions.
30 * Some like Java style: {@code public static void main(String[] args)}
31 * and some like C style: {@code public static void main(String args[])}.
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 regardless
38 * of the value for 'javaStyle'. For example, {@code byte[] getData()}.
39 * This is because C doesn't compile methods with array declarations on the name.
40 * </p>
41 * <ul>
42 * <li>
43 * Property {@code javaStyle} - Control whether to enforce Java style (true) or C style (false).
44 * Type is {@code boolean}.
45 * Default value is {@code true}.
46 * </li>
47 * </ul>
48 * <p>
49 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
50 * </p>
51 * <p>
52 * Violation Message Keys:
53 * </p>
54 * <ul>
55 * <li>
56 * {@code array.type.style}
57 * </li>
58 * </ul>
59 *
60 * @since 3.1
61 */
62 @StatelessCheck
63 public class ArrayTypeStyleCheck extends AbstractCheck {
64
65 /**
66 * A key is pointing to the warning message text in "messages.properties"
67 * file.
68 */
69 public static final String MSG_KEY = "array.type.style";
70
71 /** Control whether to enforce Java style (true) or C style (false). */
72 private boolean javaStyle = true;
73
74 @Override
75 public int[] getDefaultTokens() {
76 return getRequiredTokens();
77 }
78
79 @Override
80 public int[] getAcceptableTokens() {
81 return getRequiredTokens();
82 }
83
84 @Override
85 public int[] getRequiredTokens() {
86 return new int[] {TokenTypes.ARRAY_DECLARATOR};
87 }
88
89 @Override
90 public void visitToken(DetailAST ast) {
91 final DetailAST typeAST = ast.getParent();
92 final DetailAST identAst = typeAST.getNextSibling();
93 // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]'
94 if (identAst != null) {
95 final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
96 final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo()
97 || identAst.getColumnNo() - ast.getColumnNo() > -1;
98
99 // force all methods to be Java style (see note in top Javadoc)
100 final boolean isMethodViolation = isMethod && !isJavaStyle;
101 final boolean isVariableViolation = !isMethod
102 && isJavaStyle != javaStyle;
103
104 if (isMethodViolation || isVariableViolation) {
105 log(ast, MSG_KEY);
106 }
107 }
108 }
109
110 /**
111 * Setter to control whether to enforce Java style (true) or C style (false).
112 *
113 * @param javaStyle true if Java style should be used.
114 * @since 3.1
115 */
116 public void setJavaStyle(boolean javaStyle) {
117 this.javaStyle = javaStyle;
118 }
119
120 }