我有一堆第三方
Java类,它们使用不同的属性名称来实现相同的属性:
public class Foo { public String getReferenceID(); public void setReferenceID(String id); public String getFilename(); public void setFilename(String fileName); } public class Bar { public String getRefID(); public void setRefID(String id); public String getFileName(); public void setFileName(String fileName); }
我希望能够以规范化的形式解决这些问题,以便我可以对它们进行多态处理,这样我就可以使用Apache BeanUtils来做这些事情,比如:
PropertyUtils.copyProperties(object1,object2);
很明显,为每个类写一个适配器将是微不足道的…
public class CanonicalizedBar implements CanonicalizedBazBean { public String getReferenceID() { return this.delegate.getRefID(); } // etc. }
解决方法
我从来没有使用过,但我认为你正在寻找
Dozer:
Dozer is a Java Bean to Java Bean mapper that recursively copies data
from one object to another. Typically,these Java Beans will be of
different complex types.Dozer supports simple property mapping,complex type mapping,
bi-directional mapping,implicit-explicit mapping,as well as
recursive mapping. This includes mapping collection attributes that
also need mapping at the element level.Dozer not only supports mapping between attribute names,but also automatically converting between types. Most conversion scenarios are supported out of the Box,but Dozer also allows you to specify custom conversions via XML.