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