3

非コアJavaクラスを新しくロードされた状態に戻す方法はありますか?クラスをアンロードして最初からリロードするのと同じことをしたいと思います。私は主に静的初期化子と変数に関心があります。

問題のコンテキスト:学生コード用のロボグレーダーを書いています。私が見た一般的な学生のエラーの1つは、静的変数を不適切に使用することです。たとえば、含まれる要素の静的カウントを持つコレクションについて考えてみます。コレクションは、最初に作成して使用したときに正常に機能しますが、次のインスタンス化では失敗します。テストを可能な限りモジュール化する場合は、テスト後にクリーンな状態を復元する方法が必要です。

今、私は多かれ少なかれこのようにクラスをロードしています、そして私はそれをどのように使いたいかをスケッチしました。

    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);

もちろん、ロボグレーダーは完璧ではありませんが、よくあるエラーをキャッチしたいと思います。これもその1つです。

Java言語仕様のセクション12.7によると、クラスのアンロードのサポートはオプションです(ただし、私が望むことを正確に実行します)。非標準機能に依存せずにこれを行う方法はありますか?

最後の手段は、別々のプログラムで一連のテストを実行するAntビルドを実行することですが、可能であれば、これを1つのプロセスで機能させたいと思います。

4

2 に答える 2

3

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.

于 2012-03-01T03:29:26.440 に答える
2

率直に言って、私は各学生のコードを評価するために新しいJVMを開始します。1つのJVMインスタンスですべてを実行しようとすると、ある学生のコードが別の学生のコードに干渉する機会が多すぎます。無限ループのような単純なもの...

副作用として、これによりカスタムクラスローダーなどの必要性が大幅になくなります。

于 2012-03-01T04:24:19.770 に答える