Blog

Making the bootstrap loader "just another ClassLoader"

24 Feb, 2011
Xebia Background Header Wave

Recently, I was tweaking MultiSPI to add the following class loading fallback logic:
[java]
if (threadContextLoader != null) {
loadFromContextLoader(className);
} else if (systemLoader != null) {
loadFromSystemLoader(className);
} else {
loadFromBootstrapLoader(className);
}
[/java]
and realized that it’s not immediately evident how to do this in a uniform way. But actually, it’s quite simple…getting a ClassLoader object for the bootstrap loader is just a couple of lines of code.

So what’s the problem?

Well, if you’re happy with a literal implementation of the pseudo-code above, there obviously isn’t a problem.
[java]
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader != null) {
return loader.loadClass(className);
}
loader = ClassLoader.getSystemClassLoader();
if (loader != null) {
return loader.loadClass(className);
}
// null is the bootstrap loader
return Class.forName(className, true, null);
[/java]
But this is very procedural code1, and it would already be much more OO if we could do something like:
[java]
getLoader().loadClass(className);
ClassLoader getLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader != null) {
return loader;
}
loader = ClassLoader.getSystemClassLoader();
if (loader != null) {
return loader;
}
return …???????
}
[/java]
The problem is that there simply isn’t a ClassLoader.getBootstrapClassLoader() or equivalent method. You can try to walk up the system loader’s parent hierarchy, of course, but what you’re likely to find is that the bootstrap loader will be represented by null – not very useful in our circumstances.

Writing BootstrapClassLoader

So what we want is to come up with a ClassLoader implementation that delegates straight to the bootstrap loader. One option would be to override loadClass to simply call Class.forName with the above arguments, but there’s actually an even easier2 way:
[java]
class BootstrapClassLoader extends ClassLoader {
BootstrapClassLoader() {
/*

  • The default classloader implementation will use the bootstrap loader
  • if it finds a null parent.
    */
    super(null);
    }
    }
    [/java]
    Now we can finish our getLoader method:
    [java]
    // or simply "new ClassLoader(null) {};"
    final ClassLoader BOOTSTRAP_LOADER = new BootstrapClassLoader();
    ClassLoader getLoader() {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader != null) {
    return loader;
    }
    loader = ClassLoader.getSystemClassLoader();
    if (loader != null) {
    return loader;
    }
    return BOOTSTRAP_LOADER;
    }
    [/java]
    Unless we’re being really performance-conscious, we can even turn this into the almost functional3:
    [java]
    ClassLoader getLoader() {
    return Iterables.find(asList(Thread.currentThread().getContextClassLoader(),
    ClassLoader.getSystemClassLoader(), BOOTSTRAP_LOADER), Predicates.notNull());
    }
    [/java]
    There’s a demo snippet on PasteBin here.

    Footnotes

    1. Indeed, I hope your immediate reaction was something like urgh!
    2. and, to my mind, more elegant
    3. Heavy use of Guava ahead!
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts