From a103ccf92ba26a1b7bfb4296f9d9ce85ca6dea79 Mon Sep 17 00:00:00 2001 From: rnveach Date: Mon, 13 Feb 2017 11:09:45 -0500 Subject: Issue #3721: expanded message and documentation AbbreviationAsWordInName --- .../AbbreviationAsWordInNameTest.java | 2 +- .../naming/AbbreviationAsWordInNameCheck.java | 12 +++++- .../checkstyle/checks/naming/messages.properties | 2 +- .../naming/AbbreviationAsWordInNameCheckTest.java | 43 +++++++++++----------- src/xdocs/config_naming.xml | 13 ++++++- 5 files changed, 46 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/it/java/com/google/checkstyle/test/chapter5naming/rule53camelcase/AbbreviationAsWordInNameTest.java b/src/it/java/com/google/checkstyle/test/chapter5naming/rule53camelcase/AbbreviationAsWordInNameTest.java index 4ead6ae4d..43a130c28 100644 --- a/src/it/java/com/google/checkstyle/test/chapter5naming/rule53camelcase/AbbreviationAsWordInNameTest.java +++ b/src/it/java/com/google/checkstyle/test/chapter5naming/rule53camelcase/AbbreviationAsWordInNameTest.java @@ -42,7 +42,7 @@ public class AbbreviationAsWordInNameTest extends BaseCheckTestSupport { @Test public void abbreviationAsWordInNameTest() throws Exception { - final int maxCapitalCount = 1; + final int maxCapitalCount = 2; final String[] expected = { "50: " + getWarningMessage("newCustomerID", maxCapitalCount), diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck.java index e57ba42d3..501d2282a 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheck.java @@ -40,6 +40,16 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils; * Google Style Guide to get to know how to avoid long abbreviations in names. *

*

+ * {@code allowedAbbreviationLength} specifies how many consecutive capital letters are + * allowed in the identifier. + * A value of 3 indicates that up to 4 consecutive capital letters are allowed, + * one after the other, before a violation is printed. The identifier 'MyTEST' would be + * allowed, but 'MyTESTS' would not be. + * A value of 0 indicates that only 1 consecutive capital letter is allowed. This + * is what should be used to enforce strict camel casing. The identifier 'MyTest' would + * be allowed, but 'MyTEst' would not be. + *

+ *

