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.design;
21  
22  import java.util.Set;
23  
24  import com.puppycrawl.tools.checkstyle.StatelessCheck;
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  
30  /**
31   * <div>
32   * Checks nested (internal) classes/interfaces are declared at the bottom of the
33   * primary (top-level) class after all init and static init blocks,
34   * method, constructor and field declarations.
35   * </div>
36   *
37   * @since 5.2
38   */
39  @StatelessCheck
40  public class InnerTypeLastCheck extends AbstractCheck {
41  
42      /**
43       * A key is pointing to the warning message text in "messages.properties"
44       * file.
45       */
46      public static final String MSG_KEY = "arrangement.members.before.inner";
47  
48      /** Set of class member tokens. */
49      private static final Set<Integer> CLASS_MEMBER_TOKENS = Set.of(
50              TokenTypes.VARIABLE_DEF,
51              TokenTypes.METHOD_DEF,
52              TokenTypes.CTOR_DEF,
53              TokenTypes.INSTANCE_INIT,
54              TokenTypes.STATIC_INIT,
55              TokenTypes.COMPACT_CTOR_DEF
56      );
57  
58      /**
59       * Creates a new {@code InnerTypeLastCheck} instance.
60       */
61      public InnerTypeLastCheck() {
62          // no code by default
63      }
64  
65      @Override
66      public int[] getDefaultTokens() {
67          return getRequiredTokens();
68      }
69  
70      @Override
71      public int[] getAcceptableTokens() {
72          return getRequiredTokens();
73      }
74  
75      @Override
76      public int[] getRequiredTokens() {
77          return new int[] {
78              TokenTypes.CLASS_DEF,
79              TokenTypes.INTERFACE_DEF,
80              TokenTypes.RECORD_DEF,
81          };
82      }
83  
84      @Override
85      public void visitToken(DetailAST ast) {
86          DetailAST nextSibling = ast;
87          while (nextSibling != null) {
88              if (!ScopeUtil.isInCodeBlock(ast)
89                      && CLASS_MEMBER_TOKENS.contains(nextSibling.getType())) {
90                  log(nextSibling, MSG_KEY);
91              }
92              nextSibling = nextSibling.getNextSibling();
93          }
94      }
95  
96  }