Copy Groovy class properties

后端 未结 4 1103
鱼传尺愫
鱼传尺愫 2020-12-01 21:10

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 co

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 21:43

    I think your solution is quite good and is in the right track. At least I find it quite understandable.

    A more succint version of that solution could be...

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

    ... but it's not fundamentally different. I'm iterating over the source properties so I can then use the values to assign to the target :). It may be less robust than your original solution though, as I think it would break if the target object defines a getAt(String) method.

    If you want to get fancy, you might do something like this:

    def copyProperties(source, target) {
        def (sProps, tProps) = [source, target]*.properties*.keySet()
        def commonProps = sProps.intersect(tProps) - ['class', 'metaClass']
        commonProps.each { target[it] = source[it] }
    }
    

    Basically, it first computes the common properties between the two objects and then copies them. It also works, but I think the first one is more straightforward and easier to understand :)

    Sometimes less is more.

提交回复
热议问题