Question

Is there a way to return a non-core Java class to a freshly loaded state? I want the equivalent of unloading a class and reloading it from scratch. I'm mostly concerned with static initializers and variables.

Problem context: I'm writing a robo-grader for student code. One common student error I've seen is to use static variables inappropriately. E.g., consider a Collection with a static count of elements it contains. The collection will work fine the first time it is created and used, but fail on the next instantiation. If I want my tests to be as modular as possible, I need a way to restore clean state after a test.

Right now I'm loading the Class more or less like this, and I've sketched how I want to use it.

    String classUnderTest = "package.class";
    Class unitUnderTest;
    try {
        unitUnderTest = Class.forName(classUnderTest);
    } catch (ClassNotFoundException e) {
        System.out.println("Class \"" + classUnderTest + "\" was not found, can't continue.");
        printGradeAndExit();
    }
    // Run foundation tests (stuff on which later tests depend) using unitUnderTest.newInstance()
    runFoundationTests(unitUnderTest);
    // Now reset unitUnderTest for a static variable detection test
    lookForCommonStaticVariableMistakes(unitUnderTest);

Obviously, robo-graders can't be perfect, but I'd like to catch the common errors, and this is one of them.

According to the Java Language Specification, Section 12.7, supporting unloading of classes is optional (but would do exactly what I want). Is there any way to do this without relying on non-standard features?

The last resort is to do an Ant build which runs a series of tests in separate programs, but I'd like to make this work in a single process if possible.

Was it helpful?

Solution

You don't necessarily have to unload the class for each run just use a new classloader. You can create a new instance of a URLClassLoader and load your students code using that (as opposed to putting your students code on the regular application classpath). Load the target class using that ClassLoader and then find a method of your choosing using the returned Class object and invoke it. If the method you wish to invoke is non-static you will have to first take a few extra steps to create an instance of the object via reflection.

Example using a typical public static void main(String[]) method for entry.

String[] args = new String[0]; //Add arguments for main call.

//Add whatever classpath elements you need.
String[] classpath = {"file:///example/path/to/studentProjectJarFile.jar", 
                      "file:///example/path/to/studentProjectDir/"};

//Create classloader.
ClassLoader cl = 
            new URLClassLoader(classpath);

Class<?> mainClazz;
Method mainMethod;

//Find Class.
mainClazz = cl.loadClass("com.example.MyClass"); 
//Find the target method.
mainMethod = mainClazz.getMethod("main", String[].class); 
//Invoke method.
mainMethod.invoke(null, (Object) args); 

In the this example com.example.MyClass should not be on the regular application classpath. If it is then that is the version that will get used instead of the one found by the custom classloader as standard classloaders use a parent-first delegation for loading classes.

OTHER TIPS

Frankly, I'd start a fresh JVM to assess each student's code. There are too many opportunities for one student's code to interfere with another's if you try to do it all in one JVM instance. Something as simple as an infinite loop ...

As a side-effect, this largely removes the need for custom classloaders, etcetera.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top