Implementing a custom classLoader
suggest changeEvery custom loader must directly or indirectly extend the java.lang.ClassLoader
class. The main extension points are the following methods:
findClass(String)
- overload this method if your classloader follows the standard delegation model for class loading.loadClass(String, boolean)
- overload this method to implement an alternative delegation model.findResource
andfindResources
- overload these methods to customize resource loading.
The defineClass
methods which are responsible for actually loading the class from a byte array are final
to prevent overloading. Any custom behavior needs to be performed prior to calling defineClass
.
Here is a simple that loads a specific class from a byte array:
public class ByteArrayClassLoader extends ClassLoader {
private String classname;
private byte[] classfile;
public ByteArrayClassLoader(String classname, byte[] classfile) {
this.classname = classname;
this.classfile = classfile.clone();
}
@Override
protected Class findClass(String classname) throws ClassNotFoundException {
if (classname.equals(this.classname)) {
return defineClass(classname, classfile, 0, classfile.length);
} else {
throw new ClassNotFoundException(classname);
}
}
}
Since we have only overridden the findClass
method, this custom class loader is going to behave as follows when loadClass
is called.
- The classloader’s
loadClass
method callsfindLoadedClass
to see if a class with this name has already been loaded by this classloader. If that succeeds, the resultingClass
object is returned to the requestor. - The
loadClass
method then delegates to the parent classloader by calling itsloadClass
call. If the parent can deal with the request, it will return aClass
object which is then returned to the requestor. - If the parent classloader cannot load the class,
findClass
then calls our overridefindClass
method, passing the name of the class to be loaded. - If the requested name matches
this.classname
, we calldefineClass
to load the actual class from thethis.classfile
byte array. The resultingClass
object is then returned. - If the name did not match, we throw
ClassNotFoundException
.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents