001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.design;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * <p>
028 * Check nested (internal) classes/interfaces are declared at the bottom of the
029 * class after all method and field declarations.
030 * </p>
031 *
032 * @author <a href="mailto:ryly@mail.ru">Ruslan Dyachenko</a>
033 */
034public class InnerTypeLastCheck extends Check
035{
036    @Override
037    public int[] getDefaultTokens()
038    {
039        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
040    }
041
042    /** Meet a root class. */
043    private boolean rootClass = true;
044
045    @Override
046    public void visitToken(DetailAST ast)
047    {
048        /** First root class */
049        if (rootClass) {
050            rootClass = false;
051        }
052        else {
053            DetailAST nextSibling = ast.getNextSibling();
054            while (null != nextSibling) {
055                if (!ScopeUtils.inCodeBlock(ast)
056                    && (nextSibling.getType() == TokenTypes.VARIABLE_DEF
057                        || nextSibling.getType() == TokenTypes.METHOD_DEF))
058                {
059                    log(nextSibling.getLineNo(), nextSibling.getColumnNo(),
060                        "arrangement.members.before.inner");
061                }
062                nextSibling = nextSibling.getNextSibling();
063            }
064        }
065    }
066
067    @Override
068    public void leaveToken(DetailAST ast)
069    {
070        /** Is this a root class */
071        if (null == ast.getParent()) {
072            rootClass = true;
073        }
074    }
075}