Files
xiaozhi-esp32-server/main/manager-api/src/main/java/xiaozhi/common/utils/ConvertUtils.java
T
2025-03-29 17:49:39 +08:00

51 lines
1.4 KiB
Java

package xiaozhi.common.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
/**
* 转换工具类
* Copyright (c) 人人开源 All rights reserved.
* Website: https://www.renren.io
*/
@Slf4j
public class ConvertUtils {
public static <T> T sourceToTarget(Object source, Class<T> target) {
if (source == null) {
return null;
}
T targetObject = null;
try {
targetObject = target.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, targetObject);
} catch (Exception e) {
log.error("convert error ", e);
}
return targetObject;
}
public static <T> List<T> sourceToTarget(Collection<?> sourceList, Class<T> target) {
if (sourceList == null) {
return null;
}
List<T> targetList = new ArrayList<>(sourceList.size());
try {
for (Object source : sourceList) {
T targetObject = target.getDeclaredConstructor().newInstance();
BeanUtils.copyProperties(source, targetObject);
targetList.add(targetObject);
}
} catch (Exception e) {
log.error("convert error ", e);
}
return targetList;
}
}