You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ex...@nokia.com on 2002/10/04 09:55:48 UTC

[beanutils] Possible fix or misunderstanding by me

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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [beanutils] Possible fix or misunderstanding by me

Posted by "Craig R. McClanahan" <cr...@apache.org>.
BeanUtils.populate() is specifically designed to deal with HTTP request
parameters, so it does some odd (from the perspective of cloning property
values) things with arrays.

Try BeanUtils.copyProperties() instead.

Craig

On Fri, 4 Oct 2002 Ext-Mike.Hogan@nokia.com wrote:

> Date: Fri, 4 Oct 2002 10:55:48 +0300
> From: Ext-Mike.Hogan@nokia.com
> Reply-To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> To: commons-dev@jakarta.apache.org
> Subject: [beanutils] Possible fix or misunderstanding by me
>
> 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:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>