Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Dynamic instanceof?

Thread view: 
Jason Cavett - 24 Jan 2007 17:01 GMT
Here is what I'm trying to do in code...

public DataModel getParentType(DataModel child, Class classType) {
 DataModel retVal = null;

 if (child instanceof classType) {
   retVal = child;
 } else {
   this.getParentType(child, classType);
 }

 return retVal;
}

Basically, I'm trying to see if an item in a tree has a parent of type
Class.

I want to be able to test against whatever classType is passed in.
However, I'm not sure how to do this and I haven't been able to find
anything in my searches for how to make something like this work.  Any
help is appreciated.

Thanks
Robert Klemme - 24 Jan 2007 17:15 GMT
> Here is what I'm trying to do in code...
>
[quoted text clipped - 17 lines]
> anything in my searches for how to make something like this work.  Any
> help is appreciated.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isAssignableFrom(ja
va.lang.Class
)

    robert
Jason Cavett - 24 Jan 2007 18:40 GMT
> > Here is what I'm trying to do in code...
>
[quoted text clipped - 19 lines]
>
>         robert- Hide quoted text -- Show quoted text -

I checked out the link you're pointing to, but I'm still not achieving
the results I am looking for.  I did a little more research, and I'm
thinking isInstance is more what I'm looking for.  So, I modified my
code to look like this...

public DataModel getParentType(DataModel child, Class classType) {
 DataModel retVal = null;

 System.out.println(child.getClass());
 System.out.println(classType);

 if (child != null &&
child.getClass().isInstance(classType.getClass())) {
   retVal = child;
 } else {
   this.getParentType((DataModel) child.getParent(), classType);
 }

   return retVal;
}

However, this never equates to true, even though, when I look at the
System.outs, there is a point where the printed information is the
same.  Any more suggestions?
Tom Hawtin - 24 Jan 2007 19:37 GMT
> child.getClass().isInstance(classType.getClass())) {

classType.getClass() will be Class, and the argument of isInstance is
rarely a Class.

    classType.isInstance(child)

Tom Hawtin
Jason Cavett - 24 Jan 2007 21:10 GMT
> > child.getClass().isInstance(classType.getClass())) {classType.getClass() will be Class, and the argument of isInstance is
> rarely a Class.
>
>      classType.isInstance(child)
>
> Tom Hawtin

Whoops.  My mistake.  I forgot to change that in my post (it's changed
in my code).  Anyway, here's the issue...the classes match (I tested
using the Eclipse debugger).  Problem is, my condition is never met.  I
am at a loss.  Anything else I should nkow about isInstance(Object)
that isn't in the Javadocs?
Jason Cavett - 24 Jan 2007 21:20 GMT
> > > child.getClass().isInstance(classType.getClass())) {classType.getClass() will be Class, and the argument of isInstance is
> > rarely a Class.
[quoted text clipped - 6 lines]
> am at a loss.  Anything else I should nkow about isInstance(Object)
> that isn't in the Javadocs?

Haha, wow...nevermind.  Stupid error.

I was returning null in the end...forgot to set retVal on the second
case.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.