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