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

早过忘川 提交于 2019-12-03 08:58:48

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!