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.design;
21  
22  import java.util.BitSet;
23  
24  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
25  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28  import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;
29  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
30  
31  /**
32   * <div>
33   * Checks nested (internal) classes/interfaces are declared at the bottom of the
34   * primary (top-level) class after all init and static init blocks,
35   * method, constructor and field declarations.
36   * </div>
37   *
38   * @since 5.2
39   */
40  @FileStatefulCheck
41  public class InnerTypeLastCheck extends AbstractCheck {
42  
43      /**
44       * A key is pointing to the warning message text in "messages.properties"
45       * file.
46       */
47      public static final String MSG_KEY = "arrangement.members.before.inner";
48  
49      /** Set of class member tokens. */
50      private static final BitSet CLASS_MEMBER_TOKENS = TokenUtil.asBitSet(
51              TokenTypes.VARIABLE_DEF,
52              TokenTypes.METHOD_DEF,
53              TokenTypes.CTOR_DEF,
54              TokenTypes.INSTANCE_INIT,
55              TokenTypes.STATIC_INIT,
56              TokenTypes.COMPACT_CTOR_DEF
57      );
58  
59      /** Meet a root class. */
60      private boolean rootClass;
61  
62      @Override
63      public int[] getDefaultTokens() {
64          return getRequiredTokens();
65      }
66  
67      @Override
68      public int[] getAcceptableTokens() {
69          return getRequiredTokens();
70      }
71  
72      @Override
73      public int[] getRequiredTokens() {
74          return new int[] {
75              TokenTypes.CLASS_DEF,
76              TokenTypes.INTERFACE_DEF,
77              TokenTypes.RECORD_DEF,
78          };
79      }
80  
81      @Override
82      public void beginTree(DetailAST rootAST) {
83          rootClass = true;
84      }
85  
86      @Override
87      public void visitToken(DetailAST ast) {
88          // First root class
89          if (rootClass) {
90              rootClass = false;
91          }
92          else {
93              DetailAST nextSibling = ast;
94              while (nextSibling != null) {
95                  if (!ScopeUtil.isInCodeBlock(ast)
96                          && CLASS_MEMBER_TOKENS.get(nextSibling.getType())) {
97                      log(nextSibling, MSG_KEY);
98                  }
99                  nextSibling = nextSibling.getNextSibling();
100             }
101         }
102     }
103 
104     @Override
105     public void leaveToken(DetailAST ast) {
106         if (TokenUtil.isRootNode(ast.getParent())) {
107             rootClass = true;
108         }
109     }
110 
111 }