View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <pre>
36   * enum Foo1 {
37   *   FOO,
38   *   BAR;
39   * }
40   * </pre>
41   *
42   * <p>
43   *  The check demands that there should not be any comma after last constant in
44   *  enum definition.
45   * </p>
46   * <pre>
47   * enum Foo1 {
48   *   FOO,
49   *   BAR, //violation
50   * }
51   * </pre>
52   *
53   * <p>
54   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
55   * </p>
56   *
57   * <p>
58   * Violation Message Keys:
59   * </p>
60   * <ul>
61   * <li>
62   * {@code no.enum.trailing.comma}
63   * </li>
64   * </ul>
65   *
66   * @since 8.29
67   */
68  @StatelessCheck
69  public class NoEnumTrailingCommaCheck extends AbstractCheck {
70  
71      /**
72       * A key is pointing to the warning message text in "messages.properties"
73       * file.
74       */
75      public static final String MSG_KEY = "no.enum.trailing.comma";
76  
77      @Override
78      public int[] getDefaultTokens() {
79          return getRequiredTokens();
80      }
81  
82      @Override
83      public int[] getAcceptableTokens() {
84          return getRequiredTokens();
85      }
86  
87      @Override
88      public int[] getRequiredTokens() {
89          return new int[] {TokenTypes.ENUM_DEF};
90      }
91  
92      @Override
93      public void visitToken(DetailAST detailAST) {
94          final DetailAST enumBlock = detailAST.findFirstToken(TokenTypes.OBJBLOCK);
95          TokenUtil.findFirstTokenByPredicate(enumBlock,
96              node -> TokenUtil.isOfType(node, TokenTypes.SEMI, TokenTypes.RCURLY))
97              .map(DetailAST::getPreviousSibling)
98              .filter(token -> token.getType() == TokenTypes.COMMA)
99              .ifPresent(comma -> log(comma, MSG_KEY));
100     }
101 }