Search code examples
javajavassistcglib

Javassist factory - setting a custom classloader


With CGLIB I can do

final var enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setSuperclass(superClazz);
enhancer.setCallback(...);
enhancer.setClassLoader(getClass().getClassLoader());   // This

However with Javassist I can't find a way to set the ClassLoader. Any idea?

EDIT: omg, should I just subclass ProxyFactory? That's weird.


Solution

  • // Kotlin
    ProxyFactory.classLoaderProvider = ProxyFactory.ClassLoaderProvider { javaClass.classLoader }
    

    This doesn't seem safe imho. But that's how it's done apparently, per documentation.
    Alternatively, just subclass ProxyFactory.