Complete Example Code for BAEL-782 (#2270)

* added updated example codes

* updated example code StringToCharStream

* deleted StringToCharStream.java locally

* removed redundant file

* added code for apache commons collection SetUtils

* refactored example code

* added example code for bytebuddy
This commit is contained in:
Seun Matt 2017-07-17 23:44:10 +01:00 committed by Zeger Hendrikse
parent 3671427d5e
commit 2ce547d4cd
4 changed files with 123 additions and 0 deletions

View File

@ -530,6 +530,16 @@
<artifactId>hll</artifactId>
<version>${hll.version}</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${bytebuddy.version}</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${bytebuddy.version}</version>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>
@ -572,6 +582,7 @@
<vaadin.theme>mytheme</vaadin.theme>
<!-- /Vaadin -->
<hll.version>1.6.0</hll.version>
<bytebuddy.version>1.7.1</bytebuddy.version>
</properties>
<profiles>
<!-- Vaadin -->

View File

@ -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"; }
}

View File

@ -0,0 +1,7 @@
package com.baeldung.bytebuddy;
public class Foo {
public String sayHelloFoo() { return "Hello in Foo!"; }
}

View File

@ -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"));
}
}