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 * <module name="OverloadMethodsDeclarationOrder"/> 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 * <p> 063 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 064 * </p> 065 * <p> 066 * Violation Message Keys: 067 * </p> 068 * <ul> 069 * <li> 070 * {@code overload.methods.declaration} 071 * </li> 072 * </ul> 073 * 074 * @since 5.8 075 */ 076@StatelessCheck 077public class OverloadMethodsDeclarationOrderCheck extends AbstractCheck { 078 079 /** 080 * A key is pointing to the warning message text in "messages.properties" 081 * file. 082 */ 083 public static final String MSG_KEY = "overload.methods.declaration"; 084 085 @Override 086 public int[] getDefaultTokens() { 087 return getRequiredTokens(); 088 } 089 090 @Override 091 public int[] getAcceptableTokens() { 092 return getRequiredTokens(); 093 } 094 095 @Override 096 public int[] getRequiredTokens() { 097 return new int[] { 098 TokenTypes.OBJBLOCK, 099 }; 100 } 101 102 @Override 103 public void visitToken(DetailAST ast) { 104 final int parentType = ast.getParent().getType(); 105 if (parentType == TokenTypes.CLASS_DEF 106 || parentType == TokenTypes.ENUM_DEF 107 || parentType == TokenTypes.INTERFACE_DEF 108 || parentType == TokenTypes.LITERAL_NEW) { 109 checkOverloadMethodsGrouping(ast); 110 } 111 } 112 113 /** 114 * Checks that if overload methods are grouped together they should not be 115 * separated from each other. 116 * 117 * @param objectBlock 118 * is a class, interface or enum object block. 119 */ 120 private void checkOverloadMethodsGrouping(DetailAST objectBlock) { 121 final int allowedDistance = 1; 122 DetailAST currentToken = objectBlock.getFirstChild(); 123 final Map<String, Integer> methodIndexMap = new HashMap<>(); 124 final Map<String, Integer> methodLineNumberMap = new HashMap<>(); 125 int currentIndex = 0; 126 while (currentToken != null) { 127 if (currentToken.getType() == TokenTypes.METHOD_DEF) { 128 currentIndex++; 129 final String methodName = 130 currentToken.findFirstToken(TokenTypes.IDENT).getText(); 131 if (methodIndexMap.containsKey(methodName)) { 132 final int previousIndex = methodIndexMap.get(methodName); 133 if (currentIndex - previousIndex > allowedDistance) { 134 final int previousLineWithOverloadMethod = 135 methodLineNumberMap.get(methodName); 136 log(currentToken, MSG_KEY, 137 previousLineWithOverloadMethod); 138 } 139 } 140 methodIndexMap.put(methodName, currentIndex); 141 methodLineNumberMap.put(methodName, currentToken.getLineNo()); 142 } 143 currentToken = currentToken.getNextSibling(); 144 } 145 } 146 147}