Class AbbreviationAsWordInNameCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public class AbbreviationAsWordInNameCheck extends AbstractCheck
The Check validate abbreviations(consecutive capital letters) length in identifier name, it also allows to enforce camel case naming. Please read more at 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.-
Property
allowedAbbreviationLength
- Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). Default value is3
. -
Property
allowedAbbreviations
- Specify list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma. Default value is{}
. -
Property
ignoreFinal
- Allow to skip variables withfinal
modifier. Default value istrue
. -
Property
ignoreStatic
- Allow to skip variables withstatic
modifier. Default value istrue
. -
Property
ignoreOverriddenMethods
- Allow to ignore methods tagged with@Override
annotation (that usually mean inherited name). Default value istrue
. -
Property
tokens
- tokens to check Default value is: CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF.
Default configuration
<module name="AbbreviationAsWordInName"/>
To configure to check all variables and identifiers (including ones with the static modifier) and enforce no abbreviations (essentially camel case) except for words like 'XML' and 'URL'.
Configuration:
<module name="AbbreviationAsWordInName"> <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/> <property name="ignoreStatic" value="false"/> <property name="allowedAbbreviationLength" value="0"/> <property name="allowedAbbreviations" value="XML,URL"/> </module>
Example:
public class MyClass { // OK int firstNum; // OK int secondNUM; // violation, it allowed only 1 consecutive capital letter static int thirdNum; // OK, the static modifier would be checked static int fourthNUm; // violation, the static modifier would be checked, // and only 1 consecutive capital letter is allowed String firstXML; // OK, XML abbreviation is allowed String firstURL; // OK, URL abbreviation is allowed }
To configure to check variables, excluding fields with the static modifier, and allow abbreviations up to 2 consecutive capital letters ignoring the longer word 'CSV'.
Configuration:
<module name="AbbreviationAsWordInName"> <property name="tokens" value="VARIABLE_DEF"/> <property name="ignoreStatic" value="true"/> <property name="allowedAbbreviationLength" value="1"/> <property name="allowedAbbreviations" value="CSV"/> </module>
Example:
public class MyClass { // OK, ignore checking the class name int firstNum; // OK, abbreviation "N" is of allowed length 1 int secondNUm; // OK int secondMYNum; // violation, found "MYN" but only // 2 consecutive capital letters are allowed int thirdNUM; // violation, found "NUM" but it is allowed // only 2 consecutive capital letters static int fourthNUM; // OK, variables with static modifier // would be ignored String firstCSV; // OK, CSV abbreviation is allowed String firstXML; // violation, XML abbreviation is not allowed }
- Since:
- 5.8
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Constructor Summary
Constructors Constructor Description AbbreviationAsWordInNameCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setAllowedAbbreviationLength(int allowedAbbreviationLength)
Setter to indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ...void
setAllowedAbbreviations(String... allowedAbbreviations)
Setter to specify list of abbreviations that must be skipped for checking.void
setIgnoreFinal(boolean ignoreFinal)
Setter to allow to skip variables withfinal
modifier.void
setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods)
Setter to allow to ignore methods tagged with@Override
annotation (that usually mean inherited name).void
setIgnoreStatic(boolean ignoreStatic)
Setter to allow to skip variables withstatic
modifier.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Warning message key.- See Also:
- Constant Field Values
-
-
Method Detail
-
setIgnoreFinal
public void setIgnoreFinal(boolean ignoreFinal)
Setter to allow to skip variables withfinal
modifier.- Parameters:
ignoreFinal
- Defines if ignore variables with 'final' modifier or not.
-
setIgnoreStatic
public void setIgnoreStatic(boolean ignoreStatic)
Setter to allow to skip variables withstatic
modifier.- Parameters:
ignoreStatic
- Defines if ignore variables with 'static' modifier or not.
-
setIgnoreOverriddenMethods
public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods)
Setter to allow to ignore methods tagged with@Override
annotation (that usually mean inherited name).- Parameters:
ignoreOverriddenMethods
- Defines if ignore methods with "@Override" annotation or not.
-
setAllowedAbbreviationLength
public void setAllowedAbbreviationLength(int allowedAbbreviationLength)
Setter to indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ).- Parameters:
allowedAbbreviationLength
- amount of allowed capital letters in abbreviation.
-
setAllowedAbbreviations
public void setAllowedAbbreviations(String... allowedAbbreviations)
Setter to specify list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma.- Parameters:
allowedAbbreviations
- an string of abbreviations that must be skipped from checking, each abbreviation separated by comma.
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
-