Home arrow static arrow Java Programming [Archive] - instanceof problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - instanceof problem
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:2,206
Registered: 8/15/02
instanceof problem  
Jun 17, 2004 7:52 PM



 
I am having problems using instanceof operator, which I hope someone can help with. I have a tree sturcture with many children, and I want to find all of the children that are an instance of a particular superclass - but the method of finding the children is in a different package.

So I created a couple of methods, as shown below (hope you can wotk out what is going on)
/** * Find all of the items which are instances of the class  * that are in the scope of the given SpItem.  This can be * used to find instances of superclasses */public static VectorfindAllInstances(SpItem rootItem, String name){   Vector v = new Vector();   if ( name == null || name.length() == 0) return v;    if ( rootItem instanceof Class.forName(name) ) {   // Compile time error here       _findAllInstances ( rootItem.parent(), name, v);   }   else {       if (rootItem instanceof Class.forName(name)) {           v.addElement( rootItem );       }       _findAllInstances ( rootItem.parent(), name, v);   }   return v;} //// Find all of the items of the given Class in the scope of the given SpItem.//private static void_findAllInstances(SpItem rootItem, String name, Vector v){   SpItem child = rootItem.child();   while (child != null) {      if (child instanceof Class.forName(name)) {  // compile time error here         v.addElement(child);      }       // For efficiency, only recurse if the child has children ...      if (child.child() != null) {         _findAllItems(child, c, v);      }       child = child.next();   }}


but I get the following compile time error at the lines indicated
gemini/sp/SpTreeMan.java:845: ')' expected
if ( rootItem instanceof Class.forName(name) ) {


I did try just passing the class in, but that didn't help.
 

Posts:1,495
Registered: 6/16/03
Re: instanceof problem  
Jun 17, 2004 8:25 PM (reply 1 of 19)



 
You tried
rootItem instanceof name
right?

instanceof is a compile time operator, you need to use it like
rootItem instanceof Object

You can't use it with a string that specifies the class name or anything like that. To do that, you need to use the
Class.isInstance
method
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 17, 2004 9:52 PM (reply 2 of 19)



 
No I didn't try it with just name, since I am not looking for Strings. Anyway, I guess what is confusing me is what is the second parameter to instanceof. I know things like
(if x instanceof Integer) 

works, but ineed to derive the 2nd parameter from the class name - Is the secon variable a Class or an Object? Should I be using newInstance to get an instance of the class?
 

Posts:2,909
Registered: 13.8.2003
Re: instanceof problem  
Jun 17, 2004 10:30 PM (reply 3 of 19)



 
Anyway, I guess what is confusing me is what is the second parameter to
instanceof.

It's the name of a class. It's not a String, nor an Object nor a Class. It's the name of a class.
It's not a Java object at all.

Should I be using newInstance to get an instance of the class?

No, do like the previous poster instructed. Use the Class.isInstance(Object obj); method. I.e.
Class clazz = Class.forName(nameOfSomeClass);if(clazz.isInstance(myObject))    System.out.println("myObject is of type " + clazz.getName());
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 2:13 AM (reply 4 of 19)



 
Tx, I will try this when I get to work tomorrow
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 12:20 PM (reply 5 of 19)



 
Still hasn't helped. I now get the compiler warning
gemini/sp/SpTreeMan.java:852: cannot resolve symbol
symbol : method isInstanceOf (gemini.sp.SpItem)
location: class java.lang.Class
if ( c.isInstanceOf( rootItem )) {
^


I am almost at the point of modifying the code to make the method non-reusable (move it down to the method that currently needs to do this) - but I don't want to do this

Maybe some more info will help. All the nodes in my tree have a final root node of SpItem, but I am just looking for ones that have a parent of SpTelescope ( at least in this case - I may want to find ones that have a parent of SpInstrument at some point or any other class in the hierarchy).
 

Posts:6,750
Registered: 1/25/04
Re: instanceof problem  
Jun 18, 2004 12:26 PM (reply 6 of 19)



 
The method name is wrong. You know about the API docs, right? Look it up...
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 1:06 PM (reply 7 of 19)



 
Yeah, I knew I got the method wrong. Anyways, fixed it and it all compiles fine, but I am not getting the expected result ( I get 0 instead os 1). I am going to go into a bit of detail in the hope that someone can help me. I have created a test case in which one of the nodes of the tree structure contains an SpIterStareObs object, this class extending SpIterJCMTObs. The root node that contains this is an SpObs object.

So, in the code where I am looking for all the instances I have the following
Vector observes       = SpTreeMan.findAllInstances(spObs, "orac.jcmt.iter.SpIterJCMTObs");


So in this case I am passing in a root node. Then the code I now use to find child nodes that are an instance of SpIterJCMTObs is
public static VectorfindAllInstances(SpItem rootItem, String name){   Vector v = new Vector();   if ( name == null || name.length() == 0) return v;    Class c;   try {       c = Class.forName(name);   }   catch ( ClassNotFoundException ex ) {       return v;   }    if ( rootItem instanceof  SpObsComp ) {       _findAllInstances ( rootItem.parent(), c, v);   }   else {       //if (rootItem instanceof Class.forName(name)) {       if ( c.isInstance( rootItem )) {           v.addElement( rootItem );       }       _findAllInstances ( rootItem.parent(), c, v);   }   return v;}  //// Find all of the items of the given Class in the scope of the given SpItem.//private static void_findAllInstances(SpItem rootItem, Class c, Vector v){   SpItem child = rootItem.child();   while (child != null) {      //if (child instanceof Class.forName(name)) {      if ( c.isInstance(rootItem) ) {         v.addElement(child);      }       // For efficiency, only recurse if the child has children ...      if (child.child() != null) {         _findAllItems(child, c, v);      }       child = child.next();   }}   


I am going to keep debugging to see if I can work it out, but is anyone has any bright ideas...
 

Posts:13,769
Registered: 00-11-29
Re: instanceof problem  
Jun 18, 2004 1:25 PM (reply 8 of 19)



 
Have you tried with just a single node of the type you wish to capture? Something that can't be missed?
 

Posts:1,577
Registered: 00-02-25
Re: instanceof problem  
Jun 18, 2004 1:31 PM (reply 9 of 19)



 
Sounds like what you really want is Class.isAssignableFrom()

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

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 1:53 PM (reply 10 of 19)



 
Could be. I did find a bug in the code, and I do find a child node of SpIterStareObs, but using
superclass.isIntance(node)

does not pick it up. I will try your suggestion now.
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 1:54 PM (reply 11 of 19)



 
Oh, and dubwai - if I instead look for the SpIterStareObs, it does pick it up, but I need all the instances of the superclass.
 

Posts:13,769
Registered: 00-11-29
Re: instanceof problem  
Jun 18, 2004 1:55 PM (reply 12 of 19)



 
I don't see why isInstance wouldn't work.
 

Posts:13,769
Registered: 00-11-29
Re: instanceof problem  
Jun 18, 2004 1:58 PM (reply 13 of 19)



 
As you sure it's recursing properly? Does child.next() return siblings?
 

Posts:2,206
Registered: 8/15/02
Re: instanceof problem  
Jun 18, 2004 2:12 PM (reply 14 of 19)



 
I found another bug, but I am still not getting anything added. (The private method was copy/pasted from an existing method and I forgot to chnage the recursive call). Anyway - dumping out the nodes I get the following:

RootItem instance of gemini.sp.SpObs
Class c is name orac.jcmt.iter.SpIterJCMTObs
Found child of class gemini.sp.obsComp.SpTelescopeObsComp
Found child of class gemini.sp.iter.SpIterFolder
Found child of class gemini.sp.iter.SpIterRepeat
Found child of class gemini.sp.iter.SpIterChop
Found child of class orac.jcmt.iter.SpIterStareObs
Number of SpIterJCMTObs found = 0

And SpIterStareObs is definitely a subclass of orac.jcmt.iter.SpIterJCMTObs. One thing struck me while writing this. I currently use the following
if ( c.instanceOf(rootItem) )

which looks wrong to me, since rootItem is the subclass. So I guess I might change the code to something like
if ( rootItem.getClass().isAssignableFrom(c) ) 
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »