Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FWIW and FYI: isAssignableFrom, isInstance and differing ClassLoader instances

 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic