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