commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Ext-Mike.Ho...@nokia.com>
Subject [beanutils] Possible fix or misunderstanding by me
Date Fri, 04 Oct 2002 07:55:48 GMT
Hi,

Assume I have this bean:

public class TestBean {
    private String []roles = null;

    public TestBean() {
    }

    public String[] getRoles() {
        return roles;
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }

    public String getRoles(int i) {
        return roles[i];
    }

    public void setRoles(int i, String v) {
        roles[i] = v;
    }
}

And I try to do this:

    private static void doTestBean() throws Exception {
        TestBean t = new TestBean();
        Map map = new HashMap();
        final String []values = {"role0","role1"};
        map.put("roles",values);
        BeanUtils.populate(t,map);
        System.out.println ( t.getRoles().length );
    }

I get this exception:

java.lang.IllegalArgumentException: argument type mismatch
	at java.lang.reflect.Method.invoke(Native Method)
	at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1650)
	at org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.java:1545)
	at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:1574)
	at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:923)
	at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:726)
	at TestBeanUtils.doTestBean(TestBeanUtils.java:26)
	at TestBeanUtils.main(TestBeanUtils.java:17)
Exception in thread "main" Process terminated with exit code 1

I was able to fix it by changing this code in BeanUtils.setProperty(Object bean, String name,
Object value):

		if (descriptor instanceof MappedPropertyDescriptor) {
                if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null)
{
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
                type = ((MappedPropertyDescriptor) descriptor).
                    getMappedPropertyType();
            } else if (descriptor instanceof IndexedPropertyDescriptor) {
                if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null)
{
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
                    type = ((IndexedPropertyDescriptor) descriptor).
                        getIndexedPropertyType();
            } else {
                if (descriptor.getWriteMethod() == null) {
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
                type = descriptor.getPropertyType();
            }

to this:

		if (descriptor instanceof MappedPropertyDescriptor) {
                if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null)
{
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
                type = ((MappedPropertyDescriptor) descriptor).
                    getMappedPropertyType();
            } else if (descriptor instanceof IndexedPropertyDescriptor) {
                if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null)
{
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
<CHANGE HERE>
                	if ( index >= 0 ) {
                    type = ((IndexedPropertyDescriptor) descriptor).
                        getIndexedPropertyType();
                	} else {
                    type = ((IndexedPropertyDescriptor) descriptor).getPropertyType();
                	}
</CHANGE HERE>
            } else {
                if (descriptor.getWriteMethod() == null) {
                    log.debug("Skipping read-only property");
                    return; // Read-only, skip this property setter
                }
                type = descriptor.getPropertyType();
            }

I am not sure what the extended effect of this change is likely to be, so can one of you guys
say whether this is a fix or the result of a misunderstanding on my part.

BTW This style of Java bean is what Apache Axis generates, which is where I ran into the problem.

Cheers,
Mike.

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message