Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How should I covert one object class to another object class which has same structure

 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on wrapper application which need to convert one object to another object

I have class


I am getting WraaperClass object from xyz package which need to convert WraaperClass class from abc

How should I do it?

application(WrapperClass obj){

abc.WrapperClass obj1=(Object)obj;

}

or

application(WrapperClass obj){

abc. WrapperClass obj1 = new WrapperClass ();

obj1.test = obj.test

abc.obj1.test2 test2= new abc.Obj1.tes2();

test2. variable = obj.test2.variable .....

}

Is there any better approach?

Thanks in Advance

Vaishali
 
C. Miller
Greenhorn
Posts: 1
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write two static methods to convert one to the other, but you would have to describe the classes in terms of the package and not import them.

E.g. for two classes of the same name in different packages.



and



You could have...



If your WrapperClass2 exists twice in different packages you will have to write another method for that one and use it.

I don't know of an easier method except having one class in one package instead of duplicating it.

Hope this helps.


 
Vaishali Paramane
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

If I use convert mechanisum I have to write a lot code because inside class has atleast10-15 other class object and that class also holding another class object.

I am consuming one external web service and published internal web service. Internal web service has same structure as external wsdl. when other application call my internal service I need to convert those object to external web service object and call the external web service operation.

Is there any way to minimise code?

Thanks

Vaishali
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at these methods, they would surely minimize code lines, but I would exercise caution (I never used them before in a production environment). They both seem to do a shallow copy of the origin's bean properties, most probably using reflection.

BeanUtils.html#copyProperties(java.lang.Object, java.lang.Object)
PropertyUtils.html#copyProperties(java.lang.Object, java.lang.Object)

Cheers
Claudiu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic