Class AbbreviationAsWordInNameCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- 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
Validates 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.'_' is considered as word separator in identifier name.
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.ignoreFinal
,ignoreStatic
, andignoreStaticFinal
control whether variables with the respective modifiers are to be ignored. Note that a variable that is both static and final will always be considered underignoreStaticFinal
only, regardless of the values ofignoreFinal
andignoreStatic
. So for example ifignoreStatic
is true butignoreStaticFinal
is false, then static final variables will not be ignored.-
Property
allowedAbbreviationLength
- Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). Type isint
. Default value is3
. -
Property
allowedAbbreviations
- Specify abbreviations that must be skipped for checking. Type isjava.lang.String[]
. Default value is""
. -
Property
ignoreFinal
- Allow to skip variables withfinal
modifier. Type isboolean
. Default value istrue
. -
Property
ignoreOverriddenMethods
- Allow to ignore methods tagged with@Override
annotation (that usually mean inherited name). Type isboolean
. Default value istrue
. -
Property
ignoreStatic
- Allow to skip variables withstatic
modifier. Type isboolean
. Default value istrue
. -
Property
ignoreStaticFinal
- Allow to skip variables with bothstatic
andfinal
modifiers. Type isboolean
. Default value istrue
. -
Property
tokens
- tokens to check Type isjava.lang.String[]
. Validation type istokenSet
. Default value is: CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF, RECORD_COMPONENT_DEF.
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
abbreviation.as.word
- Since:
- 5.8
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private int
allowedAbbreviationLength
Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ).private Set<String>
allowedAbbreviations
Specify abbreviations that must be skipped for checking.private static int
DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH
The default value of "allowedAbbreviationLength" option.private boolean
ignoreFinal
Allow to skip variables withfinal
modifier.private boolean
ignoreOverriddenMethods
Allow to ignore methods tagged with@Override
annotation (that usually mean inherited name).private boolean
ignoreStatic
Allow to skip variables withstatic
modifier.private boolean
ignoreStaticFinal
Allow to skip variables with bothstatic
andfinal
modifiers.static 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 static String
getAbbreviation(String str, int beginIndex, int endIndex)
Gets the abbreviation, wherebeginIndex
andendIndex
are inclusive indexes of a sequence of consecutive upper-case characters.private String
getAbbreviationIfIllegal(String str, int beginIndex, int endIndex, int allowedLength)
Get Abbreviation if it is illegal, wherebeginIndex
andendIndex
are inclusive indexes of a sequence of consecutive upper-case characters.int[]
getAcceptableTokens()
The configurable token set.private static 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 String
getDisallowedAbbreviation(String str)
Gets the disallowed abbreviation contained in given String.int[]
getRequiredTokens()
The tokens that this check must be registered for.private boolean
hasIgnoredModifiers(DetailAST modifiers)
Checks if a variable is to be ignored based on its modifiers.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)
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 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
setIgnoreStaticFinal(boolean ignoreStaticFinal)
Setter to allow to skip variables with bothstatic
andfinal
modifiers.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, 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.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final 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
Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ).
-
allowedAbbreviations
private Set<String> allowedAbbreviations
Specify abbreviations that must be skipped for checking.
-
ignoreFinal
private boolean ignoreFinal
Allow to skip variables withfinal
modifier.
-
ignoreStatic
private boolean ignoreStatic
Allow to skip variables withstatic
modifier.
-
ignoreStaticFinal
private boolean ignoreStaticFinal
Allow to skip variables with bothstatic
andfinal
modifiers.
-
ignoreOverriddenMethods
private boolean ignoreOverriddenMethods
Allow to ignore methods tagged with@Override
annotation (that usually mean inherited name).
-
-
Constructor Detail
-
AbbreviationAsWordInNameCheck
public AbbreviationAsWordInNameCheck()
-
-
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.- Since:
- 5.8
-
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.- Since:
- 5.8
-
setIgnoreStaticFinal
public void setIgnoreStaticFinal(boolean ignoreStaticFinal)
Setter to allow to skip variables with bothstatic
andfinal
modifiers.- Parameters:
ignoreStaticFinal
- Defines if ignore variables with both 'static' and 'final' modifiers or not.- Since:
- 8.32
-
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.- Since:
- 5.8
-
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.- Since:
- 5.8
-
setAllowedAbbreviations
public void setAllowedAbbreviations(String... allowedAbbreviations)
Setter to specify abbreviations that must be skipped for checking.- Parameters:
allowedAbbreviations
- abbreviations that must be skipped from checking.- Since:
- 5.8
-
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.
-
hasIgnoredModifiers
private boolean hasIgnoredModifiers(DetailAST modifiers)
Checks if a variable is to be ignored based on its modifiers.- Parameters:
modifiers
- modifiers of the variable to be checked- Returns:
- true if there is a modifier to be ignored
-
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 String getDisallowedAbbreviation(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 String getAbbreviationIfIllegal(String str, int beginIndex, int endIndex, int allowedLength)
Get Abbreviation if it is illegal, wherebeginIndex
andendIndex
are inclusive indexes of a sequence of consecutive upper-case characters.- Parameters:
str
- namebeginIndex
- begin indexendIndex
- end indexallowedLength
- maximum allowed length for Abbreviation- Returns:
- the abbreviation if it is bigger than required and not in the
ignore list, otherwise
null
-
getAbbreviation
private static String getAbbreviation(String str, int beginIndex, int endIndex)
Gets the abbreviation, wherebeginIndex
andendIndex
are inclusive indexes of a sequence of consecutive upper-case characters.The character at
endIndex
is only included in the abbreviation if it is the last character in the string; otherwise it is usually the first capital in the next word.For example,
getAbbreviation("getXMLParser", 3, 6)
returns "XML" (not "XMLP"), and so doesgetAbbreviation("parseXML", 5, 7)
.- Parameters:
str
- namebeginIndex
- begin indexendIndex
- end index- Returns:
- the specified abbreviation
-
getChildren
private static List<DetailAST> getChildren(DetailAST node)
Gets all the children which are one level below on the current DetailAST parent node.- Parameters:
node
- Current parent node.- Returns:
- The list of children one level below on the current parent node.
-
-