diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheck.java index e9f19bfff88..ed93a74abd5 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheck.java @@ -26,18 +26,50 @@ /** *

* Checks that method names conform to a format specified - * by the format property. The format is a - * {@link java.util.regex.Pattern regular expression} - * and defaults to - * ^[a-z][a-zA-Z0-9]*$. + * by the format property. *

* *

Also, checks if a method name has the same name as the residing class. - * The default is false (it is not allowed). It is legal in Java to have - * method with the same name as a class. As long as a return type is specified + * The default is false (it is not allowed). It is legal in Java to have + * method with the same name as a class. As long as a return type is specified * it is a method and not a constructor which it could be easily confused as. - *

Does not check-style the name of an overridden methods

because the developer does not + * Does not check-style the name of an overridden methods because the developer does not * have a choice in renaming such methods. + *

+ * + * * *

* An example of how to configure the check is: @@ -64,6 +96,8 @@ * <property name="allowClassName" value="true"/> * </module> * + * + * @since 3.0 */ public class MethodNameCheck extends AbstractAccessControlNameCheck { @@ -85,7 +119,15 @@ public class MethodNameCheck private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE; /** - * For allowing method name to be the same as the class name. + * Controls whether to allow a method name to have the same name as the residing class name. + * This is not to be confused with a constructor. An easy mistake is to place a return type on + * a constructor declaration which turns it into a method. For example: + *

+     * class MyClass {
+     *     public void MyClass() {} //this is a method
+     *     public MyClass() {} //this is a constructor
+     * }
+     * 
*/ private boolean allowClassName; @@ -139,7 +181,16 @@ public void visitToken(DetailAST ast) { } /** - * Sets the property for allowing a method to be the same name as a class. + * Setter to controls whether to allow a method name to have the same name as the residing + * class name. This is not to be confused with a constructor. An easy mistake is to place + * a return type on a constructor declaration which turns it into a method. For example: + *
+     * class MyClass {
+     *     public void MyClass() {} //this is a method
+     *     public MyClass() {} //this is a constructor
+     * }
+     * 
+ * * @param allowClassName true to allow false to disallow */ public void setAllowClassName(boolean allowClassName) { diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java index cf2cd240905..62fd95b0805 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java @@ -114,6 +114,7 @@ public void testAllCheckSectionJavaDocs() throws Exception { && !"LocalFinalVariableName".equals(sectionName) && !"LocalVariableName".equals(sectionName) && !"MemberName".equals(sectionName) + && !"MethodName".equals(sectionName) ) { continue; } diff --git a/src/xdocs/config_naming.xml b/src/xdocs/config_naming.xml index 8e8af289bb0..f59f17a71f0 100644 --- a/src/xdocs/config_naming.xml +++ b/src/xdocs/config_naming.xml @@ -1030,10 +1030,18 @@ for (int i = 1; i < 10; i++) {}
+

Since Checkstyle 3.0

-

Since Checkstyle 3.0

- Validates identifiers for methods. + Checks that method names conform to a format specified by the format property. +

+

+ Also, checks if a method name has the same name as the residing class. + The default is false (it is not allowed). It is legal in Java to have + method with the same name as a class. As long as a return type is specified + it is a method and not a constructor which it could be easily confused as. + Does not check-style the name of an overridden methods because the developer + does not have a choice in renaming such methods.

@@ -1057,8 +1065,8 @@ for (int i = 1; i < 10; i++) {} allowClassName Controls whether to allow a method name to have the same - name as the residing class name. This is not to be confused - with a constructor. An easy mistake is to place a return + name as the residing class name. This is not to be confused + with a constructor. An easy mistake is to place a return type on a constructor declaration which turns it into a method. For example:
@@ -1107,11 +1115,29 @@ class MyClass {
 
       
         

- To configure the check: + An example of how to configure the check is:

<module name="MethodName"/> +

+ An example of how to configure the check for names that begin with a lower case letter, + followed by letters, digits, and underscores is: +

+ +<module name="MethodName"> + <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> +</module> + +

+ An example of how to configure the check to allow method names to be equal to the + residing class name is: +

+ +<module name="MethodName"> + <property name="allowClassName" value="true"/> +</module> +