@@ -207,18 +207,9 @@ public class RegexpCheck extends AbstractCheck {
207
207
/** Boolean to say if we should check for duplicates. */
208
208
private boolean checkForDuplicates ;
209
209
210
- /** Tracks number of matches made. */
211
- private int matchCount ;
212
-
213
- /** Tracks number of errors. */
214
- private int errorCount ;
215
-
216
210
/** Specify the pattern to match against. */
217
211
private Pattern format = Pattern .compile ("^$" , Pattern .MULTILINE );
218
212
219
- /** The matcher. */
220
- private Matcher matcher ;
221
-
222
213
/**
223
214
* Setter to specify message which is used to notify about violations,
224
215
* if empty then the default (hard-coded) message is used.
@@ -300,73 +291,54 @@ public int[] getRequiredTokens() {
300
291
return CommonUtil .EMPTY_INT_ARRAY ;
301
292
}
302
293
303
- // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
304
- @ SuppressWarnings ("deprecation" )
305
294
@ Override
306
295
public void beginTree (DetailAST rootAST ) {
307
- matcher = format .matcher (getFileContents ().getText ().getFullText ());
308
- matchCount = 0 ;
309
- errorCount = 0 ;
310
- findMatch ();
296
+ processRegexpMatches ();
311
297
}
312
298
313
299
/**
314
- * Recursive method that finds the matches .
300
+ * Processes the regexp matches and logs the number of errors in the file .
315
301
*
316
- * @noinspection TailRecursion
317
- * @noinspectionreason TailRecursion - until issue #14814
318
302
*/
319
303
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
320
304
@ SuppressWarnings ("deprecation" )
321
- private void findMatch () {
322
- final boolean foundMatch = matcher .find ();
323
- if (foundMatch ) {
324
- final FileText text = getFileContents ().getText ();
305
+ private void processRegexpMatches () {
306
+ final Matcher matcher = format .matcher (getFileContents ().getText ().getFullText ());
307
+ int errorCount = 0 ;
308
+ int matchCount = 0 ;
309
+ final FileText text = getFileContents ().getText ();
310
+ while (errorCount < errorLimit && matcher .find ()) {
325
311
final LineColumn start = text .lineColumn (matcher .start ());
326
312
final int startLine = start .getLine ();
327
313
328
- final boolean ignore = isIgnore (startLine , text , start );
329
-
314
+ final boolean ignore = isIgnore (startLine , text , start , matcher );
330
315
if (!ignore ) {
331
316
matchCount ++;
332
317
if (illegalPattern || checkForDuplicates
333
318
&& matchCount - 1 > duplicateLimit ) {
334
319
errorCount ++;
335
- logMessage (startLine );
320
+ logMessage (startLine , errorCount );
336
321
}
337
322
}
338
- if (canContinueValidation (ignore )) {
339
- findMatch ();
340
- }
341
323
}
342
- else if (!illegalPattern && matchCount == 0 ) {
343
- final String msg = getMessage ();
324
+ if (!illegalPattern && matchCount == 0 ) {
325
+ final String msg = getMessage (errorCount );
344
326
log (1 , MSG_REQUIRED_REGEXP , msg );
345
327
}
346
328
}
347
329
348
- /**
349
- * Check if we can stop validation.
350
- *
351
- * @param ignore flag
352
- * @return true is we can continue
353
- */
354
- private boolean canContinueValidation (boolean ignore ) {
355
- return errorCount <= errorLimit - 1
356
- && (ignore || illegalPattern || checkForDuplicates );
357
- }
358
-
359
330
/**
360
331
* Detect ignore situation.
361
332
*
362
333
* @param startLine position of line
363
334
* @param text file text
364
335
* @param start line column
336
+ * @param matcher The matcher
365
337
* @return true is that need to be ignored
366
338
*/
367
339
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
368
340
@ SuppressWarnings ("deprecation" )
369
- private boolean isIgnore (int startLine , FileText text , LineColumn start ) {
341
+ private boolean isIgnore (int startLine , FileText text , LineColumn start , Matcher matcher ) {
370
342
final LineColumn end ;
371
343
if (matcher .end () == 0 ) {
372
344
end = text .lineColumn (0 );
@@ -390,9 +362,10 @@ private boolean isIgnore(int startLine, FileText text, LineColumn start) {
390
362
* Displays the right message.
391
363
*
392
364
* @param lineNumber the line number the message relates to.
365
+ * @param errorCount number of errors in the file.
393
366
*/
394
- private void logMessage (int lineNumber ) {
395
- final String msg = getMessage ();
367
+ private void logMessage (int lineNumber , int errorCount ) {
368
+ final String msg = getMessage (errorCount );
396
369
397
370
if (illegalPattern ) {
398
371
log (lineNumber , MSG_ILLEGAL_REGEXP , msg );
@@ -405,9 +378,10 @@ private void logMessage(int lineNumber) {
405
378
/**
406
379
* Provide right message.
407
380
*
381
+ * @param errorCount number of errors in the file.
408
382
* @return message for violation.
409
383
*/
410
- private String getMessage () {
384
+ private String getMessage (int errorCount ) {
411
385
String msg ;
412
386
413
387
if (message == null || message .isEmpty ()) {
@@ -423,5 +397,4 @@ private String getMessage() {
423
397
424
398
return msg ;
425
399
}
426
-
427
400
}
0 commit comments