001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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 * <div>
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 * </div>
033 *
034 * <p>
035 * By default, the Check enforces Java style.
036 * </p>
037 *
038 * <p>
039 * This check strictly enforces only Java style for method return types regardless
040 * of the value for 'javaStyle'. For example, {@code byte[] getData()}.
041 * This is because C doesn't compile methods with array declarations on the name.
042 * </p>
043 * <ul>
044 * <li>
045 * Property {@code javaStyle} - Control whether to enforce Java style (true) or C style (false).
046 * Type is {@code boolean}.
047 * Default value is {@code true}.
048 * </li>
049 * </ul>
050 *
051 * <p>
052 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
053 * </p>
054 *
055 * <p>
056 * Violation Message Keys:
057 * </p>
058 * <ul>
059 * <li>
060 * {@code array.type.style}
061 * </li>
062 * </ul>
063 *
064 * @since 3.1
065 */
066@StatelessCheck
067public class ArrayTypeStyleCheck extends AbstractCheck {
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_KEY = "array.type.style";
074
075    /** Control whether to enforce Java style (true) or C style (false). */
076    private boolean javaStyle = true;
077
078    @Override
079    public int[] getDefaultTokens() {
080        return getRequiredTokens();
081    }
082
083    @Override
084    public int[] getAcceptableTokens() {
085        return getRequiredTokens();
086    }
087
088    @Override
089    public int[] getRequiredTokens() {
090        return new int[] {TokenTypes.ARRAY_DECLARATOR};
091    }
092
093    @Override
094    public void visitToken(DetailAST ast) {
095        final DetailAST typeAST = ast.getParent();
096        final DetailAST identAst = typeAST.getNextSibling();
097        // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]'
098        if (identAst != null) {
099            final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
100            final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo()
101                || identAst.getColumnNo() - ast.getColumnNo() > -1;
102
103            // force all methods to be Java style (see note in top Javadoc)
104            final boolean isMethodViolation = isMethod && !isJavaStyle;
105            final boolean isVariableViolation = !isMethod
106                    && isJavaStyle != javaStyle;
107
108            if (isMethodViolation || isVariableViolation) {
109                log(ast, MSG_KEY);
110            }
111        }
112    }
113
114    /**
115     * Setter to control whether to enforce Java style (true) or C style (false).
116     *
117     * @param javaStyle true if Java style should be used.
118     * @since 3.1
119     */
120    public void setJavaStyle(boolean javaStyle) {
121        this.javaStyle = javaStyle;
122    }
123
124}