001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 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 * To configure the check:
051 * </p>
052 * <pre>
053 * &lt;module name=&quot;NoArrayTrailingComma&quot;/&gt;
054 * </pre>
055 * <p>
056 * Which results in the following violations:
057 * </p>
058 * <pre>
059 * String[] foo1 = {
060 *   "FOO", // OK
061 *   "BAR", // violation
062 * };
063 * String[] foo2 = { "FOO", "BAR", }; // violation
064 * String[] foo3 = {
065 *   "FOO", // OK
066 *   "BAR" // OK
067 * };
068 * String[] foo4 = { "FOO", "BAR" }; // OK
069 * </pre>
070 * <p>
071 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
072 * </p>
073 * <p>
074 * Violation Message Keys:
075 * </p>
076 * <ul>
077 * <li>
078 * {@code no.array.trailing.comma}
079 * </li>
080 * </ul>
081 *
082 * @since 8.28
083 */
084@StatelessCheck
085public class NoArrayTrailingCommaCheck extends AbstractCheck {
086
087    /**
088     * A key is pointing to the warning message text in "messages.properties"
089     * file.
090     */
091    public static final String MSG_KEY = "no.array.trailing.comma";
092
093    @Override
094    public int[] getDefaultTokens() {
095        return getRequiredTokens();
096    }
097
098    @Override
099    public int[] getAcceptableTokens() {
100        return getRequiredTokens();
101    }
102
103    @Override
104    public int[] getRequiredTokens() {
105        return new int[] {TokenTypes.ARRAY_INIT};
106    }
107
108    @Override
109    public void visitToken(DetailAST arrayInit) {
110        final DetailAST rcurly = arrayInit.findFirstToken(TokenTypes.RCURLY);
111        final DetailAST previousSibling = rcurly.getPreviousSibling();
112        if (previousSibling != null && previousSibling.getType() == TokenTypes.COMMA) {
113            log(previousSibling, MSG_KEY);
114        }
115    }
116}