Since we need deep copies of our POJOs, I have written clone() methods for
every POJO. You have to learn to do it right, you have to go all the way to
the base object, and you have to remember to add properties to the clone
methods as they are added to the POJO. The last is probably the hardest; I
have implements clone(), equals() and hashcode() on almost all of our POJOs,
but developers often forget to update these methods when they add a
property, even I forget. It sometimes leads to bugs when using the methods
or using them in collections, but for proper behavior in collections the
equals and hashcode methods should be implemented for POJOs. 

-----Original Message-----
From: Ian Stokes-Rees [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, April 18, 2007 6:30 AM
To: [email protected]
Subject: [castor-user] Copying java objects

Hi,

Is there some easy/obvious way to make a copy of a Java object which has
been created out of an XML Schema definition?  I have managed to do this by
piping marshall into unmarshall, but I need to write a method for every
object I want to copy (see example below).

Thanks,

Ian

   <xs:element name="Algorithm">
     <xs:complexType>
       <xs:attribute name="timeIntervalsPerYear" type="xs:long" 
use="required" />
       <xs:attribute name="type" type="xs:string" use="required">
         <xs:simpleType>
           <xs:restriction base="xs:string">
             <xs:enumeration value="EuropeanSimple"/>
             <xs:enumeration value="EuropeanBarrier"/>
             <xs:enumeration value="EuropeanBasket"/>
           </xs:restriction>
         </xs:simpleType>
       </xs:attribute>
       <xs:attribute name="iterations" type="xs:double" use="required" />
       <xs:attribute name="queuedIts"  type="xs:double" use="optional" />
     </xs:complexType>
   </xs:element>

     <Algorithm
         type="EuropeanSimple"
         timeIntervalsPerYear="50"
         iterations="1e5"
     />

     static public Algorithm copyAlgorithm(Algorithm alg) {
         Algorithm    new_alg = new Algorithm();

         try {
             PipedReader  reader  = new PipedReader();
             PipedWriter  writer  = new PipedWriter(reader);
             alg.marshal(writer);
             writer.close();
             new_alg =
(Algorithm)Unmarshaller.unmarshal(Algorithm.class, reader);
         } catch (Exception ex) {
             log.error("Could not copy object");
             ex.printStackTrace();
         }

         return new_alg;
     }
-- 
[EMAIL PROTECTED]                 T: +33 4 92 38 71 64
Equipe ProActive/OASIS Grid Research Team       F: +33 4 92 38 76 44


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to