Skip to content

Commit 9db3909

Browse files
mohitsatrrdiachenko
authored andcommitted
Issue #14814: refactor findmatch into iteration method
1 parent 853e2ba commit 9db3909

File tree

2 files changed

+19
-53
lines changed

2 files changed

+19
-53
lines changed

config/checker-framework-suppressions/checker-nullness-optional-interning-suppressions.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4572,13 +4572,6 @@
45724572
<lineContent>/* package */ MultilineDetector(DetectorOptions options) {</lineContent>
45734573
</checkerFrameworkError>
45744574

4575-
<checkerFrameworkError unstable="false">
4576-
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheck.java</fileName>
4577-
<specifier>initialization.field.uninitialized</specifier>
4578-
<message>the default constructor does not initialize field matcher</message>
4579-
<lineContent>private Matcher matcher;</lineContent>
4580-
</checkerFrameworkError>
4581-
45824575
<checkerFrameworkError unstable="false">
45834576
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheck.java</fileName>
45844577
<specifier>initialization.field.uninitialized</specifier>

src/main/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheck.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,9 @@ public class RegexpCheck extends AbstractCheck {
207207
/** Boolean to say if we should check for duplicates. */
208208
private boolean checkForDuplicates;
209209

210-
/** Tracks number of matches made. */
211-
private int matchCount;
212-
213-
/** Tracks number of errors. */
214-
private int errorCount;
215-
216210
/** Specify the pattern to match against. */
217211
private Pattern format = Pattern.compile("^$", Pattern.MULTILINE);
218212

219-
/** The matcher. */
220-
private Matcher matcher;
221-
222213
/**
223214
* Setter to specify message which is used to notify about violations,
224215
* if empty then the default (hard-coded) message is used.
@@ -300,73 +291,54 @@ public int[] getRequiredTokens() {
300291
return CommonUtil.EMPTY_INT_ARRAY;
301292
}
302293

303-
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
304-
@SuppressWarnings("deprecation")
305294
@Override
306295
public void beginTree(DetailAST rootAST) {
307-
matcher = format.matcher(getFileContents().getText().getFullText());
308-
matchCount = 0;
309-
errorCount = 0;
310-
findMatch();
296+
processRegexpMatches();
311297
}
312298

313299
/**
314-
* Recursive method that finds the matches.
300+
* Processes the regexp matches and logs the number of errors in the file.
315301
*
316-
* @noinspection TailRecursion
317-
* @noinspectionreason TailRecursion - until issue #14814
318302
*/
319303
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
320304
@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()) {
325311
final LineColumn start = text.lineColumn(matcher.start());
326312
final int startLine = start.getLine();
327313

328-
final boolean ignore = isIgnore(startLine, text, start);
329-
314+
final boolean ignore = isIgnore(startLine, text, start, matcher);
330315
if (!ignore) {
331316
matchCount++;
332317
if (illegalPattern || checkForDuplicates
333318
&& matchCount - 1 > duplicateLimit) {
334319
errorCount++;
335-
logMessage(startLine);
320+
logMessage(startLine, errorCount);
336321
}
337322
}
338-
if (canContinueValidation(ignore)) {
339-
findMatch();
340-
}
341323
}
342-
else if (!illegalPattern && matchCount == 0) {
343-
final String msg = getMessage();
324+
if (!illegalPattern && matchCount == 0) {
325+
final String msg = getMessage(errorCount);
344326
log(1, MSG_REQUIRED_REGEXP, msg);
345327
}
346328
}
347329

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-
359330
/**
360331
* Detect ignore situation.
361332
*
362333
* @param startLine position of line
363334
* @param text file text
364335
* @param start line column
336+
* @param matcher The matcher
365337
* @return true is that need to be ignored
366338
*/
367339
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
368340
@SuppressWarnings("deprecation")
369-
private boolean isIgnore(int startLine, FileText text, LineColumn start) {
341+
private boolean isIgnore(int startLine, FileText text, LineColumn start, Matcher matcher) {
370342
final LineColumn end;
371343
if (matcher.end() == 0) {
372344
end = text.lineColumn(0);
@@ -390,9 +362,10 @@ private boolean isIgnore(int startLine, FileText text, LineColumn start) {
390362
* Displays the right message.
391363
*
392364
* @param lineNumber the line number the message relates to.
365+
* @param errorCount number of errors in the file.
393366
*/
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);
396369

397370
if (illegalPattern) {
398371
log(lineNumber, MSG_ILLEGAL_REGEXP, msg);
@@ -405,9 +378,10 @@ private void logMessage(int lineNumber) {
405378
/**
406379
* Provide right message.
407380
*
381+
* @param errorCount number of errors in the file.
408382
* @return message for violation.
409383
*/
410-
private String getMessage() {
384+
private String getMessage(int errorCount) {
411385
String msg;
412386

413387
if (message == null || message.isEmpty()) {
@@ -423,5 +397,4 @@ private String getMessage() {
423397

424398
return msg;
425399
}
426-
427400
}

0 commit comments

Comments
 (0)