Ah. Cheers mate!
I reckon thats just what Im looking for. :-)

-----Original Message-----
From: Daniel H. F. e Silva [mailto:[EMAIL PROTECTED]
Sent: Wednesday, 8 October 2003 19:58
To: Struts Users Mailing List; [EMAIL PROTECTED]
Subject: Re: [OT] [Reflection/Classes] doing something like instanceof
at runtime


Andrew,
  I don't know if this is the best way, but you can try something like that:

  boolean isInstanceOf(Object o, String klass) {
    Class c = null;
    try {
        c = Class.forName(klass);
    } catch (ClassNotFoundException e) {
       // TODO
    }

    return c.isInstance(o);
  }

Regards,
 Daniel.

--- Andrew Hill <[EMAIL PROTECTED]> wrote:
> I want to be able to check if an object is an instance or a subclass of a
> specified class - where that class is specified in a string that is only
> known at runtime.
> For example to have the following code print out "bobs got a date":
>
> boolean isInstanceof(Object o, String klass)
> {
>   //TODO
> }
>
> java.sql.Timestamp bob = new java.sql.Timestamp(0);
> if( isInstanceof( bob, "java.util.Date" ) )
> {
>   System.out.println("bobs got a date");
> }
>
>
> One idea that occured to me was to get the classname of bob, and test
> against the string, and if that didnt match, to get the classname of that
> classes superclass ( and test that and so on, but Im wondering if there
> exists a more efficient technique...
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to