* Option {@code allowedAbbreviationLength} indicates on the allowed amount of capital * letters in abbreviations in the classes, interfaces, * variables and methods names. Default value is '3'. @@ -209,7 +219,7 @@ public class AbbreviationAsWordInNameCheck extends AbstractCheck { final String abbr = getDisallowedAbbreviation(typeName); if (abbr != null) { - log(nameAst.getLineNo(), MSG_KEY, typeName, allowedAbbreviationLength); + log(nameAst.getLineNo(), MSG_KEY, typeName, allowedAbbreviationLength + 1); } } } diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/naming/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/naming/messages.properties index b62c85948..41204e8a0 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/naming/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/naming/messages.properties @@ -3,4 +3,4 @@ illegal.abstract.class.name=Name ''{0}'' must match pattern ''{1}''. method.name.equals.class.name=Method Name ''{0}'' must not equal the enclosing class name. no.abstract.class.modifier=Class ''{0}'' must be declared as ''abstract''. -abbreviation.as.word=Abbreviation in name ''{0}'' must contain no more than ''{1}'' capital letters. +abbreviation.as.word=Abbreviation in name ''{0}'' must contain no more than ''{1}'' consecutive capital letters. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheckTest.java index 0d4bce22b..c076f65f4 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/AbbreviationAsWordInNameCheckTest.java @@ -43,11 +43,11 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - final int expectedCapitalCount = 3; - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "3"); checkConfig.addAttribute("allowedAbbreviations", "III"); checkConfig.addAttribute("tokens", "CLASS_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 4; final String[] expected = { "9: " + getWarningMessage("FactoryWithBADNAme", expectedCapitalCount), @@ -62,13 +62,13 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeNamesForFourPermittedCapitalLetters() throws Exception { - final int expectedCapitalCount = 4; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "4"); checkConfig.addAttribute("allowedAbbreviations", "CLASS,FACTORY"); checkConfig.addAttribute("tokens", "CLASS_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 5; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), @@ -80,13 +80,13 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeNamesForFivePermittedCapitalLetters() throws Exception { - final int expectedCapitalCount = 5; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "5"); checkConfig.addAttribute("allowedAbbreviations", "CLASS"); checkConfig.addAttribute("tokens", "CLASS_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 6; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount), @@ -98,16 +98,16 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeAndVariablesAndMethodNames() throws Exception { - final int expectedCapitalCount = 5; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "5"); checkConfig.addAttribute("allowedAbbreviations", "CLASS"); checkConfig.addAttribute("tokens", "CLASS_DEF" + ",VARIABLE_DEF" + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF" + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 6; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), @@ -124,10 +124,9 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeAndVariablesAndMethodNamesWithNoIgnores() throws Exception { - final int expectedCapitalCount = 5; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "5"); checkConfig.addAttribute("allowedAbbreviations", "NUMBER,MARAZMATIC,VARIABLE"); checkConfig.addAttribute("ignoreStatic", "false"); checkConfig.addAttribute("ignoreFinal", "false"); @@ -136,6 +135,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF" + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 6; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), @@ -153,10 +153,9 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeAndVariablesAndMethodNamesWithIgnores() throws Exception { - final int expectedCapitalCount = 5; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "5"); checkConfig.addAttribute("allowedAbbreviations", "NUMBER,MARAZMATIC,VARIABLE"); checkConfig.addAttribute("ignoreStatic", "true"); checkConfig.addAttribute("ignoreFinal", "true"); @@ -165,6 +164,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF" + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 6; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), @@ -178,10 +178,9 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeAndVariablesAndMethodNamesWithIgnoresFinal() throws Exception { - final int expectedCapitalCount = 4; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "4"); checkConfig.addAttribute("allowedAbbreviations", "MARAZMATIC,VARIABLE"); checkConfig.addAttribute("ignoreStatic", "false"); checkConfig.addAttribute("ignoreFinal", "true"); @@ -190,6 +189,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF" + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 5; final String[] expected = { "12: " + getWarningMessage("AbstractCLASSName", expectedCapitalCount), @@ -207,10 +207,9 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { @Test public void testTypeAndVariablesAndMethodNamesWithIgnoresStatic() throws Exception { - final int expectedCapitalCount = 5; final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "5"); checkConfig.addAttribute("allowedAbbreviations", "MARAZMATIC,VARIABLE"); checkConfig.addAttribute("ignoreStatic", "true"); checkConfig.addAttribute("ignoreFinal", "false"); @@ -219,6 +218,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF" + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 6; final String[] expected = { "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount), @@ -238,11 +238,11 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - final int expectedCapitalCount = 3; - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "3"); checkConfig.addAttribute("allowedAbbreviations", ""); checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF"); checkConfig.addAttribute("ignoreOverriddenMethods", "true"); + final int expectedCapitalCount = 4; final String[] expected = { "22: " + getWarningMessage("oveRRRRRrriddenMethod", expectedCapitalCount), @@ -256,9 +256,8 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { public void testTypeNamesForZeroPermittedCapitalLetter() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - final int expectedCapitalCount = 0; checkConfig.addAttribute("allowedAbbreviationLength", - String.valueOf(expectedCapitalCount)); + "0"); checkConfig.addAttribute("allowedAbbreviations", ""); checkConfig.addAttribute("ignoreStatic", "false"); checkConfig.addAttribute("ignoreFinal", "false"); @@ -266,6 +265,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("tokens", "CLASS_DEF,INTERFACE_DEF,ENUM_DEF," + "ANNOTATION_DEF,ANNOTATION_FIELD_DEF,ENUM_CONSTANT_DEF," + "PARAMETER_DEF,VARIABLE_DEF,METHOD_DEF"); + final int expectedCapitalCount = 1; final String[] expected = { "3: " + getWarningMessage("IIIInputAbstractClassName", expectedCapitalCount), "6: " + getWarningMessage("NonAAAAbstractClassName", expectedCapitalCount), @@ -306,8 +306,7 @@ public class AbbreviationAsWordInNameCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(AbbreviationAsWordInNameCheck.class); - final int expectedCapitalCount = 1; - checkConfig.addAttribute("allowedAbbreviationLength", String.valueOf(expectedCapitalCount)); + checkConfig.addAttribute("allowedAbbreviationLength", "2"); checkConfig.addAttribute("ignoreFinal", "false"); checkConfig.addAttribute("allowedAbbreviations", null); diff --git a/src/xdocs/config_naming.xml b/src/xdocs/config_naming.xml index fdf0803e9..c12428be4 100644 --- a/src/xdocs/config_naming.xml +++ b/src/xdocs/config_naming.xml @@ -40,6 +40,17 @@ Google Style Guide to get to know how to avoid long abbreviations in names.

+ +

+ allowedAbbreviationLength specifies how many consecutive capital letters are + allowed in the identifier. + A value of 3 indicates that up to 4 consecutive capital letters are allowed, + one after the other, before a violation is printed. The identifier 'MyTEST' would be + allowed, but 'MyTESTS' would not be. + A value of 0 indicates that only 1 consecutive capital letter is allowed. This + is what should be used to enforce strict camel casing. The identifier 'MyTest' would + be allowed, but 'MyTEst' would not be. +

@@ -52,7 +63,7 @@ allowedAbbreviationLength - indicates on the allowed amount of capital letters in targeted identifiers + indicates on the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). Integer 3 -- cgit v1.2.3