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.coding;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import com.puppycrawl.tools.checkstyle.api.Check;
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import com.puppycrawl.tools.checkstyle.api.TokenTypes;
027
028/**
029 * <p>
030 * Checks that overload methods are grouped together. Example:
031 * </p>
032 * <pre>
033 * <code>
034 * public void foo(int i) {}
035 * public void foo(String s) {}
036 * public void notFoo() {} // Have to be after foo(int i, String s)
037 * public void foo(int i, String s) {}
038 * </code>
039 * </pre>
040 * <p>
041 * An example of how to configure the check is:
042 * </p>
043 *
044 * <pre>
045 * &lt;module name="OverloadMethodsDeclarationOrder"/&gt;
046 * </pre>
047 * @author maxvetrenko
048 */
049public class OverloadMethodsDeclarationOrderCheck extends Check
050{
051
052    @Override
053    public int[] getDefaultTokens()
054    {
055        return new int[] {
056            TokenTypes.OBJBLOCK,
057        };
058    }
059
060    @Override
061    public void visitToken(DetailAST ast)
062    {
063        final int parentType = ast.getParent().getType();
064        if (parentType == TokenTypes.CLASS_DEF
065                || parentType == TokenTypes.ENUM_DEF
066                || parentType == TokenTypes.INTERFACE_DEF
067                || parentType == TokenTypes.LITERAL_NEW)
068        {
069            checkOverloadMethodsGrouping(ast);
070        }
071    }
072
073    /**
074     * Checks that if overload methods are grouped together they should not be
075     * separated from each other.
076     * @param objectBlock
077     *        is a class, interface or enum object block.
078     */
079    private void checkOverloadMethodsGrouping(DetailAST objectBlock)
080    {
081        final int allowedDistance = 1;
082        DetailAST currentToken = objectBlock.getFirstChild();
083        final Map<String, Integer> methodIndexMap = new HashMap<String, Integer>();
084        final Map<String, Integer> methodLineNumberMap = new HashMap<String, Integer>();
085        int currentIndex = 0;
086        while (currentToken != null) {
087            if (currentToken.getType() == TokenTypes.METHOD_DEF) {
088                currentIndex++;
089                final String methodName =
090                        currentToken.findFirstToken(TokenTypes.IDENT).getText();
091                if (methodIndexMap.containsKey(methodName)) {
092                    final int priviousIndex = methodIndexMap.get(methodName);
093                    if (currentIndex - priviousIndex > allowedDistance) {
094                        final int previousLineWithOverloadMethod =
095                                methodLineNumberMap.get(methodName);
096                        log(currentToken.getLineNo(), "overload.methods.declaration",
097                                previousLineWithOverloadMethod);
098                    }
099                }
100                methodIndexMap.put(methodName, currentIndex);
101                methodLineNumberMap.put(methodName, currentToken.getLineNo());
102            }
103            currentToken = currentToken.getNextSibling();
104        }
105    }
106}