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

- 使用 getDeclaredConstructor().newInstance() 替代已废弃的 newInstance() 创建对象
- 完善方法参数和返回值的注释,增强代码可读性
- 修改 copyList 方法注释,明确泛型参数含义
- 调整 verify 方法中流操作,将 collect(Collectors.toList()) 改为 stream().toList()
- 统一注释风格,添加缺失的空格,提升文档规范性
This commit is contained in:
CoderKK
2026-03-19 17:12:08 +08:00
parent 442cf7adb3
commit 7997127ea6

View File

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