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