!96 refactor(util): 优化 SmartBeanUtil 代码和注释

Merge pull request !96 from CoderKK/feat-beanUtil
This commit is contained in:
卓大
2026-04-05 07:44:26 +00:00
committed by Gitee

View File

@@ -17,7 +17,7 @@ import java.util.stream.Collectors;
* @Date 2018-01-15 10:48:23 * @Date 2018-01-15 10:48:23
* @Wechat zhuoda1024 * @Wechat zhuoda1024
* @Email lab1024@163.com * @Email lab1024@163.com
* @Copyright <a href="https://1024lab.net">1024创新实验室</a> * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
*/ */
public class SmartBeanUtil { public class SmartBeanUtil {
@@ -41,15 +41,15 @@ public class SmartBeanUtil {
* *
* @param source 源 要复制的对象 * @param source 源 要复制的对象
* @param target 目标 复制到此对象 * @param target 目标 复制到此对象
* @param <T> * @param <T> 目标对象的类型
* @return * @return 目标对象
*/ */
public static <T> T copy(Object source, Class<T> target) { public static <T> T copy(Object source, Class<T> target) {
if (source == null || target == null) { if (source == null || target == null) {
return null; return null;
} }
try { try {
T newInstance = target.newInstance(); T newInstance = target.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, newInstance); BeanUtils.copyProperties(source, newInstance);
return newInstance; return newInstance;
} catch (Exception e) { } catch (Exception e) {
@@ -60,11 +60,11 @@ public class SmartBeanUtil {
/** /**
* 复制list * 复制list
* *
* @param source * @param source 源 要复制的列表
* @param target * @param target 目标 复制到此对象
* @param <T> * @param <T> 源列表的类型
* @param <K> * @param <K> 目标列表的类型
* @return * @return 目标列表
*/ */
public static <T, K> List<K> copyList(List<T> source, Class<K> target) { public static <T, K> List<K> copyList(List<T> source, Class<K> target) {
if (null == source || source.isEmpty()) { if (null == source || source.isEmpty()) {
@@ -74,11 +74,11 @@ public class SmartBeanUtil {
} }
/** /**
* 手动验证对象 Model的属性 * 手动验证对象 Model 的属性
* 需要配合 hibernate-validator 校验注解 * 需要配合 hibernate-validator 校验注解
* *
* @param t * @param t 需要验证的对象
* @return String 返回null代表验证通过否则返回错误的信息 * @return String 返回 null 代表验证通过,否则返回错误的信息
*/ */
public static <T> String verify(T t) { public static <T> String verify(T t) {
// 获取验证结果 // 获取验证结果
@@ -88,7 +88,7 @@ public class SmartBeanUtil {
return null; return null;
} }
// 返回错误信息 // 返回错误信息
List<String> messageList = validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.toList()); List<String> messageList = validate.stream().map(ConstraintViolation::getMessage).toList();
return messageList.toString(); return messageList.toString();
} }
} }