1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
27
28 /**
29 * <div>
30 * Checks that enum definition does not contain a trailing comma.
31 * Rationale: JLS allows trailing commas in arrays and enumerations, but does not allow
32 * them in other locations. To unify the coding style, the use of trailing commas should
33 * be prohibited.
34 * </div>
35 * {@snippet lang="text" :
36 * enum Foo1 {
37 * FOO,
38 * BAR;
39 * }
40 * }
41 *
42 * <p>
43 * The check demands that there should not be any comma after last constant in
44 * enum definition.
45 * </p>
46 * {@snippet lang="text" :
47 * enum Foo1 {
48 * FOO,
49 * BAR, // violation
50 * }
51 * }
52 *
53 * @since 8.29
54 */
55 @StatelessCheck
56 public class NoEnumTrailingCommaCheck extends AbstractCheck {
57
58 /**
59 * A key is pointing to the warning message text in "messages.properties"
60 * file.
61 */
62 public static final String MSG_KEY = "no.enum.trailing.comma";
63
64 /**
65 * Creates a new {@code NoEnumTrailingCommaCheck} instance.
66 */
67 public NoEnumTrailingCommaCheck() {
68 // no code by default
69 }
70
71 @Override
72 public int[] getDefaultTokens() {
73 return getRequiredTokens();
74 }
75
76 @Override
77 public int[] getAcceptableTokens() {
78 return getRequiredTokens();
79 }
80
81 @Override
82 public int[] getRequiredTokens() {
83 return new int[] {TokenTypes.ENUM_DEF};
84 }
85
86 @Override
87 public void visitToken(DetailAST detailAST) {
88 final DetailAST enumBlock = detailAST.findFirstToken(TokenTypes.OBJBLOCK);
89 TokenUtil.findFirstTokenByPredicate(enumBlock,
90 node -> TokenUtil.isOfType(node, TokenTypes.SEMI, TokenTypes.RCURLY))
91 .map(DetailAST::getPreviousSibling)
92 .filter(token -> token.getType() == TokenTypes.COMMA)
93 .ifPresent(comma -> log(comma, MSG_KEY));
94 }
95
96 }