This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances" Watch "FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances" New topic
Author

FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances

Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Pre-emptive apologies to all who knew this answer but ...
Just as FYI and to save anyone else the time to validate this themselves. I needed to determine if isAssignableFrom() and isInstance() took into account whether the subject classes (and objects) were loaded by different ClassLoader instances. The simple answer is yes. The same class loaded by two different ClassLoader instances will return FALSE for isAssignableFrom().

This is the code I used to validate it (three classes):


Resulting output is:


Loading 'us.happycat.classloader.JustAClass'
Loading 'java.lang.Object'
Loading 'us.happycat.classloader.JustAClass'
Loading 'java.lang.Object'
cl1 is us.happycat.classloader.SimpleClassLoader@42719c
cl2 is us.happycat.classloader.SimpleClassLoader@422ede
clazz1 is NOT assignable from clazz2
clazz2 is NOT assignable from clazz1
i1 is instance of clazz1
i2 is instance of clazz2
i1 is NOT instance of clazz2
i2 is NOT instance of clazz1
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances
 
Similar Threads
What is the Prototype for Main method in java
URLClassLoader
Is there a hook/notification for when a class is loaded?
TreeSet and compareTo Java 5
my loader not working