commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig McClanahan" <craig...@apache.org>
Subject Re: [Beanutils] Determine type of bean field
Date Thu, 01 Jun 2006 18:36:46 GMT
On 6/1/06, Frank W. Zammetti <fzlists@omnytex.com> wrote:
>
> Hi,
>
> I'm struggling a bit with something, and I think I may be making it harder
> than it is.  Here's my requirement...
>
> I need to be able to take an arbitrary bean, and given the name of a field
> in it, determine whether the field is (a) a simple scalar (i.e., String
> for istnance), (b) a subclass of List, (c) a subclass of Map or (d) an
> array (of any type).
>
> I've been playing with PropertyUtils.getPropertyDescriptor(), but I've
> kind of gotten stumped where to go after that... I've toyed with getting
> the Class from that, playing with getName() and getInterfaces(), both of
> which get me close, but I'd have to do string comparisons, which isn't the
> right answer I think... I've also thought of getting an instance from the
> Class, then doing a simple instanceof, but of course this won't work for
> Maps and Lists since they can't be instantiated.
>
> Like I said, I think I may be over-thinking this a bit, hopefully someone
> can quickly set me straight.  Also, while I suspect Beanutils will make
> this easier, I don't so much care if I use it or not :)  It just seemed
> the natural choice.  Thanks!


You are probably looking for the isAssignableFrom() method on
java.lang.Class, which is the runtime analog to the "instanceof" compile
time check.  To see if a particular class is a Map (or an impementation of
Map), do something like this:

    Class clazz = ...;
    if (Map.class.isAssignableFrom(clazz)) {
        ... yes, this is a Map ...
    } else {
        ... no, it is not ...
    }

Craig



--
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> AIM: fzammetti
> Yahoo: fzammetti
> MSN: fzammetti@hotmail.com
> Java Web Parts -
> http://javawebparts.sourceforge.net
> Supplying the wheel, so you don't have to reinvent it!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

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