You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/09/10 16:54:18 UTC
(maven-shared-utils) branch master updated: Deprecate StringUtils.replace and contains
This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push:
new 079eb74 Deprecate StringUtils.replace and contains
079eb74 is described below
commit 079eb74331c91e53422afcda880ff80b5715a58e
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Thu Sep 10 10:58:10 2026 +0200
Deprecate StringUtils.replace and contains
String has carried equivalents since Java 5, so these overloads only
exist to be null-tolerant.
Only the overloads with a direct JDK counterpart are marked. The max
variants and replaceOnce stay, because String.replace cannot limit the
number of replacements.
The javadoc spells out where the JDK method is not a drop-in swap:
"abc".replace("", "-") returns -a-b-c-, while this replace returns the
text unchanged when repl is empty. interpolate() now calls the 4-arg
overload so the class does not warn against itself.
---
.../org/apache/maven/shared/utils/StringUtils.java | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/utils/StringUtils.java b/src/main/java/org/apache/maven/shared/utils/StringUtils.java
index ce58368..333ac00 100644
--- a/src/main/java/org/apache/maven/shared/utils/StringUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/StringUtils.java
@@ -647,7 +647,11 @@ public class StringUtils {
* @param with char to replace with
* @return the text with any replacements processed
* @see #replace(String text, char repl, char with, int max)
+ * @deprecated use {@link String#replace(char, char)} instead. Note that the JDK method throws
+ * {@link NullPointerException} when {@code text} is <code>null</code>, where this
+ * method returns <code>null</code>.
*/
+ @Deprecated
public static String replace(@Nullable String text, char repl, char with) {
return replace(text, repl, with, -1);
}
@@ -693,7 +697,12 @@ public class StringUtils {
* @param with string to replace with
* @return the text with any replacements processed
* @see #replace(String text, String repl, String with, int max)
+ * @deprecated use {@link String#replace(CharSequence, CharSequence)} instead. Note two differences:
+ * the JDK method throws {@link NullPointerException} on <code>null</code> arguments,
+ * and it replaces an empty <code>repl</code> at every position, where this method
+ * returns <code>text</code> unchanged.
*/
+ @Deprecated
public static String replace(@Nullable String text, @Nullable String repl, @Nullable String with) {
return replace(text, repl, with, -1);
}
@@ -1900,10 +1909,10 @@ public class StringUtils {
String value = obj.toString();
- text = replace(text, "${" + key + "}", value);
+ text = replace(text, "${" + key + "}", value, -1);
if (!key.contains(" ")) {
- text = replace(text, "$" + key, value);
+ text = replace(text, "$" + key, value, -1);
}
}
return text;
@@ -2232,7 +2241,11 @@ public class StringUtils {
* @param searchChar the character to find
* @return true if the String contains the search character,
* false if not or <code>null</code> string input
+ * @deprecated use {@link String#indexOf(int)} and test for <code>>= 0</code> instead. Note that
+ * the JDK method throws {@link NullPointerException} when <code>str</code> is
+ * <code>null</code>, where this method returns <code>false</code>.
*/
+ @Deprecated
public static boolean contains(@Nullable String str, char searchChar) {
return !isEmpty(str) && str.indexOf(searchChar) >= 0;
}
@@ -2256,7 +2269,11 @@ public class StringUtils {
* @param searchStr the String to find, may be null
* @return true if the String contains the search String,
* false if not or <code>null</code> string input
+ * @deprecated use {@link String#contains(CharSequence)} instead. Note that the JDK method throws
+ * {@link NullPointerException} on <code>null</code> arguments, where this method
+ * returns <code>false</code>.
*/
+ @Deprecated
public static boolean contains(@Nullable String str, @Nullable String searchStr) {
return !(str == null || searchStr == null) && str.contains(searchStr);
}