com.gccloud.starter.common.utils.BeanConvertUtils Maven / Gradle / Ivy
package com.gccloud.starter.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
/**
* 类转换
*
* @author liuchengbiao
* @date 2020-07-07 10:44
*/
@Slf4j
public class BeanConvertUtils {
/**
* 类转换
*
* @param source 被转换的类
* @param targetClass 需要转为的目标类型
* @param
* @return
*/
public static T convert(Object source, Class targetClass) {
try {
if (source == null) {
return targetClass.newInstance();
}
T target = targetClass.newInstance();
BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* 类转换
*
* @param source 被转换的类
* @param target 需要转为的目标对象,需要自己先初始化对象
*/
public static void convert(Object source, Object target) {
BeanUtils.copyProperties(source, target);
}
}