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.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 * <p>
39 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
40 * </p>
41 *
42 * <p>
43 * Violation Message Keys:
44 * </p>
45 * <ul>
46 * <li>
47 * {@code arrangement.members.before.inner}
48 * </li>
49 * </ul>
50 *
51 * @since 5.2
52 */
53 @FileStatefulCheck
54 public class InnerTypeLastCheck extends AbstractCheck {
55
56 /**
57 * A key is pointing to the warning message text in "messages.properties"
58 * file.
59 */
60 public static final String MSG_KEY = "arrangement.members.before.inner";
61
62 /** Set of class member tokens. */
63 private static final BitSet CLASS_MEMBER_TOKENS = TokenUtil.asBitSet(
64 TokenTypes.VARIABLE_DEF,
65 TokenTypes.METHOD_DEF,
66 TokenTypes.CTOR_DEF,
67 TokenTypes.INSTANCE_INIT,
68 TokenTypes.STATIC_INIT,
69 TokenTypes.COMPACT_CTOR_DEF
70 );
71
72 /** Meet a root class. */
73 private boolean rootClass;
74
75 @Override
76 public int[] getDefaultTokens() {
77 return getRequiredTokens();
78 }
79
80 @Override
81 public int[] getAcceptableTokens() {
82 return getRequiredTokens();
83 }
84
85 @Override
86 public int[] getRequiredTokens() {
87 return new int[] {
88 TokenTypes.CLASS_DEF,
89 TokenTypes.INTERFACE_DEF,
90 TokenTypes.RECORD_DEF,
91 };
92 }
93
94 @Override
95 public void beginTree(DetailAST rootAST) {
96 rootClass = true;
97 }
98
99 @Override
100 public void visitToken(DetailAST ast) {
101 // First root class
102 if (rootClass) {
103 rootClass = false;
104 }
105 else {
106 DetailAST nextSibling = ast;
107 while (nextSibling != null) {
108 if (!ScopeUtil.isInCodeBlock(ast)
109 && CLASS_MEMBER_TOKENS.get(nextSibling.getType())) {
110 log(nextSibling, MSG_KEY);
111 }
112 nextSibling = nextSibling.getNextSibling();
113 }
114 }
115 }
116
117 @Override
118 public void leaveToken(DetailAST ast) {
119 if (TokenUtil.isRootNode(ast.getParent())) {
120 rootClass = true;
121 }
122 }
123
124 }