/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
libraries-bytecode/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java
63 строки
3 KB
panos-kakos
[JAVA-22519] Created new module libraries-stream and added 5 articles (#15403)
13 дек 2023, 10:40
13 дек 2023, 10:40
8daf19c
Код
Авторство
О чём код?
package com.baeldung.bytebuddy; import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.returns; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import org.junit.Test; 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; 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")); } }