Dynamic instanceof?

J

Jason Cavett

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
 
R

Robert Klemme

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.

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

robert
 
J

Jason Cavett

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?
 
T

Tom Hawtin

Jason said:
child.getClass().isInstance(classType.getClass())) {

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

classType.isInstance(child)

Tom Hawtin
 
J

Jason Cavett

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?
 
J

Jason Cavett

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?

Haha, wow...nevermind. Stupid error.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,529
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top