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.
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, and ignoreStaticFinal
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 under
ignoreStaticFinal only, regardless of the values of ignoreFinal
and ignoreStatic. So for example if ignoreStatic is true but
ignoreStaticFinal is false, then static final variables will not be ignored.
allowedAbbreviationLength - Indicate the number of consecutive capital
letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables
and methods names, ... ).
Type is int.
Default value is 3.
allowedAbbreviations - Specify list of abbreviations that must be skipped for
checking. Abbreviations should be separated by comma.
Type is java.lang.String[].
Default value is "".
ignoreFinal - Allow to skip variables with final modifier.
Type is boolean.
Default value is true.
ignoreStatic - Allow to skip variables with static modifier.
Type is boolean.
Default value is true.
ignoreStaticFinal - Allow to skip variables with both static and
final modifiers.
Type is boolean.
Default value is true.
ignoreOverriddenMethods - Allow to ignore methods tagged with @Override
annotation (that usually mean inherited name).
Type is boolean.
Default value is true.
tokens - tokens to check
Type is java.lang.String[].
Validation type is tokenSet.
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.
To configure the check:
<module name="AbbreviationAsWordInName"/>
Example:
public class MyClass extends SuperClass { // OK, camel case
int CURRENT_COUNTER; // violation, at most 4 consecutive capital letters allowed
static int GLOBAL_COUNTER; // OK, static is ignored
final Set<String> stringsFOUND = new HashSet<>(); // OK, final is ignored
@Override
void printCOUNTER() { // OK, overridden method is ignored
System.out.println(CURRENT_COUNTER); // OK, only definitions are checked
}
void incrementCOUNTER() { // violation, at most 4 consecutive capital letters allowed
CURRENT_COUNTER++; // OK, only definitions are checked
}
static void incrementGLOBAL() { // violation, static method is not ignored
GLOBAL_COUNTER++; // OK, only definitions are checked
}
}
To configure to include static variables and methods tagged with
@Override annotation.
Configuration:
<module name="AbbreviationAsWordInName"> <property name="ignoreStatic" value="false"/> <property name="ignoreOverriddenMethods" value="false"/> </module>
Example:
public class MyClass extends SuperClass { // OK, camel case
int CURRENT_COUNTER; // violation, at most 4 consecutive capital letters allowed
static int GLOBAL_COUNTER; // violation, static is not ignored
final Set<String> stringsFOUND = new HashSet<>(); // OK, final is ignored
@Override
void printCOUNTER() { // violation, overridden method is not ignored
System.out.println(CURRENT_COUNTER); // OK, only definitions are checked
}
void incrementCOUNTER() { // violation, at most 4 consecutive capital letters allowed
CURRENT_COUNTER++; // OK, only definitions are checked
}
static void incrementGLOBAL() { // violation, at most 4 consecutive capital letters allowed
GLOBAL_COUNTER++; // OK, only definitions are checked
}
}
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
final int TOTAL = 5; // OK, final is ignored
static final int LIMIT = 10; // OK, static final is ignored
}
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
final int TOTAL = 5; // OK, final is ignored
static final int LIMIT = 10; // OK, static final is ignored
}
To configure to check variables, enforcing no abbreviations except for variables that are both static and final.
Configuration:
<module name="AbbreviationAsWordInName">
<property name="tokens" value="VARIABLE_DEF"/>
<property name="ignoreFinal" value="false"/>
<property name="ignoreStatic" value="false"/>
<property name="ignoreStaticFinal" value="true"/>
<property name="allowedAbbreviationLength" value="0"/>
</module>
Example:
public class MyClass {
public int counterXYZ = 1; // violation
public final int customerID = 2; // violation
public static int nextID = 3; // violation
public static final int MAX_ALLOWED = 4; // OK, ignored
}
To configure to check variables, enforcing no abbreviations and ignoring static (but non-final) variables only.
Configuration:
<module name="AbbreviationAsWordInName">
<property name="tokens" value="VARIABLE_DEF"/>
<property name="ignoreFinal" value="false"/>
<property name="ignoreStatic" value="true"/>
<property name="ignoreStaticFinal" value="false"/>
<property name="allowedAbbreviationLength" value="0"/>
</module>
Example:
public class MyClass {
public int counterXYZ = 1; // violation
public final int customerID = 2; // violation
public static int nextID = 3; // OK, ignored
public static final int MAX_ALLOWED = 4; // violation
}
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
abbreviation.as.word
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and 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 java.util.Set<java.lang.String> |
allowedAbbreviations
Specify list of 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 with
final modifier. |
private boolean |
ignoreOverriddenMethods
Allow to ignore methods tagged with
@Override annotation (that
usually mean inherited name). |
private boolean |
ignoreStatic
Allow to skip variables with
static modifier. |
private boolean |
ignoreStaticFinal
Allow to skip variables with both
static and final modifiers. |
static java.lang.String |
MSG_KEY
Warning message key.
|
| Constructor and Description |
|---|
AbbreviationAsWordInNameCheck() |
| Modifier and Type | Method and Description |
|---|---|
private static java.lang.String |
getAbbreviation(java.lang.String str,
int beginIndex,
int endIndex)
Gets the abbreviation, where
beginIndex and endIndex are
inclusive indexes of a sequence of consecutive upper-case characters. |
private java.lang.String |
getAbbreviationIfIllegal(java.lang.String str,
int beginIndex,
int endIndex)
Get Abbreviation if it is illegal, where
beginIndex and endIndex are
inclusive indexes of a sequence of consecutive upper-case characters. |
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 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(java.lang.String... allowedAbbreviations)
Setter to specify list of abbreviations that must be skipped for checking.
|
void |
setIgnoreFinal(boolean ignoreFinal)
Setter to allow to skip variables with
final 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 with
static modifier. |
void |
setIgnoreStaticFinal(boolean ignoreStaticFinal)
Setter to allow to skip variables with both
static and final modifiers. |
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final int DEFAULT_ALLOWED_ABBREVIATIONS_LENGTH
private int allowedAbbreviationLength
private java.util.Set<java.lang.String> allowedAbbreviations
private boolean ignoreFinal
final modifier.private boolean ignoreStatic
static modifier.private boolean ignoreStaticFinal
static and final modifiers.private boolean ignoreOverriddenMethods
@Override annotation (that
usually mean inherited name).public AbbreviationAsWordInNameCheck()
public void setIgnoreFinal(boolean ignoreFinal)
final modifier.ignoreFinal - Defines if ignore variables with 'final' modifier or not.public void setIgnoreStatic(boolean ignoreStatic)
static modifier.ignoreStatic - Defines if ignore variables with 'static' modifier or not.public void setIgnoreStaticFinal(boolean ignoreStaticFinal)
static and final modifiers.ignoreStaticFinal - Defines if ignore variables with both 'static' and 'final' modifiers or not.public void setIgnoreOverriddenMethods(boolean ignoreOverriddenMethods)
@Override
annotation (that usually mean inherited name).ignoreOverriddenMethods - Defines if ignore methods with "@Override" annotation or not.public void setAllowedAbbreviationLength(int allowedAbbreviationLength)
allowedAbbreviationLength - amount of allowed capital letters in
abbreviation.public void setAllowedAbbreviations(java.lang.String... allowedAbbreviations)
allowedAbbreviations - an string of abbreviations that must be
skipped from checking, each abbreviation separated by comma.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate boolean isIgnoreSituation(DetailAST ast)
ast - input DetailAST node.private boolean hasIgnoredModifiers(DetailAST modifiers)
modifiers - modifiers of the variable to be checkedprivate static boolean isInterfaceDeclaration(DetailAST variableDefAst)
variableDefAst - variable definition.private static boolean hasOverrideAnnotation(DetailAST methodModifiersAST)
methodModifiersAST - A DetailAST nod is related to the given method modifiers
(MODIFIERS type).private java.lang.String getDisallowedAbbreviation(java.lang.String str)
str - the given String.private java.lang.String getAbbreviationIfIllegal(java.lang.String str, int beginIndex, int endIndex)
beginIndex and endIndex are
inclusive indexes of a sequence of consecutive upper-case characters.str - namebeginIndex - begin indexendIndex - end indexnullprivate static java.lang.String getAbbreviation(java.lang.String str, int beginIndex, int endIndex)
beginIndex and endIndex 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 does getAbbreviation("parseXML", 5, 7).
str - namebeginIndex - begin indexendIndex - end indexprivate static java.util.List<DetailAST> getChildren(DetailAST node)
node - Current parent node.Copyright © 2001-2022. All Rights Reserved.