001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.StatelessCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.TokenTypes;
029
030/**
031 * <p>
032 * Checks that overloaded methods are grouped together. Overloaded methods have the same
033 * name but different signatures where the signature can differ by the number of
034 * input parameters or type of input parameters or both.
035 * </p>
036 * <p>
037 * To configure the check:
038 * </p>
039 * <pre>
040 * &lt;module name=&quot;OverloadMethodsDeclarationOrder&quot;/&gt;
041 * </pre>
042 * <p>
043 * Example of correct grouping of overloaded methods:
044 * </p>
045 * <pre>
046 * public void foo(int i) {}
047 * public void foo(String s) {}
048 * public void foo(String s, int i) {}
049 * public void foo(int i, String s) {}
050 * public void notFoo() {}
051 * </pre>
052 * <p>
053 * Example of incorrect grouping of overloaded methods:
054 * </p>
055 * <pre>
056 * public void foo(int i) {} // OK
057 * public void foo(String s) {} // OK
058 * public void notFoo() {} // violation. Have to be after foo(String s, int i)
059 * public void foo(int i, String s) {}
060 * public void foo(String s, int i) {}
061 * </pre>
062 * @since 5.8
063 */
064@StatelessCheck
065public class OverloadMethodsDeclarationOrderCheck extends AbstractCheck {
066
067    /**
068     * A key is pointing to the warning message text in "messages.properties"
069     * file.
070     */
071    public static final String MSG_KEY = "overload.methods.declaration";
072
073    @Override
074    public int[] getDefaultTokens() {
075        return getRequiredTokens();
076    }
077
078    @Override
079    public int[] getAcceptableTokens() {
080        return getRequiredTokens();
081    }
082
083    @Override
084    public int[] getRequiredTokens() {
085        return new int[] {
086            TokenTypes.OBJBLOCK,
087        };
088    }
089
090    @Override
091    public void visitToken(DetailAST ast) {
092        final int parentType = ast.getParent().getType();
093        if (parentType == TokenTypes.CLASS_DEF
094                || parentType == TokenTypes.ENUM_DEF
095                || parentType == TokenTypes.INTERFACE_DEF
096                || parentType == TokenTypes.LITERAL_NEW) {
097            checkOverloadMethodsGrouping(ast);
098        }
099    }
100
101    /**
102     * Checks that if overload methods are grouped together they should not be
103     * separated from each other.
104     *
105     * @param objectBlock
106     *        is a class, interface or enum object block.
107     */
108    private void checkOverloadMethodsGrouping(DetailAST objectBlock) {
109        final int allowedDistance = 1;
110        DetailAST currentToken = objectBlock.getFirstChild();
111        final Map<String, Integer> methodIndexMap = new HashMap<>();
112        final Map<String, Integer> methodLineNumberMap = new HashMap<>();
113        int currentIndex = 0;
114        while (currentToken != null) {
115            if (currentToken.getType() == TokenTypes.METHOD_DEF) {
116                currentIndex++;
117                final String methodName =
118                        currentToken.findFirstToken(TokenTypes.IDENT).getText();
119                if (methodIndexMap.containsKey(methodName)) {
120                    final int previousIndex = methodIndexMap.get(methodName);
121                    if (currentIndex - previousIndex > allowedDistance) {
122                        final int previousLineWithOverloadMethod =
123                                methodLineNumberMap.get(methodName);
124                        log(currentToken, MSG_KEY,
125                                previousLineWithOverloadMethod);
126                    }
127                }
128                methodIndexMap.put(methodName, currentIndex);
129                methodLineNumberMap.put(methodName, currentToken.getLineNo());
130            }
131            currentToken = currentToken.getNextSibling();
132        }
133    }
134
135}