场景: 编码过程中经常遇到,实体类中参数名转换其他参数名,以做后续业务逻辑处理。 同一个功能,可能不同的人接手后,不知道之前转换的方法在哪里,又重新写了一个方法,或者不同的实体,需要转换成同一个实体,这样就造成重复工作浪费。
为解决这个问题,对参数名转换进行封装,提高工作效率, 参考BeanUtils.copyProperties方法,结合注解的使用,重写了一套方法。
- 新建注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RefAnnotation {
String[] dec() default {};
}
- 编写两个测试示例类
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ReflectVo implements Serializable {
private static final long serialVersionUID = 8827950221777954148L;
@RefAnnotation(dec = {"aaa","bbb","ccc"})
private String name;
@RefAnnotation(dec = {"ddd","eee"})
private String scott;
}
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SourceVo implements Serializable {
private static final long serialVersionUID = -6564361729001137186L;
private String aaa;
private String ddd;
}
- 对象复制方法
public class BeanCopyVo {
public static void copyProperties(Object source, Object target) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
Field[] fields = actualEditable.getDeclaredFields();
Map<String, Field> map = new HashMap<>();
for (Field f : fields) {
map.put(f.getName(), f);
}
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (null == writeMethod) {
continue;
}
//获取注解
String fieldName = targetPd.getName();
Field field = map.get(fieldName);
if (null == field) {
continue;
}
if (!field.isAnnotationPresent(RefAnnotation.class)) {
continue;
}
RefAnnotation annotation = field.getAnnotation(RefAnnotation.class);
String[] dec = annotation.dec();
if (writeMethod != null) {
PropertyDescriptor sourcePd = null;
for (String methodName : dec) {
sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), methodName);
if (null == sourcePd) {
continue;
}
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null && ClassUtils
.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
} catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
}
- 注意 性能影响请结合自己系统测试,本机测试结果耗时不到1ms。
git地址:https://github.com/fengyantao/beancopy
欢迎大家多多交流和指导