package com.example.util;
import com.alibaba.fastjson.JSON;
import com.example.commom.PO;
import com.example.commom.VO;
import org.springframework.beans.BeanUtils;
import java.util.*;
public class CommonsUtils {
private static final Random random = new Random();
public static VO toVO(PO po) {
VO vo = ReflectUtils.newInstance(po.getVO());
BeanUtils.copyProperties(po, vo);
return vo;
}
public static List<VO> toVO(List<PO> poList) {
List<VO> voList = null;
if (null != poList && !poList.isEmpty()) {
voList = new ArrayList<>();
for (PO po : poList) {
voList.add(toVO(po));
}
}
return voList;
}
public static <T> T toPO(VO vo) {
PO po = ReflectUtils.newInstance(vo.getPO());
BeanUtils.copyProperties(vo, po);
return (T) po;
}
public static <T> List<T> toPO(List<VO> voList) {
List<T> poList = null;
if (null != voList && !voList.isEmpty()) {
voList = new ArrayList<>();
for (VO vo : voList) {
poList.add(toPO(vo));
}
}
return poList;
}
}
package com.example.util;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class ReflectUtils {
public static <T> T newInstance(Class clazz) {
Object value = null;
try {
value = clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return (T) value;
}
}
package com.example.commom;
public interface PO {
Class getVO();
}
package com.example.commom;
public interface VO {
Class getPO();
}
package com.example.vo;
import com.example.commom.VO;
import com.example.po.AccountPO;
public class AccountVO implements VO {
private String name;
private String password;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public static class Address{
String address;
String code ;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
@Override
public Class getPO() {
return AccountPO.class;
}
}
package com.example.po;
import com.example.commom.PO;
import com.example.vo.AccountVO;
import java.io.Serializable;
public class AccountPO implements PO, Serializable {
private String name;
private String password;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static class Address{
String address;
String code ;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
@Override
public Class getVO() {
return AccountVO.class;
}
}