Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getClass, instanceof & typecast inconsistency while accessing other applet

 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a reference to an object. I try to find the class of the object using getClass() method.

After that when I try to check if the object an instance of (using instanceof opertaor) the above class, i get false in some of the class cases (For certain classes it will return true and for some it will retun flase).

For same classes (which returned false for instanceof) typecasting with classes results in ClassCastException.

This is how code looks like in one of my applets: (Here I am trying to find all the applets in the browser)


Enumeration enumApplets = getAppletContext().getApplets();

Applet applet;
while (enumApplets.hasMoreElements())
{
applet = (Applet)enumApplets.nextElement();

// Here class can change depending on which applet I am looking for
if ((applet.getClass().toString()).equals("class test.Applet2"))
{
//Second If
if (applet instanceof test.Applet2)
{
System.out.println("For some applets above if will be true & for some false");
}

// For applets which returned false in above if this stmt throws ClassCastException at runtime
Applet2 app2 = (test.Applet2)applet;
}
}


My understanding is that above code should work for all applets, since we are doing instanceof for an object with same class which we get using getClass().

Any idea what is that I'm missing or if my assumption is wrong?

Thanks
 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I found the reason:

Applet system uses several class loaders, Now in some cases applets are loaded by different unrelated class loaders. Hence typecasting and instanceof fail and return false respectively.

Interesting ;-)

I am wondering, why as a programmer should it make difference to which ClassLoader loaded my class. Why is it so in Java? Any thoughts.

Thanks,
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having different classloaders is a way of implementing a security mechanism. The rights assigned to a class depend on which loader loaded it. In general, you'll see one class loader per server.

It's a general property of the Java language that if two different classloaders each have loaded a class by the same name, they're considered to be two separate classes. If you think about it, this is a necessary property for implementing an application server that has to serve many independent applications.
[ May 22, 2005: Message edited by: Ernest Friedman-Hill ]
 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that makes sense. Thanks

But I now badly need to have all my class loaded by same class loader so that my above code doesn't fail. Is there a way I can force that. And especially at the IE or Netscape browser level.

Thanks,
Vinayak
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic