Skip to content

Commit b3b87aa

Browse files
committed
Add test that checks for bootstrap from a different type.
1 parent 70fd86f commit b3b87aa

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

byte-buddy-dep/src/precompiled/java/net/bytebuddy/test/precompiled/DynamicConstantBootstrap.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,11 @@ public static DynamicConstantBootstrap make(int intValue,
161161
}
162162
return new DynamicConstantBootstrap();
163163
}
164+
165+
public static class Other {
166+
167+
public static DynamicConstantBootstrap bootstrap(MethodHandles.Lookup lookup, Object... args) {
168+
return new DynamicConstantBootstrap();
169+
}
170+
}
164171
}

byte-buddy-dep/src/test/java/net/bytebuddy/utility/JavaConstantDynamicTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ public void testDynamicConstantFactoryLookupOnly() throws Exception {
4141
assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo)));
4242
}
4343

44+
@Test
45+
@JavaVersionRule.Enforce(11)
46+
public void testDynamicConstantFactoryLookupOnlyOtherHost() throws Exception {
47+
Class<?> bootstrap = Class.forName("net.bytebuddy.test.precompiled.DynamicConstantBootstrap$Other");
48+
Class<? extends Foo> baz = new ByteBuddy()
49+
.subclass(Foo.class)
50+
.method(isDeclaredBy(Foo.class))
51+
.intercept(FixedValue.value(JavaConstant.Dynamic.bootstrap(FOO, bootstrap.getMethod("bootstrap",
52+
Class.forName("java.lang.invoke.MethodHandles$Lookup"),
53+
Object[].class))))
54+
.make()
55+
.load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
56+
.getLoaded();
57+
assertThat(baz.getDeclaredFields().length, is(0));
58+
assertThat(baz.getDeclaredMethods().length, is(1));
59+
Foo foo = baz.getDeclaredConstructor().newInstance();
60+
assertThat(baz.getDeclaredMethod(FOO).invoke(foo), instanceOf(bootstrap.getDeclaringClass()));
61+
assertThat(baz.getDeclaredMethod(FOO).invoke(foo), sameInstance(baz.getDeclaredMethod(FOO).invoke(foo)));
62+
}
63+
4464
@Test
4565
@JavaVersionRule.Enforce(11)
4666
public void testDynamicConstantFactoryLookupAndStringOnly() throws Exception {
Binary file not shown.

0 commit comments

Comments
 (0)