001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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 *
071 * @since 8.28
072 */
073@StatelessCheck
074public class NoArrayTrailingCommaCheck extends AbstractCheck {
075
076    /**
077     * A key is pointing to the warning message text in "messages.properties"
078     * file.
079     */
080    public static final String MSG_KEY = "no.array.trailing.comma";
081
082    @Override
083    public int[] getDefaultTokens() {
084        return getRequiredTokens();
085    }
086
087    @Override
088    public int[] getAcceptableTokens() {
089        return getRequiredTokens();
090    }
091
092    @Override
093    public int[] getRequiredTokens() {
094        return new int[] {TokenTypes.ARRAY_INIT};
095    }
096
097    @Override
098    public void visitToken(DetailAST arrayInit) {
099        final DetailAST rcurly = arrayInit.findFirstToken(TokenTypes.RCURLY);
100        final DetailAST previousSibling = rcurly.getPreviousSibling();
101        if (previousSibling != null && previousSibling.getType() == TokenTypes.COMMA) {
102            log(previousSibling, MSG_KEY);
103        }
104    }
105}