Categories
Java

Customize external Java library functions, simple Byte Buddy

Customize external Java library functions simple Byte Buddy. Lib Custom is a wrapper around Byte Buddy that allows to customize external Java libraries very easily: https://github.com/jleblanc64/lib-custom

1) Add dependency to your pom.xml

<dependency>
    <groupId>io.github.jleblanc64</groupId>
    <artifactId>lib-custom</artifactId>
    <version>1.0.0</version>
</dependency>

2) Modify the external library class function

Let’s say you have an external library with following class and function:

static class A {
    static int f() {
        return 0;
    }
}

We want to override the function f and make it return 1 instead of 0. Simply use Lib Custom as follows:

LibCustom.override(A.class, "f", x -> 1);
LibCustom.load();

That’s it ! A.f() now returns 1

Leave a Reply

Your email address will not be published. Required fields are marked *