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.coding;
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 that array initialization do not contain a trailing comma.
030 * Rationale: JLS allows trailing commas in arrays and enumerations, but does not allow
031 * them in other locations. To unify the coding style, the use of trailing commas should
032 * be prohibited.
033 * </p>
034 * <pre>
035 * int[] foo = new int[] {
036 *   1,
037 *   2
038 * };
039 * </pre>
040 * <p>
041 * The check demands that there should not be any comma after the last element of an array.
042 * </p>
043 * <pre>
044 * String[] foo = new String[] {
045 *   "FOO",
046 *   "BAR", //violation
047 * }
048 * </pre>
049 * <p>
050 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
051 * </p>
052 * <p>
053 * Violation Message Keys:
054 * </p>
055 * <ul>
056 * <li>
057 * {@code no.array.trailing.comma}
058 * </li>
059 * </ul>
060 *
061 * @since 8.28
062 */
063@StatelessCheck
064public class NoArrayTrailingCommaCheck extends AbstractCheck {
065
066    /**
067     * A key is pointing to the warning message text in "messages.properties"
068     * file.
069     */
070    public static final String MSG_KEY = "no.array.trailing.comma";
071
072    @Override
073    public int[] getDefaultTokens() {
074        return getRequiredTokens();
075    }
076
077    @Override
078    public int[] getAcceptableTokens() {
079        return getRequiredTokens();
080    }
081
082    @Override
083    public int[] getRequiredTokens() {
084        return new int[] {TokenTypes.ARRAY_INIT};
085    }
086
087    @Override
088    public void visitToken(DetailAST arrayInit) {
089        final DetailAST rcurly = arrayInit.findFirstToken(TokenTypes.RCURLY);
090        final DetailAST previousSibling = rcurly.getPreviousSibling();
091        if (previousSibling != null && previousSibling.getType() == TokenTypes.COMMA) {
092            log(previousSibling, MSG_KEY);
093        }
094    }
095}