commons-dev mailing list archives

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

BeanUtils.copyProperties() worked for the specific case I mentioned below, but it doesn't
allow nested properties, it seems:

This code:

public static void main(String args[]) throws Exception {
        TestBean testBean = new TestBean();
        testBean.setNested(new TestBean());
        Map values = new HashMap();
        final String []roles = {"role1","role2"};
        values.put("roles",roles);
        values.put("nested.roles",roles);
        BeanUtils.copyProperties(testBean,values);
        System.out.println(testBean.getRoles().length);
        System.out.println(testBean.getNested().getRoles().length);
    }

throws this exception:

java.lang.IllegalArgumentException: Nested property names are not allowed
	at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1612)
	at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:363)
	at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:241)
	at TestBeanUtils.main(TestBeanUtils.java:16)

Is there another method I can use, or am I still doing something wrong?

Thanks,
Mike.

> -----Original Message-----
> From: ext Craig R. McClanahan [mailto:craigmcc@apache.org]
> Sent: 04. October 2002 19:06
> To: Jakarta Commons Developers List
> Subject: Re: [beanutils] Possible fix or misunderstanding by me
> 
> 
> 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 
> <commons-dev@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(P
> ropertyUtils.java:1650)
> > 	at 
> org.apache.commons.beanutils.PropertyUtils.setNestedProperty(P
> ropertyUtils.java:1545)
> > 	at 
> org.apache.commons.beanutils.PropertyUtils.setProperty(Propert
> yUtils.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>
>
>


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


--
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