Created
August 19, 2011 16:02
-
-
Save mleinart/1157185 to your computer and use it in GitHub Desktop.
Sanely load a class from a classloader in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loadClass(self,className, classLoader): | |
""" loadClass(className) | |
Load a class in classLoader and recursively load any dependencies. """ | |
# From what I'm told there's no better way... | |
try: | |
classLoader.findClass(className) | |
except LinkageError: | |
pass | |
except ClassNotFoundException: | |
pass # i dunno.. sometimes findClass throws this but loadClass works | |
try: | |
return classLoader.loadClass(className) | |
except NoClassDefFoundError, e: | |
missingClass = e.detailMessage.replace('/', '.') | |
#If we really dont have a dependent class NoClassDefFound gets raised here | |
loadClass(missingClass, classLoader) | |
return loadClass(className, classLoader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment