001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 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 *
044 * @since 3.1
045 */
046@StatelessCheck
047public class ArrayTypeStyleCheck extends AbstractCheck {
048
049    /**
050     * A key is pointing to the warning message text in "messages.properties"
051     * file.
052     */
053    public static final String MSG_KEY = "array.type.style";
054
055    /** Control whether to enforce Java style (true) or C style (false). */
056    private boolean javaStyle = true;
057
058    @Override
059    public int[] getDefaultTokens() {
060        return getRequiredTokens();
061    }
062
063    @Override
064    public int[] getAcceptableTokens() {
065        return getRequiredTokens();
066    }
067
068    @Override
069    public int[] getRequiredTokens() {
070        return new int[] {TokenTypes.ARRAY_DECLARATOR};
071    }
072
073    @Override
074    public void visitToken(DetailAST ast) {
075        final DetailAST typeAST = ast.getParent();
076        final DetailAST identAst = typeAST.getNextSibling();
077        // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]'
078        if (identAst != null) {
079            final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
080            final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo()
081                || identAst.getColumnNo() - ast.getColumnNo() > -1;
082
083            // force all methods to be Java style (see note in top Javadoc)
084            final boolean isMethodViolation = isMethod && !isJavaStyle;
085            final boolean isVariableViolation = !isMethod
086                    && isJavaStyle != javaStyle;
087
088            if (isMethodViolation || isVariableViolation) {
089                log(ast, MSG_KEY);
090            }
091        }
092    }
093
094    /**
095     * Setter to control whether to enforce Java style (true) or C style (false).
096     *
097     * @param javaStyle true if Java style should be used.
098     * @since 3.1
099     */
100    public void setJavaStyle(boolean javaStyle) {
101        this.javaStyle = javaStyle;
102    }
103
104}