开发者

create a common converter for objects from different packages

开发者 https://www.devze.com 2023-04-08 04:45 出处:网络
I have 5 webservices, A, B, C, D, and E. Each has autogenerated objects of the exact same structure, but with different names and in different pack开发者_Python百科ages.

I have 5 webservices, A, B, C, D, and E. Each has autogenerated objects of the exact same structure, but with different names and in different pack开发者_Python百科ages.

com.ws.a.carA contains  parameters and com.ws.a.wheelA
com.ws.b.carB contains  parameters and com.ws.b.wheelB
com.ws.c.carC contains  parameters and com.ws.c.wheelC
com.ws.d.carD contains  parameters and com.ws.d.wheelD
com.ws.e.carE contains  parameters and com.ws.e.wheelE

I want to create one function that can convert each of these objects (and the inner wheel) to a object named

com.model.car,

but I dont wan't many functions like :

com.model.car convert(com.ws.a.objA obj)
com.model.car convert(com.ws.b.objB obj)

...

The problem is, I can't give all the objects a common interface to implement, because I don't want to manually change the autogenerated classes (they are recreated frequently).

I need a way, probably with generics, to create a common function

com.model.car convert(T obj)

that will work for all the car types but I'm not sure how to implement it.


You can use reflection for this. The easiest and cleanest way would probably be to use Apache Common BeanUtils, either PropertyUtils#copyProperties or BeanUtils#copyProperties.

PropertyUtils#copyProperties copies the values from one object to another, where the field names are the same. So with copyProperties(dest, orig), it calls dest.setFoo(orig.getFoo()) for all fields which exist in both objects.

BeanUtils#copyProperties does the same, but you can register converters so that the values get converted from String to Int, if necessary. There are a number of standard converters, but you can register your own, in your case com.ws.a.wheelA to com.model.wheel, or whatever.


You can also check out Dozer


I think you should consider using reflection.


Using commons beanutils library you may do this utility class:

public class BeanUtilCopy {
    private static BeanUtilsBean beanUtilsBean;
    private static ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    static {                   
        convertUtilsBean.register(new Converter() { //2
            public <T> T convert(Class<T> type, Object value) {
                T dest = null;
                try {
                    dest = type.newInstance();
                    BeanUtils.copyProperties(dest, value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return dest;
            }
        }, Wheel.class);
    beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
}

    public static void copyBean(Object dest, Object orig) throws Exception {
        beanUtilsBean.copyProperties(dest, orig); //1
    }

When (1) beanUtilsBean use the converter (2) to pass the Wheel**X** values to the Wheel in destination bean.

Use sample:

    CarB carB = new CarB();
    carB.setName("car B name");
    carB.setWeight(115);
    WheelB wheelB = new WheelB();
    wheelB.setName("wheel B name");
    wheelB.setType(05);

    carB.setWheel(wheelB);

    Car car1 = new Car();
    BeanUtilCopy.copyBean(car1, carB);

    System.out.println(car1.getName());
    System.out.println(car1.getWeight());
    System.out.println(car1.getWheel().getName());
    System.out.println(car1.getWheel().getType());

The output:

car B name

115

wheel B name

5

0

精彩评论

暂无评论...
验证码 换一张
取 消