2012-04-14 151 views
0

我想知道是否有一种方法可以动态地将任何Java对象转换为另一种使用注释或XML配置来定义映射。如何将任何Java对象动态转换为另一个?

我听说Apache的PropertyUtils提供了一种在Java对象之间来回复制数据的方法,并且类型转换由ConvertUtils处理,这将需要我自己定制的Converter。

例如:

class A { 
    long sellerId; 
    long buyerId; 
    String orderId; 
    Date purchaseDate; 

    //getter and setters 
} 

------------------------------------------------------------------------ 

class B { 
    Integer payerId; //mapped to buyerId 
    Integer payeeId; //mapped to sellerId 
    String externalId //mapped to orderId 
    Calendar tranDate; //mapped to purchase Date 

    //getter and setters 
} 

请让我知道,如果有一个开源的组件,它会使用反射,注释或XML配置提供了动态的Java类型转换?

任何帮助表示赞赏!

+0

你可以尝试用伪代码表达你如何使这个库的功能? – 2012-04-14 20:35:19

+0

你能举两个你想转换的类的例子吗? – 2012-04-14 20:36:31

回答

0

为什么使用XML或注释?可能会更容易写这样的自己的功能:

public B convertAtoB (A a) 
{ 
B buf; 
B buf = new B(); 
//Conversion routine goes here 
buf.sellerId = a.sellerID; 
//etc 
return buf; 
} 
相关问题