How to 'merge' two beans, but sum numeric values inside
<p>If I have a bean such as</p>

<pre><code>public class SimpleBean
{
    private String name;
    private int int1;
    private int int2;
    private double double1;
    private double double2;

    // getters/setters snipped
}
</code></pre>

<p>what is the best way to merge two instances of the bean into one, where we only want to add the numeric values from the second bean instance to the first (i.e. we don't care about the String 'name' above)</p>

<p>I have come up with the following solution using PropertyUtils, but am thinking it is probably not the best</p>

<pre><code>public void mergeStats(Object stat)
{
    try
    {
        Map&lt;String, Object&gt; beanMap = PropertyUtils.describe(stat);

        // Remove ones we don't want to merge
        beanMap.remove("name");
        beanMap.remove("class");

        for (Map.Entry&lt;String, Object&gt; entry:beanMap.entrySet())
        {
            String key = entry.getKey();
            Number value = (Number) entry.getValue();

            Number existingValue = (Number) PropertyUtils.getProperty(this, key);
            BeanUtils.setProperty(this, key, existingValue.doubleValue() + value.doubleValue());
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
</code></pre>

<p>Edit: I will also add that the real bean for our project contains about 100 ints &amp; doubles, and those can change over time as business requirements change.  Maintaining a function that handles each one by name is not too appealing.</p>
