public class NumericLiteralNeedsUnderscoreCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
This check verifies that large numeric literals are spaced by underscores.
Java 7 allows for underscores to delimit numeric literals to enhance readability because very large numeric literals are hard to read. For example:
long creditCardNumber = 1234_5678_1234_5678L;
is much easier to read than
long creditCardNumber = 1234567812345678L;
This check comes with the following parameters:
"minDecimalSymbolLength" - The minimum number of symbols in a decimal literal (includes int, long, float, and double) before the check begins to look for underscores. Numeric literals with delimiters like decimal points or exponentials will be split before checking the length. Default is 7.
"maxDecimalSymbolsUntilUnderscore" - The maximum number of symbols in a decimal literal allowed before the check demands an underscore. This does not take postfixes and delimiters like decimal points and exponentials into account. Default is 3.
"minHexSymbolLength" - The minimum number of symbols in a hex literal before the check begins to look for underscores. Numeric literals with delimiters like decimal points or exponentials will be split before checking the length. Default is 5.
"maxHexSymbolsUntilUnderscore" - The maximum number of symbols in a hex literal allowed before the check demands an underscore. This does not take the prefix 0x, delimiters like decimal points and exponentials, and postfixes into account. Default is 4.
"minBinarySymbolLength" - The minimum number of symbols in a binary literal before the check begins to look for underscores. Default is 9.
"maxBinarySymbolsUntilUnderscore" - The maximum number of symbols in a binary literal allowed before the check demands an underscore. This does not take the prefix 0b and postfixes into account. Default is 8.
Examples (assuming default parameters):
// Ignored because length of token is 6, which is less than minDecimalSymbolLength (7) int ignoredDecimal = 123456; // Ignored because the postfix L is not taken into account long ignoredDecimal = 123456L; // Ignored because each segment delimited by the decimal point has a length // less than minDecimalSymbolLength (7) float ignoredDecimal = 123456.123456f; // Failed because token does not have underscores every 3 characters // (maxDecimalSymbolsUntilUnderscore = 3) int failingDecimal = 1234567; double failingDecimal = 1.1234567e0d; int passingDecimal = 1_234_567; double passingDecimal = 123456.123456e0d; double passingDecimal = 1.123_456_7e0d; int ignoredHex = 0xFFFF; int failingHex = 0xFFFFFFFF; int passingHex = 0xFFFF_FFFF; float passingHex = 0xAAAA.BBBBp1f; int ignoredBinary = 0b01010101; int failingBinary = 0b0000000000000000; int passingBinary = 0b00000000_00000000;
An example of how to configure parameters:
<module name="NumericLiteralNeedsUnderscoreCheck">
<property name="minDecimalSymbolLength" value="4"/>
<property name="maxDecimalSymbolsUntilUnderscore" value="3"/>
<property name="minHexSymbolLength" value="3"/>
<property name="maxHexSymbolsUntilUnderscore" value="2"/>
<property name="minBinarySymbolLength" value="5"/>
<property name="maxBinarySymbolsUntilUnderscore" value="4"/>
</module>
Examples (assuming above parameters):
// Ignored because length of token is 3, which is less than minDecimalSymbolLength (4) int ignoredDecimal = 123; // Ignored because each segment delimited by the decimal point has a length // less than minDecimalSymbolLength (4) float ignoredDecimal = 123.123f; // Failed because token does not have underscores every 3 characters // (maxDecimalSymbolsUntilUnderscore = 3) int failingDecimal = 1234; int passingDecimal = 1_234; int ignoredHex = 0xFF; int failingHex = 0xFFFF; int passingHex = 0xFF_FF; int ignoredBinary = 0b0101; int failingBinary = 0b00001111; int passingBinary = 0b0000_1111;
| Modifier and Type | Class and Description |
|---|---|
protected static class |
NumericLiteralNeedsUnderscoreCheck.NumericType
Type of numeric literal.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
MSG_KEY
Key for error message.
|
| Constructor and Description |
|---|
NumericLiteralNeedsUnderscoreCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
setIgnoreFieldNamePattern(String pattern)
Sets the regexp pattern for field names to ignore.
|
void |
setMaxBinarySymbolsUntilUnderscore(int amount)
Sets how many characters there can be until there must be an underscore (binary
literals).
|
void |
setMaxDecimalSymbolsUntilUnderscore(int amount)
Sets how many characters there can be until there must be an underscore (for decimal
literals).
|
void |
setMaxHexSymbolsUntilUnderscore(int amount)
Sets how many characters there can be until there must be an underscore (for hex
literals).
|
void |
setMinBinarySymbolLength(int length)
Sets how many characters in a byte literal there must be before it checks for an
underscore.
|
void |
setMinDecimalSymbolLength(int length)
Sets how many characters in a decimal literal there must be before it checks for an
underscore.
|
void |
setMinHexSymbolLength(int length)
Sets how many characters in a hex literal there must be before it checks for an
underscore.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY
public void setMinDecimalSymbolLength(int length)
length - minimum checking length of the literalpublic void setMaxDecimalSymbolsUntilUnderscore(int amount)
amount - maximum number of characters between underscorespublic void setMinHexSymbolLength(int length)
length - minimum checking length of the literalpublic void setMaxHexSymbolsUntilUnderscore(int amount)
amount - maximum number of characters between underscorespublic void setMinBinarySymbolLength(int length)
length - minimum checking length of the literalpublic void setMaxBinarySymbolsUntilUnderscore(int amount)
amount - maximum number of characters between underscorespublic void setIgnoreFieldNamePattern(String pattern)
pattern - the regexp pattern of fields to ignorepublic int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckCopyright © 2021. All rights reserved.