|
| 1 | +package com.baeldung.bytebuddy; |
| 2 | + |
| 3 | +import net.bytebuddy.ByteBuddy; |
| 4 | +import net.bytebuddy.agent.ByteBuddyAgent; |
| 5 | +import net.bytebuddy.dynamic.DynamicType; |
| 6 | +import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; |
| 7 | +import net.bytebuddy.dynamic.loading.ClassReloadingStrategy; |
| 8 | +import net.bytebuddy.implementation.FixedValue; |
| 9 | +import net.bytebuddy.implementation.MethodDelegation; |
| 10 | +import net.bytebuddy.matcher.ElementMatchers; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.lang.reflect.Modifier; |
| 15 | + |
| 16 | +import static net.bytebuddy.matcher.ElementMatchers.*; |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertNotNull; |
| 19 | + |
| 20 | +public class ByteBuddyUnitTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenObject_whenToString_thenReturnHelloWorldString() throws InstantiationException, IllegalAccessException { |
| 24 | + DynamicType.Unloaded unloadedType = new ByteBuddy() |
| 25 | + .subclass(Object.class) |
| 26 | + .method(ElementMatchers.isToString()) |
| 27 | + .intercept(FixedValue.value("Hello World ByteBuddy!")) |
| 28 | + .make(); |
| 29 | + |
| 30 | + Class<?> dynamicType = unloadedType.load(getClass().getClassLoader()) |
| 31 | + .getLoaded(); |
| 32 | + |
| 33 | + assertEquals(dynamicType.newInstance().toString(), "Hello World ByteBuddy!"); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenFoo_whenRedefined_thenReturnFooRedefined() throws Exception { |
| 38 | + ByteBuddyAgent.install(); |
| 39 | + new ByteBuddy() |
| 40 | + .redefine(Foo.class) |
| 41 | + .method(named("sayHelloFoo")) |
| 42 | + .intercept(FixedValue.value("Hello Foo Redefined")) |
| 43 | + .make() |
| 44 | + .load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); |
| 45 | + Foo f = new Foo(); |
| 46 | + assertEquals(f.sayHelloFoo(), "Hello Foo Redefined"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenSayHelloFoo_whenMethodDelegation_thenSayHelloBar() throws IllegalAccessException, InstantiationException { |
| 51 | + |
| 52 | + String r = new ByteBuddy() |
| 53 | + .subclass(Foo.class) |
| 54 | + .method( |
| 55 | + named("sayHelloFoo") |
| 56 | + .and(isDeclaredBy(Foo.class) |
| 57 | + .and(returns(String.class))) |
| 58 | + ) |
| 59 | + .intercept(MethodDelegation.to(Bar.class)) |
| 60 | + .make() |
| 61 | + .load(getClass().getClassLoader()) |
| 62 | + .getLoaded() |
| 63 | + .newInstance() |
| 64 | + .sayHelloFoo(); |
| 65 | + |
| 66 | + assertEquals(r, Bar.sayHelloBar()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenMethodName_whenDefineMethod_thenCreateMethod() throws Exception { |
| 71 | + Class<?> type = new ByteBuddy() |
| 72 | + .subclass(Object.class) |
| 73 | + .name("MyClassName") |
| 74 | + .defineMethod("custom", String.class, Modifier.PUBLIC) |
| 75 | + .intercept(MethodDelegation.to(Bar.class)) |
| 76 | + .defineField("x", String.class, Modifier.PUBLIC) |
| 77 | + .make() |
| 78 | + .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) |
| 79 | + .getLoaded(); |
| 80 | + |
| 81 | + Method m = type.getDeclaredMethod("custom", null); |
| 82 | + |
| 83 | + assertEquals(m.invoke(type.newInstance()), Bar.sayHelloBar()); |
| 84 | + assertNotNull(type.getDeclaredField("x")); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
0 commit comments