• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic