Migrate from cglib to ByteBuddy
See original GitHub issueIn diagnosing an issue for the gradle-docker-plugin library, of which I’m the current maintainer, I’m banging my head trying to get cglib to load a class into the specified custom classloader. It creates the class/object well enough but the class itself can’t be found within the actual classloader and the code ends up throwing a ClassNotFoundException. After spending way too much time with this a colleague recommended using byte-buddy instead. Looks like a very cool library but after searching the internets I couldn’t find a “migrating from cglib to byte-buddy” doc anywhere. My apologies as I haven’t read through the docs in great length yet (due to the time sink of the above) but was just wondering if you had a doc like that so that I can convert the below groovy snippet into something for byte-buddy.
final def delegate = dockerClientObjectFactory.create(dockerClientClassLoader, "${COMMAND_PACKAGE}.LogContainerResultCallback");
final def enhancer = dockerClientObjectFactory.create(dockerClientClassLoader, 'net.sf.cglib.proxy.Enhancer');
enhancer.setClassLoader(dockerClientClassLoader)
enhancer.setSuperclass(delegate.getClass())
enhancer.setCallback([
invoke: { Object proxy, java.lang.reflect.Method method, Object[] args ->
if ("onNext" == method.name && args.length && args[0]) {
// some crazy stuff we do here
}
}
try {
method.invoke(delegate, args)
} catch (InvocationTargetException e) {
throw e.cause
}
}
].asType(loadClass('net.sf.cglib.proxy.InvocationHandler')))
def callback = enhancer.create()
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
Hey, the problem you encounter is that cglib relies on its own types to work, meaning that cglib must be visible to the class loader you are injecting a class into.
There is no such guide, you are correct and I should find the time to write one some day. Let me keep this issue open for a reminder. The most used capability of cglib is probably the enhancer that you are using which should be easy enough to document.
For a rough version, there is no direct equivalent but there are several options. For you, the easiest way might even be the
InvocationHandlerAdapter:The resulting class does not have any type dependencies outside of the JDK and the delegate class such that you should not have any issues.
Very awesome and easy to understand and read. Thanks!