mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-06 09:09:10 +08:00
Compare commits
3 Commits
86c3c530b6
...
472b0d2841
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
472b0d2841 | ||
|
|
00748130ef | ||
|
|
820e0d0c8b |
@ -1,7 +1,7 @@
|
||||
|
||||
# 🚀Changelog
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.42(2025-11-25)
|
||||
# 5.8.42(2025-11-26)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 `ListUtil`增加`zip`方法(pr#4052@Github)
|
||||
@ -10,6 +10,7 @@
|
||||
* 【core 】 `CharSequenceUtil`增加`builder`方法重载(pr#4107@Github)
|
||||
* 【core 】 `Combination`和`Arrangement `重构避免数组频繁拷贝,并避免溢出(pr#4144@Github)
|
||||
* 【core 】 优化`EscapeUtil`,兼容不规范的转义(pr#4150@Github)
|
||||
* 【core 】 优化`ObjectUtil.contains`String改为CharSequence(pr#4154@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【jwt 】 修复verify方法在定义alg为`none`时验证失效问题(issue#4105@Github)
|
||||
|
||||
@ -128,7 +128,7 @@ public class ObjectUtil {
|
||||
* 对象中是否包含元素<br>
|
||||
* 支持的对象类型包括:
|
||||
* <ul>
|
||||
* <li>String</li>
|
||||
* <li>CharSequence</li>
|
||||
* <li>Collection</li>
|
||||
* <li>Map</li>
|
||||
* <li>Iterator</li>
|
||||
@ -144,11 +144,22 @@ public class ObjectUtil {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (obj instanceof String) {
|
||||
if (element == null) {
|
||||
if (obj instanceof CharSequence) {
|
||||
if (!(element instanceof CharSequence)) {
|
||||
return false;
|
||||
}
|
||||
return ((String) obj).contains(element.toString());
|
||||
String elementStr;
|
||||
try {
|
||||
elementStr = element.toString();
|
||||
// 检查 toString() 返回 null 的情况
|
||||
if (elementStr == null) {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 如果toString抛异常,认为不包含
|
||||
return false;
|
||||
}
|
||||
return obj.toString().contains(elementStr);
|
||||
}
|
||||
if (obj instanceof Collection) {
|
||||
return ((Collection<?>) obj).contains(element);
|
||||
|
||||
@ -135,4 +135,46 @@ public class ObjectUtilTest {
|
||||
// 枚举已经无法使用
|
||||
assertFalse(enumeration.hasMoreElements());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsElementToStringReturnsNull() {
|
||||
Object problematicElement = new Object() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return null; // 返回 null 的 toString
|
||||
}
|
||||
};
|
||||
assertFalse(ObjectUtil.contains("test", problematicElement)); //不会抛异常
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsElementToStringInvalidSyntax() {
|
||||
//字符串包含自定义User对象不符合语义
|
||||
assertFalse(ObjectUtil.contains("User[id=123]", new User(123)));
|
||||
}
|
||||
|
||||
|
||||
static class User{
|
||||
private int id;
|
||||
public User(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User[" +
|
||||
"id=" + id +
|
||||
']';
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsCharSequenceSupported() {
|
||||
//contains方法支持String、StringBuilder、StringBuffer
|
||||
StringBuilder stringBuilder = new StringBuilder("hello world");
|
||||
StringBuffer stringBuffer = new StringBuffer("hello world");
|
||||
String str = "hello world";
|
||||
assertTrue((ObjectUtil.contains(stringBuilder, "world")));
|
||||
assertTrue(ObjectUtil.contains(stringBuffer, "hello"));
|
||||
assertTrue(ObjectUtil.contains(str, "hello"));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user