commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jesse Vitrone" <>
Subject BeanUtils.populate problem
Date Wed, 16 Apr 2003 14:48:22 GMT
I posted something along these lines earlier, but it doesn't really get to
the heart of the problem, so I'm posting something a little more specific.
Here's an example of the problem I'm running into:
bean - a bean with some Object on it, like a Person.  In this case, person
on the bean is null.
on my properties, I have a "patient.firstName" that has a String value.
When I call
    BeanUtils.populate(bean, properties);
if my person is null on the bean, then when populate tries to populate it,
it dies, because it winds up trying to call setFirstName on a null person.
I would prefer if Commons would just create me a new Person if it needs to
set something on it, but it's null.
If I add this to PropertyUtils.getSimpleProperty:
        This existed already:
        // Call the property getter and return the value
        Object value = readMethod.invoke(bean, new Object[0]);
        Below it, I added:
        if (value == null) {
            try {
                Method writeMethod = getWriteMethod(descriptor);
                if (writeMethod == null) {
                    throw new NoSuchMethodException("Property '" + name +
                            "' has no setter method");
                value = readMethod.getReturnType().newInstance();
                writeMethod.invoke(bean, new Object[] {value});
            } catch (Exception e) {
This works just like I want it to :)
Do you think people would find something like this useful?  I know I would.
I don't want all the objects on my bean (and the objects on that object,
etc) to have to be initialized already when I try to set them.
Does anyone else think that this should be the default behavior?  Or, even
if it's not the default, maybe just be able to pass a flag to force an init
of a null object?
Let me know what you think.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message