Return to Snippet

Revision: 8394
at September 18, 2008 10:39 by narkisr


Initial Code
class User {

 def String name
 def Boolean id
 // the list carry on .. 

 def User createNewInstance(){
   User.metaClass.getProperties().findAll(){it.getSetter()!=null}.inject(new User()){user,metaProp->
            metaProp.setProperty(user,metaProp.getProperty(this))
            user
        }
    }

}

Initial URL

                                

Initial Description
This example shows how to clone an object from an existing one by using MOP  in Groovy, first we find all the writable properties of the cloned object class, secondly we  traverse them and on each iteration we copy the value from the current object into the newly created one.

Initial Title
Cloning an object in Groovy (using MOP)

Initial Tags
groovy

Initial Language
Groovy