• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getClass, instanceof & typecast inconsistency while accessing other applet

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic