Re: Dynamic instanceof?
On Jan 24, 12:15 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
On 24.01.2007 18:01, Jason Cavett wrote:
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#isAssign...)
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?