001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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;
026import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
027
028/**
029 * <p>
030 *  Checks that enum definition does not contain a trailing comma.
031 *  Rationale: JLS allows trailing commas in arrays and enumerations, but does not allow
032 *  them in other locations. To unify the coding style, the use of trailing commas should
033 *  be prohibited.
034 * </p>
035 * <pre>
036 * enum Foo1 {
037 *   FOO,
038 *   BAR;
039 * }
040 * </pre>
041 * <p>
042 *  The check demands that there should not be any comma after last constant in
043 *  enum definition.
044 * </p>
045 * <pre>
046 * enum Foo1 {
047 *   FOO,
048 *   BAR, //violation
049 * }
050 * </pre>
051 * <p>
052 * To configure the check:
053 * </p>
054 * <pre>
055 * &lt;module name=&quot;NoEnumTrailingComma&quot;/&gt;
056 * </pre>
057 * <p>
058 * Which results in the following violations:
059 * </p>
060 * <pre>
061 * enum Foo1 {
062 *   FOO,
063 *   BAR; //OK
064 * }
065 * enum Foo2 {
066 *   FOO,
067 *   BAR //OK
068 * }
069 * enum Foo3 {
070 *   FOO,
071 *   BAR, //violation
072 * }
073 * enum Foo4 {
074 *   FOO,
075 *   BAR, // violation
076 *   ;
077 * }
078 * enum Foo5 {
079 *   FOO,
080 *   BAR,; // violation
081 * }
082 * enum Foo6 { FOO, BAR,; } // violation
083 * enum Foo7 { FOO, BAR, } // violation
084 * enum Foo8 {
085 *   FOO,
086 *   BAR // OK
087 *   ;
088 * }
089 * enum Foo9 { FOO, BAR; } // OK
090 * enum Foo10 { FOO, BAR } // OK
091 * </pre>
092 * <p>
093 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
094 * </p>
095 * <p>
096 * Violation Message Keys:
097 * </p>
098 * <ul>
099 * <li>
100 * {@code no.enum.trailing.comma}
101 * </li>
102 * </ul>
103 *
104 * @since 8.29
105 */
106@StatelessCheck
107public class NoEnumTrailingCommaCheck extends AbstractCheck {
108
109    /**
110     * A key is pointing to the warning message text in "messages.properties"
111     * file.
112     */
113    public static final String MSG_KEY = "no.enum.trailing.comma";
114
115    @Override
116    public int[] getDefaultTokens() {
117        return getRequiredTokens();
118    }
119
120    @Override
121    public int[] getAcceptableTokens() {
122        return getRequiredTokens();
123    }
124
125    @Override
126    public int[] getRequiredTokens() {
127        return new int[] {TokenTypes.ENUM_DEF};
128    }
129
130    @Override
131    public void visitToken(DetailAST detailAST) {
132        final DetailAST enumBlock = detailAST.findFirstToken(TokenTypes.OBJBLOCK);
133        TokenUtil.findFirstTokenByPredicate(enumBlock,
134            node -> TokenUtil.isOfType(node, TokenTypes.SEMI, TokenTypes.RCURLY))
135            .map(DetailAST::getPreviousSibling)
136            .filter(token -> token.getType() == TokenTypes.COMMA)
137            .ifPresent(comma -> log(comma, MSG_KEY));
138    }
139}