From 55435babb6bc6640bc66161743ffbde93a3c458b Mon Sep 17 00:00:00 2001
From: alexkravin Does not check-style the name of an overriden methods
because the developer does not
+ * have a choice in renaming such methods.
*
@@ -64,6 +67,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*
* @author Oliver Burn
* @author Travis Schneeberger
+ * @author Utkarsh Srivastava
* @version 1.1
*/
public class MethodNameCheck
@@ -74,6 +78,16 @@ public class MethodNameCheck
*/
private boolean mAllowClassName;
+ /**
+ * {@link Override Override} annotation name.
+ */
+ private static final String OVERRIDE = "Override";
+
+ /**
+ * Canonical {@link Override Override} annotation name.
+ */
+ private static final String CANONICAL_OVERRIDE = "java.lang." + OVERRIDE;
+
/** Creates a new MethodNameCheck instance. */
public MethodNameCheck()
{
@@ -89,7 +103,11 @@ public class MethodNameCheck
@Override
public void visitToken(DetailAST aAst)
{
- super.visitToken(aAst); // Will check the name against the format.
+ if (!AnnotationUtility.containsAnnotation(aAst, OVERRIDE)
+ && !AnnotationUtility.containsAnnotation(aAst, CANONICAL_OVERRIDE))
+ {
+ super.visitToken(aAst); // Will check the name against the format.
+ }
if (!mAllowClassName) {
final DetailAST method =
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheckTest.java
index 6a0070c8f..08ca55499 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/MethodNameCheckTest.java
@@ -111,4 +111,18 @@ public class MethodNameCheckTest
verify(checkConfig, getPath("naming/InputMethodNameExtra.java"), expected);
}
+
+ @Test
+ public void testOverridenMethods() throws Exception
+ {
+ final DefaultConfiguration checkConfig =
+ createCheckConfig(MethodNameCheck.class);
+
+ final String[] expected = {
+ "17:17: Name 'PUBLICfoo' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
+ "20:20: Name 'PROTECTEDfoo' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
+ };
+
+ verify(checkConfig, getPath("naming/InputMethodNameOverridenMethods.java"), expected);
+ }
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/naming/InputMethodNameOverridenMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/naming/InputMethodNameOverridenMethods.java
new file mode 100644
index 000000000..9dd2e020e
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/naming/InputMethodNameOverridenMethods.java
@@ -0,0 +1,23 @@
+package com.puppycrawl.tools.checkstyle.naming;
+
+public class InputMethodNameOverridenMethods extends SomeClass
+{
+ @Override
+ public void PUBLICfoo() { //Ignored due to impossibility of change by developer
+
+ }
+
+ @java.lang.Override
+ protected void PROTECTEDfoo() { //Ignored due to impossibility of change by developer
+
+ }
+}
+
+class SomeClass {
+ public void PUBLICfoo() { //Warning (broken naming convention)
+
+ }
+ protected void PROTECTEDfoo() { //Warning (broken naming convention)
+
+ }
+}