一个公司两个系统,创建的实体含义一样,但是字段不一致,用get和set方法转换太恶心,而且容易空指针,所以使用注解和反射来写,特地记录一下,有要用的同学可以拿去用哈。
上代码:
package com.yunda.cross.util;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 属性名称*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FileName {String name() default "";
}
package com.yunda.cross.util;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class BeanConvertorUtil {/*** @author lixiao 90183971* @date 2021/5/21 9:02* @param src 带属性的实体 target被赋值的实体* @return void*/public static Object convertor(Object src,Object target) throws Exception {Class<?> srcClz = src.getClass();Class<?> targetClz = target.getClass();Field[] fields = srcClz.getDeclaredFields();for (Field field : fields) {field.setAccessible(true);//获取属性值Object value = field.get(src);//获取注解FileName annotation = field.getAnnotation(FileName.class);if(annotation!=null && annotation.name()!=null){//获取当前方法的参数类型Class<?> c = getMethodParamTypes(target,"set" + captureName(annotation.name()));Method method = targetClz.getDeclaredMethod("set" + captureName(annotation.name()),c);method.setAccessible(true);method.invoke(target,value);}}return target;}//方法首字母大写private static String captureName(String name) {name = name.substring(0, 1).toUpperCase() + name.substring(1);return name;}//获取方法的类型private static Class<?> getMethodParamTypes(Object classInstance,String methodName){//全部方法Method[] methods = classInstance.getClass().getMethods();Class<?> param = null;for (int i = 0; i< methods.length; i++) {//和传入方法名匹配if(methodName.equals(methods[i].getName())){param = methods[i].getParameterTypes()[0];}}return param;}}
使用:
public static void main(String[] args) {
Test1Po test1Po = new Test1Po();
test1Po.setAgeA("a");
test1Po.setClassNameA("c");
test1Po.setEmailA("e");
test1Po.setParentA("p");
Test2Po test2Po = new Test2Po();try {System.out.println(JSON.toJSONString(copy(test1Po, test2Po)));
} catch (Exception e) {e.printStackTrace();
}
}
package com.yunda.cross.util;import org.springframework.stereotype.Component;public class Test1Po {@FileName(name = "name")private String nameA;@FileName(name = "sex")private String sexA;@FileName(name = "age")private String ageA;@FileName(name = "email")private String emailA;@FileName(name = "className")private String classNameA;@FileName(name = "parent")private String parentA;public String getNameA() {return nameA;}public void setNameA(String nameA) {this.nameA = nameA;}public String getSexA() {return sexA;}public void setSexA(String sexA) {this.sexA = sexA;}public String getAgeA() {return ageA;}public void setAgeA(String ageA) {this.ageA = ageA;}public String getEmailA() {return emailA;}public void setEmailA(String emailA) {this.emailA = emailA;}public String getClassNameA() {return classNameA;}public void setClassNameA(String classNameA) {this.classNameA = classNameA;}public String getParentA() {return parentA;}public void setParentA(String parentA) {this.parentA = parentA;}
}
package com.yunda.cross.util;public class Test2Po {private String name;private String sex;private String age;private String email;private String className;private String parent;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getParent() {return parent;}public void setParent(String parent) {this.parent = parent;}
}