Enhanced the naming checks ConstantName, MemberName, MethodName and StaticVariableName to utilise the access control tuning features of AbstractAccessControlNameCheck. Thanks to Steve McKay for patch #2893581.
This commit is contained in:
parent
19d385d84a
commit
2bf724effc
|
|
@ -81,7 +81,7 @@ public class ConstantNameCheck
|
|||
final boolean isFinal = (modifiersAST != null)
|
||||
&& modifiersAST.branchContains(TokenTypes.FINAL);
|
||||
|
||||
if ((isStatic && isFinal)
|
||||
if ((isStatic && isFinal && shouldCheckInScope(modifiersAST))
|
||||
|| ScopeUtils.inInterfaceOrAnnotationBlock(aAST))
|
||||
{
|
||||
// Handle the serialVersionUID and serialPersistentFields constants
|
||||
|
|
|
|||
|
|
@ -74,7 +74,6 @@ public class MemberNameCheck
|
|||
|
||||
return (!isStatic && !ScopeUtils.inInterfaceOrAnnotationBlock(aAST)
|
||||
&& !ScopeUtils.isLocalVariableDef(aAST))
|
||||
&& (modifiersAST != null)
|
||||
&& shouldCheckInScope(modifiersAST);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,8 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
|||
* @author Travis Schneeberger
|
||||
* @version 1.1
|
||||
*/
|
||||
public class MethodNameCheck extends AbstractNameCheck
|
||||
public class MethodNameCheck
|
||||
extends AbstractAccessControlNameCheck
|
||||
{
|
||||
/**
|
||||
* for allowing method name to be the same as the class name.
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ public class StaticVariableNameCheck
|
|||
|
||||
return (isStatic
|
||||
&& !isFinal
|
||||
&& shouldCheckInScope(modifiersAST)
|
||||
&& !ScopeUtils.inInterfaceOrAnnotationBlock(aAST));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
/**
|
||||
* Test input for MethodNameCheck specifically
|
||||
* Test input for MethodNameCheck specifically
|
||||
* whether the method name equals the class name.
|
||||
*
|
||||
*
|
||||
* @author Travis Schneeberger
|
||||
*/
|
||||
public class InputMethNameEqualClsName {
|
||||
|
|
@ -12,22 +12,27 @@ public class InputMethNameEqualClsName {
|
|||
public int InputMethNameEqualClsName() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Inner {
|
||||
|
||||
//illegal name
|
||||
private int PRIVATEInputMethNameEqualClsName() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Inner {
|
||||
//illegal name
|
||||
public int Inner() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//OK name - name of the outter class's ctor
|
||||
public int InputMethNameEqualClsName() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void anotherMethod() {
|
||||
new InputMethNameEqualClsName() {
|
||||
|
||||
|
||||
//illegal name
|
||||
public int InputMethNameEqualClsName() {
|
||||
return 1;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,21 @@ public class ConstantNameCheckTest
|
|||
verify(checkConfig, getPath("InputSimple.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccessControlTuning()
|
||||
throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ConstantNameCheck.class);
|
||||
checkConfig.addAttribute("applyToPublic", "false");
|
||||
checkConfig.addAttribute("applyToProtected", "false");
|
||||
checkConfig.addAttribute("applyToPackage", "false");
|
||||
final String[] expected = {
|
||||
"142:30: Name 'BAD__NAME' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'."
|
||||
};
|
||||
verify(checkConfig, getPath("InputSimple.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterfaceAndAnnotation()
|
||||
throws Exception
|
||||
|
|
|
|||
|
|
@ -28,15 +28,16 @@ public class MethodNameCheckTest
|
|||
final String[] expected = {
|
||||
"12:16: Method Name 'InputMethNameEqualClsName' must not equal the enclosing class name.",
|
||||
"12:16: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"18:20: Method Name 'Inner' must not equal the enclosing class name.",
|
||||
"18:20: Name 'Inner' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"23:20: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"32:24: Method Name 'InputMethNameEqualClsName' must not equal the enclosing class name.",
|
||||
"32:24: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"42:9: Method Name 'SweetInterface' must not equal the enclosing class name.",
|
||||
"42:9: Name 'SweetInterface' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"48:17: Method Name 'Outter' must not equal the enclosing class name.",
|
||||
"48:17: Name 'Outter' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"17:17: Name 'PRIVATEInputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"23:20: Method Name 'Inner' must not equal the enclosing class name.",
|
||||
"23:20: Name 'Inner' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"28:20: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"37:24: Method Name 'InputMethNameEqualClsName' must not equal the enclosing class name.",
|
||||
"37:24: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"47:9: Method Name 'SweetInterface' must not equal the enclosing class name.",
|
||||
"47:9: Name 'SweetInterface' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"53:17: Method Name 'Outter' must not equal the enclosing class name.",
|
||||
"53:17: Name 'Outter' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("InputMethNameEqualClsName.java"), expected);
|
||||
|
|
@ -51,11 +52,31 @@ public class MethodNameCheckTest
|
|||
|
||||
final String[] expected = {
|
||||
"12:16: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"18:20: Name 'Inner' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"23:20: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"32:24: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"42:9: Name 'SweetInterface' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"48:17: Name 'Outter' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"17:17: Name 'PRIVATEInputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"23:20: Name 'Inner' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"28:20: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"37:24: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"47:9: Name 'SweetInterface' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"53:17: Name 'Outter' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("InputMethNameEqualClsName.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccessTuning() throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(MethodNameCheck.class);
|
||||
checkConfig.addAttribute("allowClassName", "true"); //allow method names and class names to equal
|
||||
checkConfig.addAttribute("applyToPrivate", "false"); //allow method names and class names to equal
|
||||
final String[] expected = {
|
||||
"12:16: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"23:20: Name 'Inner' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"28:20: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"37:24: Name 'InputMethNameEqualClsName' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"47:9: Name 'SweetInterface' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
"53:17: Name 'Outter' must match pattern '^[a-z][a-zA-Z0-9]*$'.",
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("InputMethNameEqualClsName.java"), expected);
|
||||
|
|
|
|||
|
|
@ -19,5 +19,18 @@ public class StaticVariableNameCheckTest
|
|||
};
|
||||
verify(checkConfig, getPath("InputSimple.java"), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccessTuning()
|
||||
throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(StaticVariableNameCheck.class);
|
||||
checkConfig.addAttribute("format", "^s[A-Z][a-zA-Z0-9]*$");
|
||||
checkConfig.addAttribute("applyToPrivate", "false"); // allow method names and class names to equal
|
||||
final String[] expected = {
|
||||
};
|
||||
verify(checkConfig, getPath("InputSimple.java"), expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,12 @@
|
|||
<section name="Release 5.1">
|
||||
<p>New features:</p>
|
||||
<ul>
|
||||
<li></li>
|
||||
<li>
|
||||
Enhanced the naming checks ConstantName, MemberName, MethodName
|
||||
and StaticVariableName to utilise the access control tuning features
|
||||
of AbstractAccessControlNameCheck. Thanks to Steve McKay for patch
|
||||
#2893581.
|
||||
</li>
|
||||
</ul>
|
||||
<p>Bug fixes:</p>
|
||||
<ul>
|
||||
|
|
|
|||
Loading…
Reference in New Issue