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.TokenTypes;
024
025/**
026 * <p>
027 * Check nested (internal) classes/interfaces are declared at the bottom of the
028 * class after all method and field declarations.
029 * </p>
030 *
031 * @author <a href="mailto:ryly@mail.ru">Ruslan Dyachenko</a>
032 */
033public class InnerTypeLastCheck extends Check
034{
035    @Override
036    public int[] getDefaultTokens()
037    {
038        return new int[] {TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
039    }
040
041    /** Meet a root class. */
042    private boolean mRootClass = true;
043
044    @Override
045    public void visitToken(DetailAST aAST)
046    {
047        /** First root class */
048        if (mRootClass) {
049            mRootClass = false;
050        }
051        else {
052            DetailAST nextSibling = aAST.getNextSibling();
053            while (null != nextSibling
054                && ((nextSibling.getType() != TokenTypes.CLASS_DEF)
055                    || (nextSibling.getType() != TokenTypes.INTERFACE_DEF)))
056            {
057                if (nextSibling.getType() == TokenTypes.VARIABLE_DEF
058                        || nextSibling.getType() == TokenTypes.METHOD_DEF)
059                {
060                    log(nextSibling.getLineNo(), nextSibling.getColumnNo(),
061                        "arrangement.members.before.inner");
062                }
063                nextSibling = nextSibling.getNextSibling();
064            }
065        }
066    }
067
068    @Override
069    public void leaveToken(DetailAST aAST)
070    {
071        /** Is this a root class */
072        if (null == aAST.getParent()) {
073            mRootClass = true;
074        }
075    }
076}