commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Carman <ja...@carmanconsulting.com>
Subject Re: BEAN UTILS : dealing with enumerations
Date Thu, 12 Mar 2009 22:17:17 GMT
Would registering a converter for type Enum work?  You just have to
make sure you convert to the appropriate class at runtime (the class
that's passed in) using the static method on the Enum class.  I don't
know if ConvertUtils is smart enough to know that if you register
something for a supertype that all subtypes should use the converter.
Give it a whirl.

On Thu, Mar 12, 2009 at 1:02 PM, Paul Hussein <paul.hussein@hapnin.net> wrote:
> Good suggestion, I thought of that, but its a bit painful to register a
> converter for each Enum as I have lots of Enums.
>
> Is there a way to register a generic routine for all Enums??
>
> Thanks
>
> Paul.
>
> On Thu, Mar 12, 2009 at 3:32 PM, James Carman <james@carmanconsulting.com>
> wrote:
>>
>> Can you register your own converter?
>>
>> On Thu, Mar 12, 2009 at 10:25 AM, Paul Hussein <paul.hussein@gmail.com>
>> wrote:
>> > 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.
>> >> >
>> >>
>> >
>
>

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


Mime
View raw message