001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 * <p> 028 * Checks that method names conform to a format specified 029 * by the format property. 030 * </p> 031 * 032 * <p>Also, checks if a method name has the same name as the residing class. 033 * The default is false (it is not allowed). It is legal in Java to have 034 * method with the same name as a class. As long as a return type is specified 035 * it is a method and not a constructor which it could be easily confused as. 036 * Does not check-style the name of an overridden methods because the developer does not 037 * have a choice in renaming such methods. 038 * </p> 039 * 040 * <ul> 041 * <li> 042 * Property {@code format} - Specifies valid identifiers. 043 * Default value is {@code "^[a-z][a-zA-Z0-9]*$"}. 044 * </li> 045 * <li> 046 * Property {@code allowClassName} - Controls whether to allow a method name to have the same name 047 * as the residing class name. This is not to be confused with a constructor. An easy mistake is 048 * to place a return type on a constructor declaration which turns it into a method. For example: 049 * <pre> 050 * class MyClass { 051 * public void MyClass() {} //this is a method 052 * public MyClass() {} //this is a constructor 053 * } 054 * </pre> Default value is {@code false}. 055 * </li> 056 * <li> 057 * Property {@code applyToPublic} - Controls whether to apply the check to public member. 058 * Default value is {@code true}. 059 * </li> 060 * <li> 061 * Property {@code applyToProtected} - Controls whether to apply the check to protected member. 062 * Default value is {@code true}. 063 * </li> 064 * <li> 065 * Property {@code applyToPackage} - Controls whether to apply the check to package-private member. 066 * Default value is {@code true}. 067 * </li> 068 * <li> 069 * Property {@code applyToPrivate} - Controls whether to apply the check to private member. 070 * Default value is {@code true}. 071 * </li> 072 * </ul> 073 * 074 * <p> 075 * An example of how to configure the check is: 076 * </p> 077 * <pre> 078 * <module name="MethodName"/> 079 * </pre> 080 * <p> 081 * An example of how to configure the check for names that begin with 082 * a lower case letter, followed by letters, digits, and underscores is: 083 * </p> 084 * <pre> 085 * <module name="MethodName"> 086 * <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> 087 * </module> 088 * </pre> 089 * 090 * <p> 091 * An example of how to configure the check to allow method names 092 * to be equal to the residing class name is: 093 * </p> 094 * <pre> 095 * <module name="MethodName"> 096 * <property name="allowClassName" value="true"/> 097 * </module> 098 * </pre> 099 * 100 * @since 3.0 101 */ 102public class MethodNameCheck 103 extends AbstractAccessControlNameCheck { 104 105 /** 106 * A key is pointing to the warning message text in "messages.properties" 107 * file. 108 */ 109 public static final String MSG_KEY = "method.name.equals.class.name"; 110 111 /** 112 * {@link Override Override} annotation name. 113 */ 114 private static final String OVERRIDE = "Override"; 115 116 /** 117 * Canonical {@link Override Override} annotation name. 118 */ 119 private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE; 120 121 /** 122 * Controls whether to allow a method name to have the same name as the residing class name. 123 * This is not to be confused with a constructor. An easy mistake is to place a return type on 124 * a constructor declaration which turns it into a method. For example: 125 * <pre> 126 * class MyClass { 127 * public void MyClass() {} //this is a method 128 * public MyClass() {} //this is a constructor 129 * } 130 * </pre> 131 */ 132 private boolean allowClassName; 133 134 /** Creates a new {@code MethodNameCheck} instance. */ 135 public MethodNameCheck() { 136 super("^[a-z][a-zA-Z0-9]*$"); 137 } 138 139 @Override 140 public int[] getDefaultTokens() { 141 return getRequiredTokens(); 142 } 143 144 @Override 145 public int[] getAcceptableTokens() { 146 return getRequiredTokens(); 147 } 148 149 @Override 150 public int[] getRequiredTokens() { 151 return new int[] {TokenTypes.METHOD_DEF, }; 152 } 153 154 @Override 155 public void visitToken(DetailAST ast) { 156 if (!AnnotationUtil.containsAnnotation(ast, OVERRIDE) 157 && !AnnotationUtil.containsAnnotation(ast, CANONICAL_OVERRIDE)) { 158 // Will check the name against the format. 159 super.visitToken(ast); 160 } 161 162 if (!allowClassName) { 163 final DetailAST method = 164 ast.findFirstToken(TokenTypes.IDENT); 165 //in all cases this will be the classDef type except anon inner 166 //with anon inner classes this will be the Literal_New keyword 167 final DetailAST classDefOrNew = ast.getParent().getParent(); 168 final DetailAST classIdent = 169 classDefOrNew.findFirstToken(TokenTypes.IDENT); 170 // Following logic is to handle when a classIdent can not be 171 // found. This is when you have a Literal_New keyword followed 172 // a DOT, which is when you have: 173 // new Outclass.InnerInterface(x) { ... } 174 // Such a rare case, will not have the logic to handle parsing 175 // down the tree looking for the first ident. 176 if (classIdent != null 177 && method.getText().equals(classIdent.getText())) { 178 log(method, MSG_KEY, method.getText()); 179 } 180 } 181 } 182 183 /** 184 * Setter to controls whether to allow a method name to have the same name as the residing 185 * class name. This is not to be confused with a constructor. An easy mistake is to place 186 * a return type on a constructor declaration which turns it into a method. For example: 187 * <pre> 188 * class MyClass { 189 * public void MyClass() {} //this is a method 190 * public MyClass() {} //this is a constructor 191 * } 192 * </pre> 193 * 194 * @param allowClassName true to allow false to disallow 195 */ 196 public void setAllowClassName(boolean allowClassName) { 197 this.allowClassName = allowClassName; 198 } 199 200}