Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 frozen (i.e. already created via toClass().

What's the way to overcome this? Ideally, the first test should delete the class somehow - perhaps unload from the classloader - but as I read in JLS, the unload operation is not reliable.

So perhaps the workaround is to check in the class creating code whether it exists, and if it does, defrost() it, remove all members etc, and re-create it.

Any other ideas?

Or is there some reliable way to delete the class through Javassist?

like image 215
Ondra Žižka Avatar asked Oct 04 '22 17:10

Ondra Žižka


1 Answers

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.

like image 77
Holger Avatar answered Oct 11 '22 06:10

Holger