001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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 com.puppycrawl.tools.checkstyle.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; 026 027/** 028 * <p> 029 * Check nested (internal) classes/interfaces are declared at the bottom of the 030 * class after all method and field declarations. 031 * </p> 032 * 033 * @author <a href="mailto:ryly@mail.ru">Ruslan Dyachenko</a> 034 */ 035public class InnerTypeLastCheck extends AbstractCheck { 036 037 /** 038 * A key is pointing to the warning message text in "messages.properties" 039 * file. 040 */ 041 public static final String MSG_KEY = "arrangement.members.before.inner"; 042 043 /** Meet a root class. */ 044 private boolean rootClass = true; 045 046 @Override 047 public int[] getDefaultTokens() { 048 return getAcceptableTokens(); 049 } 050 051 @Override 052 public int[] getAcceptableTokens() { 053 return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF}; 054 } 055 056 @Override 057 public int[] getRequiredTokens() { 058 return getAcceptableTokens(); 059 } 060 061 @Override 062 public void visitToken(DetailAST ast) { 063 // First root class 064 if (rootClass) { 065 rootClass = false; 066 } 067 else { 068 DetailAST nextSibling = ast.getNextSibling(); 069 while (nextSibling != null) { 070 if (!ScopeUtils.isInCodeBlock(ast) 071 && (nextSibling.getType() == TokenTypes.VARIABLE_DEF 072 || nextSibling.getType() == TokenTypes.METHOD_DEF)) { 073 log(nextSibling.getLineNo(), nextSibling.getColumnNo(), 074 MSG_KEY); 075 } 076 nextSibling = nextSibling.getNextSibling(); 077 } 078 } 079 } 080 081 @Override 082 public void leaveToken(DetailAST ast) { 083 // Is this a root class 084 if (ast.getParent() == null) { 085 rootClass = true; 086 } 087 } 088}