001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018/////////////////////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.design; 021 022import java.util.BitSet; 023 024import com.puppycrawl.tools.checkstyle.FileStatefulCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; 029import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 030 031/** 032 * <p> 033 * Checks nested (internal) classes/interfaces are declared at the bottom of the 034 * primary (top-level) class after all init and static init blocks, 035 * method, constructor and field declarations. 036 * </p> 037 * <p> 038 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 039 * </p> 040 * <p> 041 * Violation Message Keys: 042 * </p> 043 * <ul> 044 * <li> 045 * {@code arrangement.members.before.inner} 046 * </li> 047 * </ul> 048 * 049 * @since 5.2 050 */ 051@FileStatefulCheck 052public class InnerTypeLastCheck extends AbstractCheck { 053 054 /** 055 * A key is pointing to the warning message text in "messages.properties" 056 * file. 057 */ 058 public static final String MSG_KEY = "arrangement.members.before.inner"; 059 060 /** Set of class member tokens. */ 061 private static final BitSet CLASS_MEMBER_TOKENS = TokenUtil.asBitSet( 062 TokenTypes.VARIABLE_DEF, 063 TokenTypes.METHOD_DEF, 064 TokenTypes.CTOR_DEF, 065 TokenTypes.INSTANCE_INIT, 066 TokenTypes.STATIC_INIT, 067 TokenTypes.COMPACT_CTOR_DEF 068 ); 069 070 /** Meet a root class. */ 071 private boolean rootClass; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getAcceptableTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getRequiredTokens() { 085 return new int[] { 086 TokenTypes.CLASS_DEF, 087 TokenTypes.INTERFACE_DEF, 088 TokenTypes.RECORD_DEF, 089 }; 090 } 091 092 @Override 093 public void beginTree(DetailAST rootAST) { 094 rootClass = true; 095 } 096 097 @Override 098 public void visitToken(DetailAST ast) { 099 // First root class 100 if (rootClass) { 101 rootClass = false; 102 } 103 else { 104 DetailAST nextSibling = ast; 105 while (nextSibling != null) { 106 if (!ScopeUtil.isInCodeBlock(ast) 107 && CLASS_MEMBER_TOKENS.get(nextSibling.getType())) { 108 log(nextSibling, MSG_KEY); 109 } 110 nextSibling = nextSibling.getNextSibling(); 111 } 112 } 113 } 114 115 @Override 116 public void leaveToken(DetailAST ast) { 117 if (TokenUtil.isRootNode(ast.getParent())) { 118 rootClass = true; 119 } 120 } 121 122}