mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
add test
This commit is contained in:
@@ -183,6 +183,7 @@ public class BeanUtil {
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- beanPath
|
||||
/**
|
||||
* 获取Bean中的属性值
|
||||
*
|
||||
@@ -221,6 +222,7 @@ public class BeanUtil {
|
||||
public static void setProperty(final Object bean, final String expression, final Object value) {
|
||||
BeanPath.of(expression).setValue(bean, value);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- toBean
|
||||
|
||||
@@ -728,9 +730,31 @@ public class BeanUtil {
|
||||
|
||||
return hasSetter(clazz) || hasPublicField(clazz);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- hasXXX
|
||||
/**
|
||||
* 检查Bean<br>
|
||||
* 遍历Bean的字段并断言检查字段,当某个字段:
|
||||
* 断言为{@code true} 时,返回{@code true}并不再检查后续字段;<br>
|
||||
* 断言为{@code false}时,继续检查后续字段
|
||||
*
|
||||
* @param bean Bean
|
||||
* @param predicate 断言
|
||||
* @return 是否触发断言为真
|
||||
*/
|
||||
public static boolean checkBean(final Object bean, final Predicate<Field> predicate) {
|
||||
if (null == bean) {
|
||||
return true;
|
||||
}
|
||||
for (final Field field : FieldUtil.getFields(bean.getClass())) {
|
||||
if (ModifierUtil.isStatic(field)) {
|
||||
continue;
|
||||
}
|
||||
if (predicate.test(field)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有Setter方法<br>
|
||||
@@ -829,31 +853,6 @@ public class BeanUtil {
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 检查Bean<br>
|
||||
* 遍历Bean的字段并断言检查字段,当某个字段:
|
||||
* 断言为{@code true} 时,返回{@code true}并不再检查后续字段;<br>
|
||||
* 断言为{@code false}时,继续检查后续字段
|
||||
*
|
||||
* @param bean Bean
|
||||
* @param predicate 断言
|
||||
* @return 是否触发断言为真
|
||||
*/
|
||||
public static boolean checkBean(final Object bean, final Predicate<Field> predicate) {
|
||||
if (null == bean) {
|
||||
return true;
|
||||
}
|
||||
for (final Field field : FieldUtil.getFields(bean.getClass())) {
|
||||
if (ModifierUtil.isStatic(field)) {
|
||||
continue;
|
||||
}
|
||||
if (predicate.test(field)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Getter或Setter方法名对应的字段名称,规则如下:
|
||||
* <ul>
|
||||
|
||||
@@ -120,7 +120,7 @@ public class BeanPath<T> implements Iterator<BeanPath<T>> {
|
||||
}
|
||||
// 每一个边界符之前的表达式是一个完整的KEY,开始处理KEY
|
||||
}
|
||||
if (builder.length() > 0) {
|
||||
if (!builder.isEmpty()) {
|
||||
this.node = NodeFactory.createNode(builder.toString());
|
||||
// 如果以[结束,表示后续还有表达式,需保留'[',如name[0]
|
||||
this.child = StrUtil.nullIfEmpty(expression.substring(c == CharUtil.BRACKET_START ? i : i + 1));
|
||||
|
||||
@@ -1131,4 +1131,26 @@ public class BeanUtilTest {
|
||||
final PropertyDescriptor nonExistingField = BeanUtil.getPropertyDescriptor(Person.class, "nonExistingField");
|
||||
assertNull(nonExistingField);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBeanToMap_IgnoreNullValue() {
|
||||
Person person = new Person();
|
||||
person.setName(null);
|
||||
person.setAge(25);
|
||||
|
||||
// 忽略空值
|
||||
Map<String, Object> map = BeanUtil.beanToMap(person, false, true);
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(25, map.get("age"));
|
||||
assertFalse(map.containsKey("name"));
|
||||
|
||||
// 不忽略空值
|
||||
map = BeanUtil.beanToMap(person, false, false);
|
||||
assertEquals(3, map.size());
|
||||
assertTrue(map.containsKey("name"));
|
||||
assertNull(map.get("name"));
|
||||
assertEquals(25, map.get("age"));
|
||||
assertTrue(map.containsKey("openid"));
|
||||
assertNull(map.get("openid"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user