mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add edit method
This commit is contained in:
@@ -3,11 +3,12 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.6.4 (2021-04-14)
|
# 5.6.4 (2021-04-18)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 DatePattern补充DateTimeFormatter(pr#308@Gitee)
|
* 【core 】 DatePattern补充DateTimeFormatter(pr#308@Gitee)
|
||||||
* 【core 】 DateUtil.compare增加支持给定格式比较(pr#310@Gitee)
|
* 【core 】 DateUtil.compare增加支持给定格式比较(pr#310@Gitee)
|
||||||
|
* 【core 】 BeanUtil增加edit方法(issue#I3J6BG@Gitee)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【db 】 修复SQL分页时未使用别名导致的错误,同时count时取消order by子句(issue#I3IJ8X@Gitee)
|
* 【db 】 修复SQL分页时未使用别名导致的错误,同时count时取消order by子句(issue#I3IJ8X@Gitee)
|
||||||
|
@@ -729,6 +729,31 @@ public class BeanUtil {
|
|||||||
return ClassUtil.getClassName(bean, isSimple).equals(isSimple ? StrUtil.upperFirst(beanClassName) : beanClassName);
|
return ClassUtil.getClassName(bean, isSimple).equals(isSimple ? StrUtil.upperFirst(beanClassName) : beanClassName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑Bean的字段,static字段不会处理<br>
|
||||||
|
* 例如需要对指定的字段做判空操作、null转""操作等等。
|
||||||
|
*
|
||||||
|
* @param bean bean
|
||||||
|
* @param editor 编辑器函数
|
||||||
|
* @param <T> 被编辑的Bean类型
|
||||||
|
* @return bean
|
||||||
|
* @since 5.6.4
|
||||||
|
*/
|
||||||
|
public static <T> T edit(T bean, Editor<Field> editor){
|
||||||
|
if (bean == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Field[] fields = ReflectUtil.getFields(bean.getClass());
|
||||||
|
for (Field field : fields) {
|
||||||
|
if (ModifierUtil.isStatic(field)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
editor.edit(field);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 把Bean里面的String属性做trim操作。此方法直接对传入的Bean做修改。
|
* 把Bean里面的String属性做trim操作。此方法直接对传入的Bean做修改。
|
||||||
* <p>
|
* <p>
|
||||||
@@ -740,18 +765,10 @@ public class BeanUtil {
|
|||||||
* @return 处理后的Bean对象
|
* @return 处理后的Bean对象
|
||||||
*/
|
*/
|
||||||
public static <T> T trimStrFields(T bean, String... ignoreFields) {
|
public static <T> T trimStrFields(T bean, String... ignoreFields) {
|
||||||
if (bean == null) {
|
return edit(bean, (field)->{
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Field[] fields = ReflectUtil.getFields(bean.getClass());
|
|
||||||
for (Field field : fields) {
|
|
||||||
if (ModifierUtil.isStatic(field)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (ignoreFields != null && ArrayUtil.containsIgnoreCase(ignoreFields, field.getName())) {
|
if (ignoreFields != null && ArrayUtil.containsIgnoreCase(ignoreFields, field.getName())) {
|
||||||
// 不处理忽略的Fields
|
// 不处理忽略的Fields
|
||||||
continue;
|
return field;
|
||||||
}
|
}
|
||||||
if (String.class.equals(field.getType())) {
|
if (String.class.equals(field.getType())) {
|
||||||
// 只有String的Field才处理
|
// 只有String的Field才处理
|
||||||
@@ -764,9 +781,8 @@ public class BeanUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return field;
|
||||||
|
});
|
||||||
return bean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -5,7 +5,7 @@ package cn.hutool.core.lang;
|
|||||||
* 此编辑器两个作用:
|
* 此编辑器两个作用:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* 1、如果返回值为<code>null</code>,表示此值被抛弃
|
* 1、如果返回值为{@code null},表示此值被抛弃
|
||||||
* 2、对对象做修改
|
* 2、对对象做修改
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
@@ -18,7 +18,7 @@ public interface Editor<T> {
|
|||||||
* 修改过滤后的结果
|
* 修改过滤后的结果
|
||||||
*
|
*
|
||||||
* @param t 被过滤的对象
|
* @param t 被过滤的对象
|
||||||
* @return 修改后的对象,如果被过滤返回<code>null</code>
|
* @return 修改后的对象,如果被过滤返回{@code null}
|
||||||
*/
|
*/
|
||||||
T edit(T t);
|
T edit(T t);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user