import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class BeanUtil {
public static <T> T copyPropertiesToT(Object o, Class<T> c) {
T t = null;
try {
t = c.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
assert t != null;
if(o == null){
return null;
}
BeanUtils.copyProperties(o, t);
return t;
}
public static <T> List<T> copyPropertiesToList(List<?> oList, Class<T> c) {
if(oList == null){
return new ArrayList<>();
}
return oList.stream().map(o -> {
T t = null;
try {
t = c.newInstance();
BeanUtils.copyProperties(o, t);
return t;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}).collect(Collectors.toList());
}
public static <T> PageInfo<T> getPageInfoData(PageInfo<?> fromPage, T data){
PageInfo<T> result = new PageInfo<T>();
result.setCurrentPage(fromPage.getCurrentPage());
result.setPageSize(fromPage.getPageSize());
result.setTotalCount(fromPage.getTotalCount());
result.setTotalPage(fromPage.getTotalPage());
result.setDataList(data);
return result;
}
public static void main(String[] args) {
List<ClientBase> list = new ArrayList<>();
list.add(MockUtil.mock(new ClientBase()));
list.add(MockUtil.mock(new ClientBase()));
list.add(MockUtil.mock(new ClientBase()));
list.add(MockUtil.mock(new ClientBase()));
List<ClientBaseVO> clientBaseVOS = BeanUtil.copyPropertiesToList(list, ClientBaseVO.class);
System.out.println(clientBaseVOS.size());
ClientBaseVO clientBaseVO = BeanUtil.copyPropertiesToT(MockUtil.mock(new ClientBase()), ClientBaseVO.class);
System.out.println(clientBaseVO);
PageInfo<List<ClientBaseVO>> pageData = null;
List<LoanOrderListVo> a = BeanUtil.copyPropertiesToList(pageData.getDataList(), LoanOrderListVo.class);
PageInfo<List<LoanOrderListVo>> b = BeanUtil.getPageInfoData(pageData,a);
}
}