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.Option
allowedAbbreviationLength
indicates on the allowed amount of capital letters in abbreviations in the classes, interfaces, variables and methods names. Default value is '3'.Option
allowedAbbreviations
- list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma, no spaces are allowed.Option
ignoreFinal
allow to skip variables withfinal
modifier. Default value istrue
.Option
ignoreStatic
allow to skip variables withstatic
modifier. Default value istrue
.Option
Default configurationignoreOverriddenMethod
- Allows to ignore methods tagged with@Override
annotation (that usually mean inherited name). Default value istrue
.<module name="AbbreviationAsWordInName" />
To configure to check variables and classes identifiers, do not ignore variables with static modifier and allow no abbreviations (enforce camel case phrase) but allow XML and URL abbreviations.
<module name="AbbreviationAsWordInName"> <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/> <property name="ignoreStatic" value="false"/> <property name="allowedAbbreviationLength" value="1"/> <property name="allowedAbbreviations" value="XML,URL"/> </module>
-
-
Field Summary
Fields Modifier and Type Field Description private int
allowedAbbreviationLength
Variable indicates on the allowed amount of capital letters in abbreviations in the classes, interfaces, variables and methods names.private java.util.Set<java.lang.String>
allowedAbbreviations
Set of allowed abbreviation to ignore in check.private static int
DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH
The default value of "allowedAbbreviationLength" option.private boolean
ignoreFinal
Allows to ignore variables with 'final' modifier.private boolean
ignoreOverriddenMethods
Allows to ignore methods with '@Override' annotation.private boolean
ignoreStatic
Allows to ignore variables with 'static' modifier.static java.lang.String
MSG_KEY
Warning message key.
-
Constructor Summary
Constructors Constructor Description AbbreviationAsWordInNameCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
getAbbreviationIfIllegal(java.lang.String str, int beginIndex, int endIndex)
Get Abbreviation if it is illegal.int[]
getAcceptableTokens()
The configurable token set.private static java.util.List<DetailAST>
getChildren(DetailAST node)
Gets all the children which are one level below on the current DetailAST parent node.int[]
getDefaultTokens()
Returns the default token a check is interested in.private java.lang.String
getDisallowedAbbreviation(java.lang.String str)
Gets the disallowed abbreviation contained in given String.int[]
getRequiredTokens()
The tokens that this check must be registered for.private static boolean
hasOverrideAnnotation(DetailAST methodModifiersAST)
Checks that the method has "@Override" annotation.private boolean
isIgnoreSituation(DetailAST ast)
Checks if it is an ignore situation.private static boolean
isInterfaceDeclaration(DetailAST variableDefAst)
Check that variable definition in interface or @interface definition.void
setAllowedAbbreviationLength(int allowedAbbreviationLength)
Allowed abbreviation length in names.void
setAllowedAbbreviations(java.lang.String... allowedAbbreviations)
Set a list of abbreviations that must be skipped for checking.void
setIgnoreFinal(boolean ignoreFinal)
Sets ignore option for variables with 'final' modifier.void
setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods)
Sets ignore option for methods with "@Override" annotation.void
setIgnoreStatic(boolean ignoreStatic)
Sets ignore option for variables with 'static' modifier.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
Warning message key.- See Also:
- Constant Field Values
-
DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH
private static final int DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH
The default value of "allowedAbbreviationLength" option.- See Also:
- Constant Field Values
-
allowedAbbreviationLength
private int allowedAbbreviationLength
Variable indicates on the allowed amount of capital letters in abbreviations in the classes, interfaces, variables and methods names.
-
allowedAbbreviations
private java.util.Set<java.lang.String> allowedAbbreviations
Set of allowed abbreviation to ignore in check.
-
ignoreFinal
private boolean ignoreFinal
Allows to ignore variables with 'final' modifier.
-
ignoreStatic
private boolean ignoreStatic
Allows to ignore variables with 'static' modifier.
-
ignoreOverriddenMethods
private boolean ignoreOverriddenMethods
Allows to ignore methods with '@Override' annotation.
-
-
Method Detail
-
setIgnoreFinal
public void setIgnoreFinal(boolean ignoreFinal)
Sets ignore option for variables with 'final' modifier.- Parameters:
ignoreFinal
- Defines if ignore variables with 'final' modifier or not.
-
setIgnoreStatic
public void setIgnoreStatic(boolean ignoreStatic)
Sets ignore option for variables with 'static' modifier.- Parameters:
ignoreStatic
- Defines if ignore variables with 'static' modifier or not.
-
setIgnoreOverriddenMethods
public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods)
Sets ignore option for methods with "@Override" annotation.- Parameters:
ignoreOverriddenMethods
- Defines if ignore methods with "@Override" annotation or not.
-
setAllowedAbbreviationLength
public void setAllowedAbbreviationLength(int allowedAbbreviationLength)
Allowed abbreviation length in names.- Parameters:
allowedAbbreviationLength
- amount of allowed capital letters in abbreviation.
-
setAllowedAbbreviations
public void setAllowedAbbreviations(java.lang.String... allowedAbbreviations)
Set a list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma, no spaces is allowed.- 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
-
isIgnoreSituation
private boolean isIgnoreSituation(DetailAST ast)
Checks if it is an ignore situation.- Parameters:
ast
- input DetailAST node.- Returns:
- true if it is an ignore situation found for given input DetailAST node.
-
isInterfaceDeclaration
private static boolean isInterfaceDeclaration(DetailAST variableDefAst)
Check that variable definition in interface or @interface definition.- Parameters:
variableDefAst
- variable definition.- Returns:
- true if variable definition(variableDefAst) is in interface or @interface definition.
-
hasOverrideAnnotation
private static boolean hasOverrideAnnotation(DetailAST methodModifiersAST)
Checks that the method has "@Override" annotation.- Parameters:
methodModifiersAST
- A DetailAST nod is related to the given method modifiers (MODIFIERS type).- Returns:
- true if method has "@Override" annotation.
-
getDisallowedAbbreviation
private java.lang.String getDisallowedAbbreviation(java.lang.String str)
Gets the disallowed abbreviation contained in given String.- Parameters:
str
- the given String.- Returns:
- the disallowed abbreviation contained in given String as a separate String.
-
getAbbreviationIfIllegal
private java.lang.String getAbbreviationIfIllegal(java.lang.String str, int beginIndex, int endIndex)
Get Abbreviation if it is illegal.- Parameters:
str
- namebeginIndex
- begin indexendIndex
- end index- Returns:
- true is abbreviation is bigger that required and not in ignore list
-
-