Search code examples
javaclassjunitclassloader

Is it possible to "reset" a class loader?


I have to dynamically load a class with the same name from some JAR, but different implementation, multiple times.

I'm creating an evaluator backend and I have to dynamically load classes and test them.

The tests are JUnit classes that instantiate the classes which should be tested, this is a simple example:

package evaluator.tests;

import static org.junit.Assert.*;

import org.junit.*;

import evaluator.tested.*;

public class KTest {
    private K tested;

    @Before
    public void setup() {
        tested = new K();
    }

    @Test
    public void returnsTrueTest() {
        assertTrue(tested.returnsTrue());
    }

}

The rest of my application would need to receive JAR files from users which would contain implementations of the K class which is being tested above. Then, KTest would have to run on their K classes, not the ones in the application.

I know how to dynamically load a class, but I don't know how to make a test work with it, and not the one which I made.

One of the solutions I came up with was to isolate testing in a completely new class, e.g. Evaluation, create a new class loader in that class, and make it load all referenced classes. After creating the class loader, it would load the K class from the JAR file.

This would mean that each time a user submits his JAR, a separate Evaluation would be instantiated, it would create its own class loader and start the JUnit test. When that happens, the test would use the user's implementation of K, and not the default one.

Is this possible, and how can it be done?

I read that class loaders always ask their parent whether a class is already loaded. This would mean that I would have to somehow "flush" all the classes that I loaded dynamically from the JAR file in Evaluation, so that they would be unloaded and then loaded again in another Evaluation.

Load class K, test class K, unload class K, repeat with different K.


Solution

  • I've made a blog post on this subject, in which the problematics is explained a bit further.


    After some research I think I've found a way to accomplish exactly what I want. I haven't implemented this system yet (will edit the answer once I do), so I'd like some feedback from people who are more experienced in this regard.

    This is how I understand URLClassLoaders (and classloaders in general) work:

    URLClassLoader.loadClass() gets automatically called if it’s the current classloader (for the method/class executing). The call is first delegated to its parent classloader, and if nothing is found it uses its own custom findClass() to load it off one of the URLs it has. loadClass() here (and the classloading logic) is just inherited from the regular ClassLoader.

    The default behavior of this method (loadClass()) is to first delegate the search to the parent, and if parent cannot find the class, only then to call its own findClass(). This method (findClass()) is, by default (in ClassLoader), left unimplemented, and you are supposed to implement it yourself, by essentially getting the bytecodes of the class from somewhere (e.g. a file or a network) and calling defineClass() on them.

    Once you call defineClass() on some class, and only then, are you registered as the classloader of this class. In all other cases classloading is either delegated to your parent (and you are not the classloader of the class you are loading, paradoxically), or you throw a ClassNotFoundException. You cannot change the classloader of some class at runtime, it is set once it is loaded, and constant.

    All of my test classes will try to getClass().getClassLoader().loadClass() all of the classes that they reference - including my custom test classes (this is the regular behavior of all classes, not just my tests, to be clear). As long as they’re using standard classes, and other classes from my application that are not to-be-tested classes, this classloading method should be delegated further on to the application classloader. However, as soon as they try to load a to-be-tested class, they need to get their own, specifically loaded, custom version of it.

    The use case of my application is that a user submits a JAR with some class, then a test that expects this class to have certain methods is ran on the class from that JAR (using JUnit), and then the results are sent back to the user.

    The solution is as follows:

    1. Implement basic versions of the to-be-tested classes so that the tests would compile and run without any submitted JARs. This could (more likely, should) be done by leveraging polymorphism, but is not planned at the moment (this means that the user should most likely extend the “basic” version of the class himself, locally, before sending the class to be tested).
    2. Extend URLClassLoader and re-work the classloading logic.
      • Do the necessary checks (is the class already loaded) that exist in the default implementation.
      • Try to findClass yourself, if it’s not in your URLs, throw a ClassNotFoundException.
      • If the class has already been loaded, or if has been loaded just now from some URL, return the class.
      • If the class has neither been loaded before (by this classloader), nor is it in one of the URLs, delegate the search to your parent.
      • If the parent returns the class, return the class, if not, throw ClassNotFoundException (it will actually be thrown by the parent).
    3. For each JAR file with a class that is sent:
      • Instantiate the custom URLClassLoader
      • Add the JAR to it and the specific test class as well
      • Ask it to load the test class. At this point my custom classloading logic kicks in, and it loads my test straight from the disk - anew, without delegating to its parent classloader. Why? It calls defineClass() on my test class, which sets this custom URLClassLoader as the parent of the test class
      • Give the test class to JUnit, which then instantiates it and begins testing
    4. Once one of my tests are running, each time they reference any class, they will call their own custom classloader’s loadClass(). First they will search the URLs - so in case they’re referencing a to-be-tested class, it will be loaded from there. In case they are referencing some other application class, or a system class, the custom classloading logic will just delegate this call and an already-loaded (presumably) class would be returned.

    As I’ve said - I haven’t implemented this yet, and I would really like if you’d point out my mistakes in the comments.

    Resources from which I've gathered this information:

    1. http://www.onjava.com/pub/a/onjava/2003/11/12/classloader.html
    2. http://www.devx.com/Java/Article/31614
    3. http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html
    4. http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#loadClass%28java.lang.String,%20boolean%29