001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import com.puppycrawl.tools.checkstyle.StatelessCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * <p>
029 * Checks the style of array type definitions.
030 * Some like Java style: {@code public static void main(String[] args)}
031 * and some like C style: {@code public static void main(String args[])}.
032 * </p>
033 * <p>
034 * By default, the Check enforces Java style.
035 * </p>
036 * <p>
037 * This check strictly enforces only Java style for method return types regardless
038 * of the value for 'javaStyle'. For example, {@code byte[] getData()}.
039 * This is because C doesn't compile methods with array declarations on the name.
040 * </p>
041 * <ul>
042 * <li>
043 * Property {@code javaStyle} - Control whether to enforce Java style (true) or C style (false).
044 * Type is {@code boolean}.
045 * Default value is {@code true}.
046 * </li>
047 * </ul>
048 * <p>
049 * To configure the check to enforce Java style:
050 * </p>
051 * <pre>
052 * &lt;module name="ArrayTypeStyle"/&gt;
053 * </pre>
054 * <p>
055 * Example:
056 * </p>
057 * <pre>
058 * public class MyClass {
059 *   int[] nums; // OK
060 *   String strings[]; // violation
061 *
062 *   char[] toCharArray() { // OK
063 *     return null;
064 *   }
065 *
066 *   byte getData()[] { // violation
067 *     return null;
068 *   }
069 * }
070 * </pre>
071 * <p>
072 * To configure the check to enforce C style:
073 * </p>
074 * <pre>
075 * &lt;module name="ArrayTypeStyle"&gt;
076 *   &lt;property name="javaStyle" value="false"/&gt;
077 * &lt;/module&gt;
078 * </pre>
079 * <p>
080 * Example:
081 * </p>
082 * <pre>
083 * public class MyClass {
084 *   int[] nums; // violation
085 *   String strings[]; // OK
086 *
087 *   char[] toCharArray() { // OK
088 *     return null;
089 *   }
090 *
091 *   byte getData()[] { // violation
092 *     return null;
093 *   }
094 * }
095 * </pre>
096 * <p>
097 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
098 * </p>
099 * <p>
100 * Violation Message Keys:
101 * </p>
102 * <ul>
103 * <li>
104 * {@code array.type.style}
105 * </li>
106 * </ul>
107 *
108 * @since 3.1
109 */
110@StatelessCheck
111public class ArrayTypeStyleCheck extends AbstractCheck {
112
113    /**
114     * A key is pointing to the warning message text in "messages.properties"
115     * file.
116     */
117    public static final String MSG_KEY = "array.type.style";
118
119    /** Control whether to enforce Java style (true) or C style (false). */
120    private boolean javaStyle = true;
121
122    @Override
123    public int[] getDefaultTokens() {
124        return getRequiredTokens();
125    }
126
127    @Override
128    public int[] getAcceptableTokens() {
129        return getRequiredTokens();
130    }
131
132    @Override
133    public int[] getRequiredTokens() {
134        return new int[] {TokenTypes.ARRAY_DECLARATOR};
135    }
136
137    @Override
138    public void visitToken(DetailAST ast) {
139        final DetailAST typeAST = ast.getParent();
140        final DetailAST identAst = typeAST.getNextSibling();
141        // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]'
142        if (identAst != null) {
143            final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
144            final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo()
145                || identAst.getColumnNo() - ast.getColumnNo() > -1;
146
147            // force all methods to be Java style (see note in top Javadoc)
148            final boolean isMethodViolation = isMethod && !isJavaStyle;
149            final boolean isVariableViolation = !isMethod
150                    && isJavaStyle != javaStyle;
151
152            if (isMethodViolation || isVariableViolation) {
153                log(ast, MSG_KEY);
154            }
155        }
156    }
157
158    /**
159     * Setter to control whether to enforce Java style (true) or C style (false).
160     *
161     * @param javaStyle true if Java style should be used.
162     */
163    public void setJavaStyle(boolean javaStyle) {
164        this.javaStyle = javaStyle;
165    }
166
167}