Javassist: re-creating a class - delete first, or defrost() and modify?

后端 未结 2 2137
无人共我
无人共我 2021-02-10 03:28

I use Javassist to create a class. And in a test suite, when a second test tries to create the same class, it fails at pool.makeClass( ... ) because the class is fr

相关标签:
2条回答
  • 2021-02-10 04:17

    You cannot unload a single class from a ClassLoader. A class may be unloaded if it and its ClassLoader became unreachable but since every class refers to its loader that implies that all classes loaded by this loader must have become unreachable too.

    But you can (re-)create the class using a different ClassLoader. Well, formally it is a different class with the same name (and maybe the same byte code) then. If the code executed within the test case leaves no references in the heap, the ClassLoader and its classes might be collected after the test.

    0 讨论(0)
  • 2021-02-10 04:20

    I get the same problem, i solved it this way, may be cannot apply for your test case:

    Make CtClass a private static variable of your Class.

    Create a method that check's if CtClass is already built. If CtClass is not built, call the method that builds it, else, return that CtClass.

    Make that all your test use the new method.

    So, if you have N Test, just the first one will attempt to Built the CtClass, the rest of them will have the static CtClass Variable.

    0 讨论(0)
提交回复
热议问题