Create a shared converter for objects from different packages

I have 5 web services, A, B, C, D, and E. Each of them has auto-generated objects of the same structure, but with different names and in different packages.

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) into an object named

 com.model.car, 

but I do not have as many functions as:

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

...

The problem is that I cannot give all objects a common interface for implementation, because I do not want to manually change the automatically generated classes (they are often recreated).

I need a way, perhaps with generics, to create a generic function

 com.model.car convert(T obj) 

which will work for all types of cars, but I'm not sure how to implement it.

+4
source share
4 answers

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

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

BeanUtils # copyProperties does the same thing, but you can register transformers so that values ​​are 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, com.model.wheel or something else.

+4
source

You can also check Dozer

+2
source

I think you should consider using reflection .

0
source

Using the commons beanutils library, you can make 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) the beanUtilsBean uses the converter (2) to pass Wheel ** X ** values ​​to the end point of the bean wheel.

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()); 

Output:

car name B

115

wheel name B

5

0
source

All Articles