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 * <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 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
050 * </p>
051 * <p>
052 * Violation Message Keys:
053 * </p>
054 * <ul>
055 * <li>
056 * {@code array.type.style}
057 * </li>
058 * </ul>
059 *
060 * @since 3.1
061 */
062@StatelessCheck
063public class ArrayTypeStyleCheck extends AbstractCheck {
064
065    /**
066     * A key is pointing to the warning message text in "messages.properties"
067     * file.
068     */
069    public static final String MSG_KEY = "array.type.style";
070
071    /** Control whether to enforce Java style (true) or C style (false). */
072    private boolean javaStyle = true;
073
074    @Override
075    public int[] getDefaultTokens() {
076        return getRequiredTokens();
077    }
078
079    @Override
080    public int[] getAcceptableTokens() {
081        return getRequiredTokens();
082    }
083
084    @Override
085    public int[] getRequiredTokens() {
086        return new int[] {TokenTypes.ARRAY_DECLARATOR};
087    }
088
089    @Override
090    public void visitToken(DetailAST ast) {
091        final DetailAST typeAST = ast.getParent();
092        final DetailAST identAst = typeAST.getNextSibling();
093        // If identAst is null, we have a 'LITERAL_NEW' expression, i.e. 'new int[2][2]'
094        if (identAst != null) {
095            final boolean isMethod = typeAST.getParent().getType() == TokenTypes.METHOD_DEF;
096            final boolean isJavaStyle = identAst.getLineNo() > ast.getLineNo()
097                || identAst.getColumnNo() - ast.getColumnNo() > -1;
098
099            // 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}