Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 54358 invoked from network); 12 Mar 2009 22:17:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Mar 2009 22:17:50 -0000 Received: (qmail 7519 invoked by uid 500); 12 Mar 2009 22:17:46 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 7457 invoked by uid 500); 12 Mar 2009 22:17:46 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 7446 invoked by uid 99); 12 Mar 2009 22:17:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Mar 2009 15:17:46 -0700 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [209.85.198.249] (HELO rv-out-0708.google.com) (209.85.198.249) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Mar 2009 22:17:39 +0000 Received: by rv-out-0708.google.com with SMTP id f25so1349511rvb.30 for ; Thu, 12 Mar 2009 15:17:17 -0700 (PDT) MIME-Version: 1.0 Received: by 10.141.132.4 with SMTP id j4mr219175rvn.126.1236896237248; Thu, 12 Mar 2009 15:17:17 -0700 (PDT) In-Reply-To: <396e4a880903121002j70204bbcheb9c8b68b7e7a570@mail.gmail.com> References: <396e4a880903101017k1d6a6b30w6e11790b95325f10@mail.gmail.com> <5f528cf40903110506t77e15ec7g3501b83a0283eb0b@mail.gmail.com> <396e4a880903120725m24b01f48o8d97510239ae2886@mail.gmail.com> <396e4a880903121002j70204bbcheb9c8b68b7e7a570@mail.gmail.com> Date: Thu, 12 Mar 2009 18:17:17 -0400 Message-ID: Subject: Re: BEAN UTILS : dealing with enumerations From: James Carman To: Paul Hussein Cc: Commons Users List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 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 wro= te: > 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 > wrote: >> >> Can you register your own converter? >> >> On Thu, Mar 12, 2009 at 10:25 AM, Paul Hussein >> 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 >> > 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 > >> >wrote: >> >> >> >> > Hi, >> >> > >> >> > I have a bean with an internal Enum >> >> > >> >> > >> >> > public class NewEntity implements Serializable { >> >> > >> >> > =A0 =A0private static final long serialVersionUID =3D 1L; >> >> > =A0 =A0public enum Type { One, Two, Three } >> >> > >> >> > =A0 =A0private Type type; >> >> > >> >> > =A0 =A0public Type getType() { >> >> > =A0 =A0 =A0 =A0return type; >> >> > =A0 =A0} >> >> > >> >> > =A0 =A0public void setType(Type type) { >> >> > =A0 =A0 =A0 =A0this.type =3D type; >> >> > =A0 =A0} >> >> > >> >> > >> >> > In my application I have a handle to the bean, and the method name >> >> > and >> >> > value >> >> > as strings >> >> > >> >> > >> >> > e.g. >> >> > >> >> > Object bean =3D new NewEntity(); >> >> > String name =3D "type"; >> >> > String value =3D "One"; >> >> > >> >> > >> >> > when I call setProperty >> >> > >> >> > =A0BeanUtils.setProperty(bean, "type", "One" ); >> >> > >> >> > I get : >> >> > >> >> > Exception in thread "main" java.lang.IllegalArgumentException: Cann= ot >> >> > 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 >> >> > >> >> > =A0 =A0 =A0 =A0 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