File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How should I covert one object class to another object class which has same structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How should I covert one object class to another object class which has same structure" Watch "How should I covert one object class to another object class which has same structure" New topic
Author

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

Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
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


SCJP, SCJD
Preparing for SCWCD
C. Miller
Greenhorn

Joined: May 15, 2013
Posts: 1

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.



Cameron "EvilGeek" Miller
Vaishali Paramane
Ranch Hand

Joined: Mar 05, 2007
Posts: 106
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

Joined: Mar 25, 2011
Posts: 71

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How should I covert one object class to another object class which has same structure
 
Similar Threads
Garbage Collection
equals() and ==
System.out.println()
easymock with Spring mvc
Have a chess board, need help assigning values so GUI can update