Skip to content

Commit bc09af4

Browse files
SONARJAVA-5745 Stop using org.apache.commons.lang3.StringUtils::countMatches (#5288)
Co-authored-by: Dorian Burihabwa <75226315+dorian-burihabwa-sonarsource@users.noreply.github.com>
1 parent 02ec1b2 commit bc09af4

6 files changed

Lines changed: 94 additions & 4 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/AbstractPrintfChecker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import java.util.stream.Collectors;
3030
import java.util.stream.IntStream;
3131
import javax.annotation.Nullable;
32-
import org.apache.commons.lang3.StringUtils;
32+
33+
import org.sonar.java.checks.helpers.StringUtils;
3334
import org.sonar.java.checks.methods.AbstractMethodDetection;
3435
import org.sonar.java.model.LiteralUtils;
3536
import org.sonar.plugins.java.api.semantic.MethodMatchers;

java-checks/src/main/java/org/sonar/java/checks/HardcodedIpCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.Optional;
2222
import java.util.regex.Matcher;
2323
import java.util.regex.Pattern;
24-
import org.apache.commons.lang3.StringUtils;
2524
import org.sonar.check.Rule;
25+
import org.sonar.java.checks.helpers.StringUtils;
2626
import org.sonar.java.model.LiteralUtils;
2727
import org.sonar.plugins.java.api.JavaFileScanner;
2828
import org.sonar.plugins.java.api.JavaFileScannerContext;

java-checks/src/main/java/org/sonar/java/checks/PreparedStatementAndResultSetCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import java.util.Optional;
2020
import javax.annotation.CheckForNull;
2121
import javax.annotation.Nullable;
22-
import org.apache.commons.lang3.StringUtils;
2322
import org.sonar.check.Rule;
2423
import org.sonar.java.checks.helpers.ReassignmentFinder;
24+
import org.sonar.java.checks.helpers.StringUtils;
2525
import org.sonar.java.checks.methods.AbstractMethodDetection;
2626
import org.sonar.java.model.ExpressionUtils;
2727
import org.sonar.plugins.java.api.semantic.MethodMatchers;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java.checks.helpers;
18+
19+
import javax.annotation.Nullable;
20+
21+
public class StringUtils {
22+
private StringUtils() {}
23+
24+
/** Check if the string is null or empty. */
25+
public static boolean isEmpty(@Nullable String s) {
26+
return s == null || s.isEmpty();
27+
}
28+
29+
/** Count non-overlapping occurrences of <code>pattern</code> in the <code>string</code>. */
30+
public static int countMatches(@Nullable String string, @Nullable String pattern) {
31+
if (isEmpty(string) || isEmpty(pattern)) {
32+
return 0;
33+
}
34+
35+
int count = 0;
36+
int idx = 0;
37+
while ((idx = string.indexOf(pattern, idx)) != -1) {
38+
count++;
39+
idx += pattern.length();
40+
}
41+
42+
return count;
43+
}
44+
}

java-checks/src/main/java/org/sonar/java/checks/regex/RegexComplexityCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import java.util.HashSet;
2121
import java.util.List;
2222
import java.util.Set;
23-
import org.apache.commons.lang3.StringUtils;
2423
import org.sonar.check.Rule;
2524
import org.sonar.check.RuleProperty;
25+
import org.sonar.java.checks.helpers.StringUtils;
2626
import org.sonar.java.model.ExpressionUtils;
2727
import org.sonar.java.model.LineUtils;
2828
import org.sonar.plugins.java.api.JavaFileScannerContext;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java.checks.helpers;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class StringUtilsTest {
24+
@Test
25+
void testIsEmpty() {
26+
assertThat(StringUtils.isEmpty(null)).isTrue();
27+
assertThat(StringUtils.isEmpty("")).isTrue();
28+
assertThat(StringUtils.isEmpty(" ")).isFalse();
29+
assertThat(StringUtils.isEmpty("abc")).isFalse();
30+
}
31+
32+
@Test
33+
void testCountMatches() {
34+
assertThat(StringUtils.countMatches(null, "ab")).isZero();
35+
assertThat(StringUtils.countMatches("", "ab")).isZero();
36+
assertThat(StringUtils.countMatches("ab", null)).isZero();
37+
assertThat(StringUtils.countMatches("ab", "")).isZero();
38+
39+
assertThat(StringUtils.countMatches("abababab", "cccc")).isZero();
40+
assertThat(StringUtils.countMatches("abababab", "ab")).isEqualTo(4);
41+
42+
assertThat(StringUtils.countMatches("abaTaba", "aba")).isEqualTo(2);
43+
assertThat(StringUtils.countMatches("abababa", "aba")).isEqualTo(2);
44+
}
45+
}

0 commit comments

Comments
 (0)