Issue #2290: Add 'ignoreOverridden' option to skip methods with @Override annotation

This commit is contained in:
Andrei Selkin 2015-11-04 23:11:52 +03:00 committed by Roman Ivanov
parent a619bc1391
commit c9b625bcf0
4 changed files with 128 additions and 1 deletions

View File

@ -30,6 +30,9 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* and defaults to
* <strong>^[a-z][a-zA-Z0-9]*$</strong>.
* </p>
* <p>The check has the following option:</p>
* <p><b>ignoreOverridden</b> - allows to skip methods with Override annotation from
* validation. Default values is <b>false</b> .</p>
* <p>
* An example of how to configure the check is:
* </p>
@ -45,11 +48,27 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
* &lt;property name="format" value="^^[a-z](_?[a-zA-Z0-9]+)*$"/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* An example of how to configure the check to skip methods with Override annotation from
* validation:
* </p>
* <pre>
* &lt;module name="ParameterName"&gt;
* &lt;property name="ignoreOverridden" value="true"/&gt;
* &lt;/module&gt;
* </pre>
*
* @author Oliver Burn
* @author Andrei Selkin
*/
public class ParameterNameCheck
extends AbstractNameCheck {
/**
* Allows to skip methods with Override annotation from validation.
*/
private boolean ignoreOverridden;
/**
* Creates a new {@code ParameterNameCheck} instance.
*/
@ -57,6 +76,15 @@ public class ParameterNameCheck
super("^[a-z][a-zA-Z0-9]*$");
}
/**
* Sets whether to skip methods with Override annotation from validation.
*
* @param ignoreOverridden Flag for skipping methods with Override annotation.
*/
public void setIgnoreOverridden(boolean ignoreOverridden) {
this.ignoreOverridden = ignoreOverridden;
}
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
@ -74,6 +102,29 @@ public class ParameterNameCheck
@Override
protected boolean mustCheckName(DetailAST ast) {
return ast.getParent().getType() != TokenTypes.LITERAL_CATCH;
boolean checkName = true;
if (ignoreOverridden && isOverriddenMethod(ast)
|| ast.getParent().getType() == TokenTypes.LITERAL_CATCH) {
checkName = false;
}
return checkName;
}
/**
* Checks whether a method is annotated with Override annotation.
* @param ast method parameter definition token.
* @return true if a method is annotated with Override annotation.
*/
private static boolean isOverriddenMethod(DetailAST ast) {
boolean overridden = false;
final DetailAST parent = ast.getParent().getParent();
final DetailAST annotation = parent.getFirstChild().getFirstChild();
if (annotation.getType() == TokenTypes.ANNOTATION) {
final DetailAST overrideToken = annotation.findFirstToken(TokenTypes.IDENT);
if ("Override".equals(overrideToken.getText())) {
overridden = true;
}
}
return overridden;
}
}

View File

@ -92,4 +92,39 @@ public class ParameterNameCheckTest
};
assertArrayEquals(expected, actual);
}
@Test
public void testSkipMethodsWithOverrideAnnotationTrue()
throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(ParameterNameCheck.class);
checkConfig.addAttribute("format", "^h$");
checkConfig.addAttribute("ignoreOverridden", "true");
final String pattern = "^h$";
final String[] expected = {
"11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
"15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
};
verify(checkConfig, getPath("InputOverrideAnnotation.java"), expected);
}
@Test
public void testSkipMethodsWithOverrideAnnotationFalse()
throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(ParameterNameCheck.class);
checkConfig.addAttribute("format", "^h$");
checkConfig.addAttribute("ignoreOverridden", "false");
final String pattern = "^h$";
final String[] expected = {
"6:34: " + getCheckMessage(MSG_INVALID_PATTERN, "o", pattern),
"11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
"15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
};
verify(checkConfig, getPath("InputOverrideAnnotation.java"), expected);
}
}

View File

@ -0,0 +1,16 @@
package com.puppycrawl.tools.checkstyle.checks.naming;
public class InputOverrideAnnotation {
@Override
public boolean equals(Object o) {
return super.equals(o);
}
@SuppressWarnings("")
public void foo(Object object) {
}
public void foo2(Integer aaaa) {}
}

View File

@ -907,6 +907,22 @@ class MyClass {
<td><a href="property_types.html#regexp">regular expression</a></td>
<td><code>^[a-z][a-zA-Z0-9]*$</code></td>
</tr>
<tr>
<td>ignoreOverridden</td>
<td>
Allows to skip methods with Override annotation from validation. For example, the
following method's parameter will be skipped from validation, if
ignoreOverridden is true:
<pre>
@Override
public boolean equals(Object o) {
return super.equals(o);
}
</pre>
</td>
<td><a href="property_types.html#boolean">Boolean</a></td>
<td><code>false</code></td>
</tr>
</table>
</subsection>
@ -917,6 +933,15 @@ class MyClass {
<source>
&lt;module name=&quot;ParameterName&quot;/&gt;
</source>
<p>
An example of how to configure the check to skip methods with Override annotation from
validation:
</p>
<source>
&lt;module name="ParameterName"&gt;
&lt;property name="ignoreOverridden" value="true"/&gt;
&lt;/module&gt;
</source>
</subsection>
<subsection name="Example of Usage">