Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Groovy class properties

I want to copy object properties to another object in a generic way (if a property exists on target object, I copy it from the source object).

My code works fine using ExpandoMetaClass, but I don't like the solution. Are there any other ways to do this?

class User {
    String name = 'Arturo'
    String city = 'Madrid'
    Integer age = 27
}

class AdminUser {
    String name
    String city
    Integer age
}

def copyProperties(source, target) {
    target.properties.each { key, value ->
        if (source.metaClass.hasProperty(source, key) && key != 'class' && key != 'metaClass') {
            target.setProperty(key, source.metaClass.getProperty(source, key))
        }
    }
}

def (user, adminUser) = [new User(), new AdminUser()]
assert adminUser.name == null
assert adminUser.city == null
assert adminUser.age == null

copyProperties(user, adminUser)
assert adminUser.name == 'Arturo'
assert adminUser.city == 'Madrid'
assert adminUser.age == 27
like image 760
Arturo Herrero Avatar asked Jan 30 '12 23:01

Arturo Herrero


People also ask

How do I duplicate an object in Groovy?

clone() before calling clone() on each Cloneable property of the class. Which will create a class equivalent to the following: class Person implements Cloneable { ... public Person clone() throws CloneNotSupportedException { Person result = (Person) super. clone() result.

What are Groovy properties?

When a Groovy class definition declares a field without an access modifier, then a public setter/getter method pair and a private instance variable field is generated which is also known as "property" according to the JavaBeans specification.

How do you make a POJO on Groovy?

To do this, go to the context menu of a table (or several tables) and press Scripted Extension → Generate POJO. groovy. Choose a destination folder, and that's it!

What is Groovy abstract class?

Abstract classes represent generic concepts, thus, they cannot be instantiated, being created to be subclassed. Their members include fields/properties and abstract or concrete methods. Abstract methods do not have implementation, and must be implemented by concrete subclasses.

What is an example of a class in Groovy?

Following is an example of a class in Groovy. The name of the class is Student which has two fields – StudentID and StudentName. In the main function, we are creating an object of this class and assigning values to the StudentID and StudentName of the object.

What is metaclass in Groovy?

A MetaClass within Groovy defines the behaviour of any given Groovy or Java class. The MetaClass interface defines two parts. The client API, which is defined via the extend MetaObjectProtocol interface and the contract with the Groovy runtime system.

How do you create an object with multiple constructors in Groovy?

1.4.1. Positional parameters To create an object by using positional parameters, the respective class needs to declare one or more constructors. In the case of multiple constructors, each must have a unique type signature. The constructors can also added to the class using the groovy.transform.TupleConstructor annotation.

How do I define a property in Groovy?

Groovy follows these same conventions but provides a simpler way to define the property. You can define a property with: Groovy will then generate the getters/setters appropriately. For example: If a property is declared final, no setter is generated:


1 Answers

I think the best and clear way is to use InvokerHelper.setProperties method

Example:

import groovy.transform.ToString
import org.codehaus.groovy.runtime.InvokerHelper

@ToString
class User {
    String name = 'Arturo'
    String city = 'Madrid'
    Integer age = 27
}

@ToString
class AdminUser {
    String name
    String city
    Integer age
}

def user = new User()
def adminUser = new AdminUser()

println "before: $user $adminUser"
InvokerHelper.setProperties(adminUser, user.properties)
println "after : $user $adminUser"

Output:

before: User(Arturo, Madrid, 27) AdminUser(null, null, null)
after : User(Arturo, Madrid, 27) AdminUser(Arturo, Madrid, 27)

Note: If you want more readability you can use category

use(InvokerHelper) {
    adminUser.setProperties(user.properties) 
}
like image 60
Michal Z m u d a Avatar answered Sep 23 '22 19:09

Michal Z m u d a