forked from sevntu-checkstyle/sevntu.checkstyle
/
NumericLiteralNeedsUnderscoreCheck.java
429 lines (399 loc) · 12.7 KB
/
NumericLiteralNeedsUnderscoreCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.github.sevntu.checkstyle.checks.coding;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* <p>
* This check verifies that large numeric literals are spaced by underscores.
* </p>
* <p>
* Java 7 allows for underscores to delimit numeric literals to enhance readability because very
* large numeric literals are hard to read. For example:
* </p>
* <code>
* long creditCardNumber = 1234_5678_1234_5678L;
* </code>
* <p>
* is much easier to read than
* </p>
* <code>
* long creditCardNumber = 1234567812345678L;
* </code>
* <p>
* This check comes with the following parameters:
* </p>
* <p>
* "minDecimalSymbolLength" - The minimum number of symbols in a decimal literal before the check
* begins to look for underscores. For example, if this is set to 7, then the following code will
* pass because it is not yet of length 7 and will be ignored:
* </p>
* <p>
* <code>
* int i = 123456;
* </code>
* </p>
* <p>
* "maxDecimalSymbolsUntilUnderscore" - The maximum number of symbols in a decimal literal allowed
* before the check demands an underscore. For example, if this is set to 3 and
* minDecimalSymbolLength is set to 7, then the first line will fail and the second will pass:
* </p>
* <p>
* <code>
* int failing = 1234567;<br/>
* int passing = 1_234_567;
* </code>
* </p>
* <p>
* "minHexSymbolLength" - The minimum number of symbols in a hex literal before the check begins to
* look for underscores. For example, if this is set to 5, then the following code will pass because
* it is not yet of length 5 and will be ignored:
* </p>
* <p>
* <code>
* int i = 0xFFFF;
* </code>
* </p>
* <p>
* "maxHexSymbolsUntilUnderscore" - The maximum number of symbols in a hex literal allowed before
* the check demands an underscore. For example, if this is set to 4 and minHexSymbolLength is set
* to 5, then the first line will fail and the second will pass:
* </p>
* <p>
* <code>
* int failing = 0xFFFFFFFF;<br/>
* int passing = 0xFFFF_FFFF;
* </code>
* </p>
* <p>
* "minBinarySymbolLength" - The minimum number of symbols in a binary literal before the check
* begins to look for underscores. For example, if this is set to 9, then the following code will
* pass because it is not yet of length 9 and will be ignored:
* </p>
* <p>
* <code>
* int i = 0b01010101;
* </code>
* </p>
* <p>
* "maxBinarySymbolsUntilUnderscore" - The maximum number of symbols in a decimal literal allowed
* before the check demands an underscore. For example, if this is set to 8 and
* minBinarySymbolLength is set to 9, then the first line will fail and the second will pass:
* </p>
* <p>
* <code>
* int failing = 0b0000000000000000;<br/>
* int passing = 0b00000000_00000000;
* </code>
* </p>
* <p>
* An example of how to configure parameters:
* </p>
*
* <pre>
* <module name="NumericLiteralNeedsUnderscoreCheck">
* <property name="minDecimalSymbolLength" value="7"/>
* <property name="maxDecimalSymbolsUntilUnderscore" value="3"/>
* <property name="minHexSymbolLength" value="5"/>
* <property name="maxHexSymbolsUntilUnderscore" value="4"/>
* <property name="minBinarySymbolLength" value="9"/>
* <property name="maxBinarySymbolsUntilUnderscore" value="8"/>
* </module>
* </pre>
* @author Cheng-Yu Pai
*/
public class NumericLiteralNeedsUnderscoreCheck extends Check
{
/**
* Key for error message.
*/
public static final String MSG_KEY = "numeric.literal.need.underscore";
private static enum Type
{
DECIMAL, HEX, BINARY;
}
private static final int DEFAULT_MIN_DECIMAL_SYMBOL_LEN = 7;
private static final int DEFAULT_MAX_DECIMAL_SYMBOLS_UNTIL_UNDERSCORE = 3;
private static final int DEFAULT_MIN_HEX_SYMBOL_LEN = 5;
private static final int DEFAULT_MAX_HEX_SYMBOLS_UNTIL_UNDERSCORE = 4;
private static final int DEFAULT_MIN_BINARY_SYMBOL_LEN = 9;
private static final int DEFAULT_MAX_BINARY_SYMBOLS_UNTIL_UNDERSCORE = 8;
private int minDecimalSymbolLength = DEFAULT_MIN_DECIMAL_SYMBOL_LEN;
private int maxDecimalSymbolsUntilUnderscore = DEFAULT_MAX_DECIMAL_SYMBOLS_UNTIL_UNDERSCORE;
private int minHexSymbolLength = DEFAULT_MIN_HEX_SYMBOL_LEN;
private int maxHexSymbolsUntilUnderscore = DEFAULT_MAX_HEX_SYMBOLS_UNTIL_UNDERSCORE;
private int minBinarySymbolLength = DEFAULT_MIN_BINARY_SYMBOL_LEN;
private int maxBinarySymbolsUntilUnderscore = DEFAULT_MAX_BINARY_SYMBOLS_UNTIL_UNDERSCORE;
/**
* Sets how many characters in a literal there must be before it checks for an underscore
*/
public void setMinDecimalSymbolLen(int len)
{
minDecimalSymbolLength = len;
}
/**
* Sets how many characters there can be until there must be an underscore (for numeric
* literals)
*/
public void setMaxDecimalSymbolsUntilUnderscore(int amount)
{
maxDecimalSymbolsUntilUnderscore = amount;
}
/**
* Sets how many characters in a hex literal there must be before it checks for an underscore
*/
public void setMinHexSymbolLen(int len)
{
minHexSymbolLength = len;
}
/**
* Sets how many characters there can be until there must be an underscore (for hex literals)
*/
public void setMaxHexSymbolsUntilUnderscore(int amount)
{
maxHexSymbolsUntilUnderscore = amount;
}
/**
* Sets how many characters in a byte literal there must be before it checks for an underscore
*/
public void setMinBinarySymbolLen(int len)
{
minBinarySymbolLength = len;
}
/**
* Sets how many characters there can be until there must be an underscore (for byte literals)
*/
public void setMaxBinarySymbolsUntilUnderscore(int amount)
{
maxBinarySymbolsUntilUnderscore = amount;
}
@Override
public int[] getDefaultTokens()
{
return new int[] {
TokenTypes.NUM_INT,
TokenTypes.NUM_LONG,
TokenTypes.NUM_FLOAT,
TokenTypes.NUM_DOUBLE
};
}
@Override
public void visitToken(final DetailAST ast)
{
if (!passesCheck(ast.getText())) {
log(ast.getLineNo(), MSG_KEY);
}
}
/**
* Returns true if the numeric literal passes the check
*/
private boolean passesCheck(String rawLiteral)
{
int minCheckingLength = minSymbolsBeforeChecking(rawLiteral);
int symbolsUntilUnderscore = maxSymbolsUntilUnderscore(rawLiteral);
String[] tokens = generateStrippedTokens(rawLiteral);
boolean passing = true;
for (String token : tokens) {
if (!tokenPassesRequirement(token, minCheckingLength, symbolsUntilUnderscore)) {
passing = false;
break;
}
}
return passing;
}
/**
* <p>
* Returns the type of numeric literal given the raw text.
* </p>
* <p>
* Decimal literals are normal numbers. Example: 1, 1.0, 1.0e0f, 1L
* </p>
* <p>
* Hex literals are preceded by 0x. Example: 0xDEADBEEF, 0xA.Bp1d
* </p>
* <p>
* Binary literals are preceded by 0b. Example: 0b00001111
* </p>
*/
private Type getNumericType(String rawLiteral)
{
Type type;
if (rawLiteral.length() < 2) {
type = Type.DECIMAL;
}
else {
String prefix = rawLiteral.substring(0, 2);
if (prefix.equals("0x")) {
type = Type.HEX;
}
else if (prefix.equals("0b")) {
type = Type.BINARY;
}
else {
type = Type.DECIMAL;
}
}
return type;
}
/**
* <p>
* Generates easily checkable tokens from the raw literal text.
* </p>
* <p>
* For example: 123.4567 passes because each section itself is not too long and is perfectly
* readable.
* </p>
* Additionally, Java will not compile underscores next to decimal points etc.
* @param rawLiteral
* @return easily checkable tokens
*/
private String[] generateStrippedTokens(String rawLiteral)
{
Type type = getNumericType(rawLiteral);
String strippedLiteral = removePrePostfixByType(rawLiteral, type);
String[] tokens;
switch (type) {
case DECIMAL:
tokens = strippedLiteral.split("[\\.eE]");
break;
case HEX:
tokens = strippedLiteral.split("[\\.pP]");
break;
case BINARY:
// Binary literals do not need splitting, fall through to default
default:
tokens = new String[1];
tokens[0] = strippedLiteral;
break;
}
return tokens;
}
private int minSymbolsBeforeChecking(String rawLiteral)
{
Type type = getNumericType(rawLiteral);
int minLength;
switch (type) {
case DECIMAL:
minLength = minDecimalSymbolLength;
break;
case HEX:
minLength = minHexSymbolLength;
break;
case BINARY:
minLength = minBinarySymbolLength;
break;
default:
throw new IllegalStateException("Unexpected numeric type " + type.toString());
}
return minLength;
}
private int maxSymbolsUntilUnderscore(String rawLiteral)
{
Type type = getNumericType(rawLiteral);
int maxSymbols;
switch (type) {
case DECIMAL:
maxSymbols = maxDecimalSymbolsUntilUnderscore;
break;
case HEX:
maxSymbols = maxHexSymbolsUntilUnderscore;
break;
case BINARY:
maxSymbols = maxBinarySymbolsUntilUnderscore;
break;
default:
throw new IllegalStateException("Unexpected numeric type " + type.toString());
}
return maxSymbols;
}
/**
* Returns whether or not the text passes the underscore requirement given the text and minimum
* length
*/
private boolean tokenPassesRequirement(String token, int minLength, int symbolsUntilUnderscore)
{
if (token.length() < minLength) {
return true;
}
final char underscore = '_';
int symbolCount = 0;
boolean passes = true;
for (int i = 0; i < token.length(); i++) {
char current = token.charAt(i);
if (symbolCount >= symbolsUntilUnderscore && current != underscore) {
passes = false;
break;
}
if (current == underscore) {
symbolCount = 0;
}
else {
symbolCount++;
}
}
return passes;
}
/**
* Removes 0x, 0b prefixes, and l, L, f, F, d, D postfixes from numeric literals
*/
private String removePrePostfixByType(String rawLiteral, Type literalType)
{
String processedLiteral;
switch (literalType) {
case DECIMAL:
processedLiteral = removeLetterPostfix(rawLiteral);
break;
case HEX:
processedLiteral = removePrefix(rawLiteral);
processedLiteral = removePostfixHex(processedLiteral);
break;
case BINARY:
processedLiteral = removePrefix(rawLiteral);
processedLiteral = removeLetterPostfix(processedLiteral);
break;
default:
processedLiteral = rawLiteral;
break;
}
return processedLiteral;
}
private String removePrefix(String text)
{
return text.substring(2);
}
private String removeLetterPostfix(String text)
{
char lastchar = text.charAt(text.length() - 1);
String noPostfixText;
if (Character.isDigit(lastchar)) {
noPostfixText = text;
}
else {
noPostfixText = text.substring(0, text.length() - 1);
}
return noPostfixText;
}
/**
* Removes postfixes from hex literals
*/
private String removePostfixHex(String text)
{
char lastchar = Character.toUpperCase(text.charAt(text.length() - 1));
boolean hasPostfix = false;
if (lastchar == 'L') {
// Example: 0x00FFL
hasPostfix = true;
}
else if (lastchar == 'F' && (text.contains("p") || text.contains("P"))) {
// Example: 0x1.0p1f (Hex Float)
hasPostfix = true;
}
String noPostfixText;
if (hasPostfix) {
noPostfixText = text.substring(0, text.length() - 1);
}
else {
noPostfixText = text;
}
return noPostfixText;
}
}