Skip to content

Commit 2ce547d

Browse files
SeunMattzhendrikse
authored andcommitted
Complete Example Code for BAEL-782 (eugenp#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
1 parent 3671427 commit 2ce547d

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,16 @@
530530
<artifactId>hll</artifactId>
531531
<version>${hll.version}</version>
532532
</dependency>
533+
<dependency>
534+
<groupId>net.bytebuddy</groupId>
535+
<artifactId>byte-buddy</artifactId>
536+
<version>${bytebuddy.version}</version>
537+
</dependency>
538+
<dependency>
539+
<groupId>net.bytebuddy</groupId>
540+
<artifactId>byte-buddy-agent</artifactId>
541+
<version>${bytebuddy.version}</version>
542+
</dependency>
533543
</dependencies>
534544
<properties>
535545
<multiverse.version>0.7.0</multiverse.version>
@@ -572,6 +582,7 @@
572582
<vaadin.theme>mytheme</vaadin.theme>
573583
<!-- /Vaadin -->
574584
<hll.version>1.6.0</hll.version>
585+
<bytebuddy.version>1.7.1</bytebuddy.version>
575586
</properties>
576587
<profiles>
577588
<!-- Vaadin -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.bytebuddy;
2+
3+
import net.bytebuddy.implementation.bind.annotation.BindingPriority;
4+
5+
public class Bar {
6+
7+
@BindingPriority(3)
8+
public static String sayHelloBar() { return "Holla in Bar!"; }
9+
10+
@BindingPriority(2)
11+
public static String sayBar() { return "bar"; }
12+
13+
public String bar() { return Bar.class.getSimpleName() + " - Bar"; }
14+
15+
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.bytebuddy;
2+
3+
public class Foo {
4+
5+
public String sayHelloFoo() { return "Hello in Foo!"; }
6+
7+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)