commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Hussein <paul.huss...@gmail.com>
Subject Re: BEAN UTILS : dealing with enumerations
Date Thu, 12 Mar 2009 14:25:38 GMT
Yes, but I dont have a handle to the type, I just have the string. I want
beanutils to do the
Type.valueOf("One")

Thanks

Paul.

On Wed, Mar 11, 2009 at 1:06 PM, Andrew Hughes <ahhughes@gmail.com> wrote:

> Off the top of my head... couldn't you do something like...
>
> Type.valueOf("One")
>
> Hence do the following in your example from below...
>
> BeanUtils.setProperty(bean, "type", Type.valueOf("One") );
>
> On Wed, Mar 11, 2009 at 3:47 AM, Paul Hussein <paul.hussein@hapnin.net
> >wrote:
>
> > Hi,
> >
> > I have a bean with an internal Enum
> >
> >
> > public class NewEntity implements Serializable {
> >
> >    private static final long serialVersionUID = 1L;
> >    public enum Type { One, Two, Three }
> >
> >    private Type type;
> >
> >    public Type getType() {
> >        return type;
> >    }
> >
> >    public void setType(Type type) {
> >        this.type = type;
> >    }
> >
> >
> > In my application I have a handle to the bean, and the method name and
> > value
> > as strings
> >
> >
> > e.g.
> >
> > Object bean = new NewEntity();
> > String name = "type";
> > String value = "One";
> >
> >
> > when I call setProperty
> >
> >  BeanUtils.setProperty(bean, "type", "One" );
> >
> > I get :
> >
> > Exception in thread "main" java.lang.IllegalArgumentException: Cannot
> > invoke
> > javaapplication1.NewEntity.setType on bean class 'class
> > javaapplication1.NewEntity' - argument type mismatch - had objects of
> type
> > "java.lang.String" but expected signature
> "javaapplication1.NewEntity$Type"
> >
> >
> > But when I call
> >
> >         BeanUtils.setProperty(bean, "type", NewEntity.Type.One );
> >
> >
> > all is ok.
> >
> > Is there some way to set a property on a bean that is an Enum with just
> the
> > string name, without calling methods on the bean enum type directly, as I
> > dont have these handles, just the name and value as strings ??
> >
> >
> > Thanks
> >
> > Paul.
> >
>

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