-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
ByteBuddyTest.java
126 lines (109 loc) · 4.45 KB
/
ByteBuddyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package net.bytebuddy;
import net.bytebuddy.dynamic.TypeResolutionStrategy;
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Test;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
public class ByteBuddyTest {
@Test(expected = IllegalArgumentException.class)
public void testEnumWithoutValuesIsIllegal() throws Exception {
new ByteBuddy().makeEnumeration();
}
@Test
public void testEnumeration() throws Exception {
Class<?> type = new ByteBuddy()
.makeEnumeration("foo")
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(Modifier.isPublic(type.getModifiers()), is(true));
assertThat(type.isEnum(), is(true));
assertThat(type.isInterface(), is(false));
assertThat(type.isAnnotation(), is(false));
}
@Test
public void testInterface() throws Exception {
Class<?> type = new ByteBuddy()
.makeInterface()
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(Modifier.isPublic(type.getModifiers()), is(true));
assertThat(type.isEnum(), is(false));
assertThat(type.isInterface(), is(true));
assertThat(type.isAnnotation(), is(false));
}
@Test
public void testAnnotation() throws Exception {
Class<?> type = new ByteBuddy()
.makeAnnotation()
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(Modifier.isPublic(type.getModifiers()), is(true));
assertThat(type.isEnum(), is(false));
assertThat(type.isInterface(), is(true));
assertThat(type.isAnnotation(), is(true));
}
@Test
public void testTypeInitializerInstrumentation() throws Exception {
Recorder recorder = new Recorder();
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.invokable(isTypeInitializer())
.intercept(MethodDelegation.to(recorder))
.make(new TypeResolutionStrategy.Active())
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(type.getDeclaredConstructor().newInstance(), instanceOf(type));
assertThat(recorder.counter, is(1));
}
@Test
public void testImplicitStrategyBootstrap() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER)
.getLoaded();
assertThat(type.getClassLoader(), notNullValue(ClassLoader.class));
}
@Test
public void testImplicitStrategyNonBootstrap() throws Exception {
ClassLoader classLoader = new URLClassLoader(new URL[0], ClassLoadingStrategy.BOOTSTRAP_LOADER);
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.make()
.load(classLoader)
.getLoaded();
assertThat(type.getClassLoader(), is(classLoader));
}
@Test
public void testImplicitStrategyInjectable() throws Exception {
ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, Collections.<String, byte[]>emptyMap());
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.make()
.load(classLoader)
.getLoaded();
assertThat(type.getClassLoader(), is(classLoader));
}
@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(ByteBuddy.class).apply();
ObjectPropertyAssertion.of(ByteBuddy.EnumerationImplementation.class).apply();
}
public static class Recorder {
public int counter;
public void instrument() {
counter++;
}
}
}