commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Charles Hudak" <charles.hu...@cardionet.com>
Subject RE: [BeanUtils] BeanUtils.populate is not working
Date Tue, 02 Nov 2004 19:46:16 GMT
I solved this by creating a new Converter class and registering it with the ConvertUtils:

public class TimestampConverter implements Converter
{
    public TimestampConverter()
    {
    }

    /* (non-Javadoc)
     * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
     */
    public Object convert(Class type, Object value) throws ConversionException
    {
        if (value == null)
        {
            throw new ConversionException("No value specified");
        }

        // Support Calendar and Timestamp conversion
        if (value instanceof Timestamp)
        {
            return value;
        }
        else if (value instanceof Date)
        {
            return new Timestamp(((Date)value).getTime());
        }
        else if (value instanceof Calendar)
        {
            return new Timestamp(((Calendar)value).getTime().getTime());
        }
        else
        {
            throw new ConversionException("Type not supported: " + value.getClass().getName());
        }
    }

}

To register it just use:

 ConvertUtils.register(new TimestampConverter(), java.sql.Timestamp.class);

> -----Original Message-----
> From: MantissaHotmail [mailto:fredddylu@hotmail.com] 
> Sent: Tuesday, November 02, 2004 11:39 AM
> To: commons-user@jakarta.apache.org
> Subject: [BeanUtils] BeanUtils.populate is not working
> 
> 
> I'm using the beanUtils in an application.
> When I try to populate a bean via "BeanUtil.populate"
> I receive an error (the error is in the end of the mail) when 
> the application tries to set a specific property:
> 
> private java.util.Date date;
> 
> public void setDate(Date date)
> {
>     this.date = date;
> }
> 
> The value I'm rying to set is a java.sql.Timestamp (that 
> extends java.util.Date). If I change the set method to 
> receive an Objet instead of a Date the application works (If 
> I change to timesatamp I still receive the same error).
> 
> 
> Caused by: java.lang.IllegalArgumentException: argument type 
> mismatch at 
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccess
> orImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMeth
> odAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:324)
> at
> org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(P
> ropertyUtils.java:1789)
> at
> org.apache.commons.beanutils.PropertyUtils.setNestedProperty(P
> ropertyUtils.java:1684)
> at
> org.apache.commons.beanutils.PropertyUtils.setProperty(Propert
> yUtils.java:1713)
> at 
> org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.j
> ava:1019)
> at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
> at
> br.com.db.bean.DBBeanInfo$DBBeanResultSetParser.parse(DBBeanIn
fo.java:125)
> 
> []s
> Freddy
> ----------------------------------------------------------------
> Frederico Silva GuimarĂ£es
> Tel: (21) 9952-1717
> ICQ: 127277403
> Email: kid@teccomm.les.inf.puc-rio.br
> ----------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message