question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrate from cglib to ByteBuddy

See original GitHub issue

In 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
raphwcommented, Feb 23, 2018

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:

new ByteBuddy()
  .subclass(delegate.getClass())
  .method(any()).intercept(InvocationHandlerAdapter.of((proxy, method, args) -> {
    // put your code here
  }).make().load(dockerClientClassLoader);

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.

0reactions
cdancycommented, Apr 8, 2018

Very awesome and easy to understand and read. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to migrate from cglib to Byte Buddy? - java - Stack Overflow
I'm trying to migrate my code from cglib to Byte Buddy. This is what I have using cglib: Enhancer enhancer = new Enhancer();...
Read more >
JDK 11 and proxies in a world past sun.misc.Unsafe
Migrating cglib proxies to Byte Buddy ... A cglib proxy is typically defined using the Enhancer class in combination with an ...
Read more >
A Guide to Byte Buddy - Baeldung
In this to-the-point article, we're going to use the framework to manipulate existing classes, create new classes on demand, and even intercept ...
Read more >
Andrei Solntsev · Blog
Migration from CGLIB to ByteBuddy ; Mocking final classes and methods. Yes! I must use it! - I decided. What a huge disappointment...
Read more >
Using Byte Buddy for proxy creation - My daily Java
And while there are alternatives to cglib that support Java 17, migration is not always straight-forward. To ease such migration, this article ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found