001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.naming; 021 022import com.puppycrawl.tools.checkstyle.api.DetailAST; 023import com.puppycrawl.tools.checkstyle.api.TokenTypes; 024import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 025 026/** 027 * <div> 028 * Checks that method names conform to a specified pattern. 029 * </div> 030 * 031 * <p>Also, checks if a method name has the same name as the residing class. 032 * The default is false (it is not allowed). It is legal in Java to have 033 * method with the same name as a class. As long as a return type is specified 034 * it is a method and not a constructor which it could be easily confused as. 035 * Does not check-style the name of an overridden methods because the developer does not 036 * have a choice in renaming such methods. 037 * </p> 038 * 039 * <ul> 040 * <li> 041 * Property {@code allowClassName} - Control whether to allow a method name to have the same name 042 * as the enclosing class name. Setting this property {@code false} helps to avoid 043 * confusion between constructors and methods. 044 * Type is {@code boolean}. 045 * Default value is {@code false}. 046 * </li> 047 * <li> 048 * Property {@code applyToPackage} - Control if check should apply to package-private members. 049 * Type is {@code boolean}. 050 * Default value is {@code true}. 051 * Since version 5.1 052 * </li> 053 * <li> 054 * Property {@code applyToPrivate} - Control if check should apply to private members. 055 * Type is {@code boolean}. 056 * Default value is {@code true}. 057 * Since version 5.1 058 * </li> 059 * <li> 060 * Property {@code applyToProtected} - Control if check should apply to protected members. 061 * Type is {@code boolean}. 062 * Default value is {@code true}. 063 * Since version 5.1 064 * </li> 065 * <li> 066 * Property {@code applyToPublic} - Control if check should apply to public members. 067 * Type is {@code boolean}. 068 * Default value is {@code true}. 069 * Since version 5.1 070 * </li> 071 * <li> 072 * Property {@code format} - Sets the pattern to match valid identifiers. 073 * Type is {@code java.util.regex.Pattern}. 074 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 075 * </li> 076 * </ul> 077 * 078 * <p> 079 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 080 * </p> 081 * 082 * <p> 083 * Violation Message Keys: 084 * </p> 085 * <ul> 086 * <li> 087 * {@code method.name.equals.class.name} 088 * </li> 089 * <li> 090 * {@code name.invalidPattern} 091 * </li> 092 * </ul> 093 * 094 * @since 3.0 095 */ 096public class MethodNameCheck 097 extends AbstractAccessControlNameCheck { 098 099 /** 100 * A key is pointing to the warning message text in "messages.properties" 101 * file. 102 */ 103 public static final String MSG_KEY = "method.name.equals.class.name"; 104 105 /** 106 * Control whether to allow a method name to have the same name as the enclosing class name. 107 * Setting this property {@code false} helps to avoid confusion 108 * between constructors and methods. 109 */ 110 private boolean allowClassName; 111 112 /** Creates a new {@code MethodNameCheck} instance. */ 113 public MethodNameCheck() { 114 super("^[a-z][a-zA-Z0-9]*$"); 115 } 116 117 @Override 118 public int[] getDefaultTokens() { 119 return getRequiredTokens(); 120 } 121 122 @Override 123 public int[] getAcceptableTokens() { 124 return getRequiredTokens(); 125 } 126 127 @Override 128 public int[] getRequiredTokens() { 129 return new int[] {TokenTypes.METHOD_DEF, }; 130 } 131 132 @Override 133 public void visitToken(DetailAST ast) { 134 if (!AnnotationUtil.hasOverrideAnnotation(ast)) { 135 // Will check the name against the format. 136 super.visitToken(ast); 137 } 138 139 if (!allowClassName) { 140 final DetailAST method = 141 ast.findFirstToken(TokenTypes.IDENT); 142 // in all cases this will be the classDef type except anon inner 143 // with anon inner classes this will be the Literal_New keyword 144 final DetailAST classDefOrNew = ast.getParent().getParent(); 145 final DetailAST classIdent = 146 classDefOrNew.findFirstToken(TokenTypes.IDENT); 147 // Following logic is to handle when a classIdent can not be 148 // found. This is when you have a Literal_New keyword followed 149 // a DOT, which is when you have: 150 // new Outclass.InnerInterface(x) { ... } 151 // Such a rare case, will not have the logic to handle parsing 152 // down the tree looking for the first ident. 153 if (classIdent != null 154 && method.getText().equals(classIdent.getText())) { 155 log(method, MSG_KEY, method.getText()); 156 } 157 } 158 } 159 160 /** 161 * Setter to control whether to allow a method name to have the same name 162 * as the enclosing class name. Setting this property {@code false} 163 * helps to avoid confusion between constructors and methods. 164 * 165 * @param allowClassName true to allow false to disallow 166 * @since 5.0 167 */ 168 public void setAllowClassName(boolean allowClassName) { 169 this.allowClassName = allowClassName; 170 } 171 172}