diff --git a/libraries/pom.xml b/libraries/pom.xml
index 6be4980b93..c23b6ef912 100644
--- a/libraries/pom.xml
+++ b/libraries/pom.xml
@@ -530,6 +530,16 @@
hll
${hll.version}
+
+ net.bytebuddy
+ byte-buddy
+ ${bytebuddy.version}
+
+
+ net.bytebuddy
+ byte-buddy-agent
+ ${bytebuddy.version}
+
0.7.0
@@ -572,6 +582,7 @@
mytheme
1.6.0
+ 1.7.1
diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java b/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java
new file mode 100644
index 0000000000..d0362a6c92
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java
@@ -0,0 +1,16 @@
+package com.baeldung.bytebuddy;
+
+import net.bytebuddy.implementation.bind.annotation.BindingPriority;
+
+public class Bar {
+
+ @BindingPriority(3)
+ public static String sayHelloBar() { return "Holla in Bar!"; }
+
+ @BindingPriority(2)
+ public static String sayBar() { return "bar"; }
+
+ public String bar() { return Bar.class.getSimpleName() + " - Bar"; }
+
+
+}
diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java b/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java
new file mode 100644
index 0000000000..4be06785b1
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java
@@ -0,0 +1,7 @@
+package com.baeldung.bytebuddy;
+
+public class Foo {
+
+ public String sayHelloFoo() { return "Hello in Foo!"; }
+
+}
diff --git a/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java b/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java
new file mode 100644
index 0000000000..6b7364a0a5
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java
@@ -0,0 +1,89 @@
+package com.baeldung.bytebuddy;
+
+import net.bytebuddy.ByteBuddy;
+import net.bytebuddy.agent.ByteBuddyAgent;
+import net.bytebuddy.dynamic.DynamicType;
+import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
+import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
+import net.bytebuddy.implementation.FixedValue;
+import net.bytebuddy.implementation.MethodDelegation;
+import net.bytebuddy.matcher.ElementMatchers;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import static net.bytebuddy.matcher.ElementMatchers.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class ByteBuddyUnitTest {
+
+ @Test
+ public void givenObject_whenToString_thenReturnHelloWorldString() throws InstantiationException, IllegalAccessException {
+ DynamicType.Unloaded unloadedType = new ByteBuddy()
+ .subclass(Object.class)
+ .method(ElementMatchers.isToString())
+ .intercept(FixedValue.value("Hello World ByteBuddy!"))
+ .make();
+
+ Class> dynamicType = unloadedType.load(getClass().getClassLoader())
+ .getLoaded();
+
+ assertEquals(dynamicType.newInstance().toString(), "Hello World ByteBuddy!");
+ }
+
+ @Test
+ public void givenFoo_whenRedefined_thenReturnFooRedefined() throws Exception {
+ ByteBuddyAgent.install();
+ new ByteBuddy()
+ .redefine(Foo.class)
+ .method(named("sayHelloFoo"))
+ .intercept(FixedValue.value("Hello Foo Redefined"))
+ .make()
+ .load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
+ Foo f = new Foo();
+ assertEquals(f.sayHelloFoo(), "Hello Foo Redefined");
+ }
+
+ @Test
+ public void givenSayHelloFoo_whenMethodDelegation_thenSayHelloBar() throws IllegalAccessException, InstantiationException {
+
+ String r = new ByteBuddy()
+ .subclass(Foo.class)
+ .method(
+ named("sayHelloFoo")
+ .and(isDeclaredBy(Foo.class)
+ .and(returns(String.class)))
+ )
+ .intercept(MethodDelegation.to(Bar.class))
+ .make()
+ .load(getClass().getClassLoader())
+ .getLoaded()
+ .newInstance()
+ .sayHelloFoo();
+
+ assertEquals(r, Bar.sayHelloBar());
+ }
+
+ @Test
+ public void givenMethodName_whenDefineMethod_thenCreateMethod() throws Exception {
+ Class> type = new ByteBuddy()
+ .subclass(Object.class)
+ .name("MyClassName")
+ .defineMethod("custom", String.class, Modifier.PUBLIC)
+ .intercept(MethodDelegation.to(Bar.class))
+ .defineField("x", String.class, Modifier.PUBLIC)
+ .make()
+ .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
+ .getLoaded();
+
+ Method m = type.getDeclaredMethod("custom", null);
+
+ assertEquals(m.invoke(type.newInstance()), Bar.sayHelloBar());
+ assertNotNull(type.getDeclaredField("x"));
+
+ }
+
+
+}