View Javadoc
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  
27  /**
28   * <div>
29   * Checks if unnecessary semicolon is in enum definitions.
30   * Semicolon is not needed if enum body contains only enum constants.
31   * </div>
32   *
33   * @since 8.22
34   */
35  @StatelessCheck
36  public final class UnnecessarySemicolonInEnumerationCheck extends AbstractCheck {
37  
38      /**
39       * A key is pointing to the warning message text in "messages.properties"
40       * file.
41       */
42      public static final String MSG_SEMI = "unnecessary.semicolon";
43  
44      @Override
45      public int[] getDefaultTokens() {
46          return getRequiredTokens();
47      }
48  
49      @Override
50      public int[] getAcceptableTokens() {
51          return getRequiredTokens();
52      }
53  
54      @Override
55      public int[] getRequiredTokens() {
56          return new int[] {
57              TokenTypes.ENUM_DEF,
58          };
59      }
60  
61      @Override
62      public void visitToken(DetailAST ast) {
63          final DetailAST enumBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
64          final boolean hasMembers = hasMembersAfterConstants(enumBlock);
65          DetailAST sibling = enumBlock.getFirstChild();
66          while (sibling != null) {
67              if (sibling.getType() == TokenTypes.SEMI
68                      && (!hasMembers || isPrecededBySemi(sibling))) {
69                  log(sibling, MSG_SEMI);
70              }
71              sibling = sibling.getNextSibling();
72          }
73      }
74  
75      /**
76       * Checks if enum body contains any member (method, constructor, field, etc.)
77       * in addition to enum constants and semicolons.
78       *
79       * @param enumBlock the enum body to check
80       * @return true if enum body has members, false if it only has constants and semicolons.
81       */
82      private static boolean hasMembersAfterConstants(DetailAST enumBlock) {
83          boolean result = false;
84          DetailAST sibling = enumBlock.getFirstChild();
85          while (sibling != null) {
86              final int type = sibling.getType();
87              if (type != TokenTypes.LCURLY
88                      && type != TokenTypes.RCURLY
89                      && type != TokenTypes.ENUM_CONSTANT_DEF
90                      && type != TokenTypes.SEMI
91                      && type != TokenTypes.COMMA) {
92                  result = true;
93                  break;
94              }
95              sibling = sibling.getNextSibling();
96          }
97          return result;
98      }
99  
100     /**
101      * Checks if semicolon is immediately preceded by another semicolon,
102      * making it a redundant duplicate.
103      *
104      * @param ast semicolon to check
105      * @return true if previous sibling is also a semicolon, false otherwise.
106      */
107     private static boolean isPrecededBySemi(DetailAST ast) {
108         final DetailAST prev = ast.getPreviousSibling();
109         return prev.getType() == TokenTypes.SEMI;
110     }
111 }