View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
36   * enum Foo1 {
37   *   FOO,
38   *   BAR;
39   * }
40   * </code></pre></div>
41   *
42   * <p>
43   *  The check demands that there should not be any comma after last constant in
44   *  enum definition.
45   * </p>
46   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
47   * enum Foo1 {
48   *   FOO,
49   *   BAR, // violation
50   * }
51   * </code></pre></div>
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      @Override
65      public int[] getDefaultTokens() {
66          return getRequiredTokens();
67      }
68  
69      @Override
70      public int[] getAcceptableTokens() {
71          return getRequiredTokens();
72      }
73  
74      @Override
75      public int[] getRequiredTokens() {
76          return new int[] {TokenTypes.ENUM_DEF};
77      }
78  
79      @Override
80      public void visitToken(DetailAST detailAST) {
81          final DetailAST enumBlock = detailAST.findFirstToken(TokenTypes.OBJBLOCK);
82          TokenUtil.findFirstTokenByPredicate(enumBlock,
83              node -> TokenUtil.isOfType(node, TokenTypes.SEMI, TokenTypes.RCURLY))
84              .map(DetailAST::getPreviousSibling)
85              .filter(token -> token.getType() == TokenTypes.COMMA)
86              .ifPresent(comma -> log(comma, MSG_KEY));
87      }
88  }