Skip to content

Commit 921b212

Browse files
committed
Test static lambda hoisting works via LambdaDeserializer
LambdaMetafactory returns a ConstantCallSite bound to a shared instance of a lambda, rather than a reference to the no-arg constructor. This is a technique to avoid unnecessary allocations. This test checks that we preserve this property when deserializing.
1 parent a912944 commit 921b212

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/test/java/scala/compat/java8/runtime/LambdaDeserializerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ public void cached() {
6969
Assert.assertEquals(reconstituted1.getClass(), reconstituted2.getClass());
7070
}
7171

72+
@Test
73+
public void cachedStatic() {
74+
HashMap<String, MethodHandle> cache = new HashMap<>();
75+
F1<Boolean, String> f1 = lambdaHost.lambdaBackedByStaticImplMethod();
76+
// Check that deserialization of a static lambda always returns the
77+
// same instance.
78+
Assert.assertSame(reconstitute(f1, cache), reconstitute(f1, cache));
79+
80+
// (as is the case with regular invocation.)
81+
Assert.assertSame(f1, lambdaHost.lambdaBackedByStaticImplMethod());
82+
}
83+
7284
@Test
7385
public void implMethodNameChanged() {
7486
F1<Boolean, String> f1 = lambdaHost.lambdaBackedByStaticImplMethod();

0 commit comments

Comments
 (0)