@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String age;
private String name;
private String sex;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private Integer age;
private String sex;
private String name;
private String num;
}
@Slf4j
public class Convert {
public static void main(String[] args) {
Student student = new Student("12", "小明", null);
Teacher teacher = new Teacher();
copyProperties(student, teacher, true);
System.out.println(teacher);
}
public static Object copyProperties(Object source, Object target, Boolean ignoreNullField) {
List<Map<String, Object>> sourceFields = getFieldInfo(source);
if (ObjectUtils.isEmpty(sourceFields)) {
return source;
}
for (Map sourceFieldMap : sourceFields) {
try {
Field f = target.getClass().getDeclaredField(sourceFieldMap.get("name").toString());
if (ignoreNullField && ObjectUtils.isEmpty(sourceFieldMap.get("value"))) {
continue;
}
String scTypeName=null;
Object obj=null;
if(!sourceFieldMap.get("type").equals(f.getType().toString())){
String s = f.getType().toString();
String[] fSplit =s.split("\\.");
String fTypeName=fSplit[fSplit.length-1] ;
String[] scSplit = sourceFieldMap.get("type").toString().split("\\.");
scTypeName=scSplit[scSplit.length-1];
obj=returnVlaue(fTypeName,scTypeName,sourceFieldMap);
f.setAccessible(true);
f.set(target, obj);
}else{
f.setAccessible(true);
f.set(target, sourceFieldMap.get("value"));
}
} catch (Exception ex) {
try {
Field superField = target.getClass().getSuperclass()
.getDeclaredField(sourceFieldMap.get("name").toString());
superField.setAccessible(true);
superField.set(target, sourceFieldMap.get("value"));
} catch (Exception e1) {
log.error(ex.getMessage(), "转换的类型有误!!!" + ex);
}
}
}
return source;
}
private static Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter);
return method.invoke(o);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
private static String[] getFieldName(Object o) {
Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
fieldNames[i] = fields[i].getName();
}
return fieldNames;
}
private static List<Map<String, Object>> getFieldInfo(Object o) {
List<Map<String, Object>> list = new ArrayList<>();
if (ObjectUtils.isEmpty(o)) {
return null;
}
List<Field> fields = new ArrayList<>(Arrays.asList(o.getClass().getDeclaredFields()));
Class sc = o.getClass().getSuperclass();
if (sc != null) {
fields.addAll(Arrays.asList(sc.getDeclaredFields()));
}
for (Field field : fields) {
Map<String, Object> infoMap = new HashMap<>();
infoMap.put("type", field.getType().toString());
infoMap.put("name", field.getName());
infoMap.put("value", getFieldValueByName(field.getName(), o));
list.add(infoMap);
}
return list;
}
public static Object returnVlaue(String fTypeName,String scTypeName,Map sourceFieldMap){
if(fTypeName.equals("String")){
return sourceFieldMap.get(scTypeName).toString();
}else if(fTypeName.equals("Integer")){
return Integer.valueOf(sourceFieldMap.get("value")+"");
}else if(fTypeName.equals("BigDecimal")){
return new BigDecimal(sourceFieldMap.get("value").toString());
}
return null;
}
}