|
26 | 26 | package jdk.graal.compiler.hotspot.test;
|
27 | 27 |
|
28 | 28 | import static org.junit.Assert.assertEquals;
|
| 29 | +import static org.junit.Assert.assertNotEquals; |
29 | 30 | import static org.junit.Assert.assertNotNull;
|
30 | 31 | import static org.junit.Assert.assertTrue;
|
31 | 32 | import static org.junit.Assert.fail;
|
32 | 33 |
|
33 | 34 | import java.math.BigInteger;
|
| 35 | +import java.util.function.Function; |
34 | 36 |
|
35 | 37 | import org.junit.Test;
|
36 | 38 | import org.objectweb.asm.Type;
|
|
40 | 42 | import jdk.vm.ci.runtime.JVMCI;
|
41 | 43 |
|
42 | 44 | public class LambdaStableNameTest {
|
| 45 | + |
43 | 46 | @Test
|
44 | 47 | public void checkStableLamdaNameForRunnableAndAutoCloseable() {
|
45 | 48 | String s = "a string";
|
46 |
| - Runnable r = s::hashCode; |
47 |
| - ResolvedJavaType rType = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(r.getClass()); |
| 49 | + Runnable r0 = s::hashCode; |
| 50 | + String r0Name = getLambdaName(r0.getClass()); |
48 | 51 |
|
49 |
| - String name = LambdaUtils.findStableLambdaName(rType); |
50 |
| - assertLambdaName(name); |
| 52 | + Runnable r1 = s::hashCode; |
| 53 | + String r1Name = getLambdaName(r1.getClass()); |
| 54 | + |
| 55 | + assertEquals("The two stable lambda names should the same as they reference the same method and implement the same interface", r0Name, r1Name); |
51 | 56 |
|
52 | 57 | AutoCloseable ac = s::hashCode;
|
53 |
| - ResolvedJavaType acType = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(ac.getClass()); |
54 |
| - String acName = LambdaUtils.findStableLambdaName(acType); |
55 |
| - assertEquals("Both stable lambda names are the same as they reference the same method", name, acName); |
| 58 | + String acName = getLambdaName(ac.getClass()); |
| 59 | + |
| 60 | + assertNotEquals("The two stable lambda names should not be the same as they reference the same method but implement different interfaces", r0Name, acName); |
56 | 61 |
|
57 | 62 | String myName = Type.getInternalName(getClass());
|
58 |
| - assertEquals("The name known in 24.0 version is computed", "L" + myName + "$$Lambda.0x605511206480068bfd9e0bafd4f79e22;", name); |
| 63 | + assertEquals("The name known in 24.0 version is computed", "L" + myName + "$$Lambda.0x59cf38d78b5471f8ea57f1c28b37039c;", r0Name); |
| 64 | + |
| 65 | + Function<String, Integer> f0 = (str) -> str.hashCode(); |
| 66 | + String f0Name = getLambdaName(f0.getClass()); |
| 67 | + |
| 68 | + interface ValueTransformer<L, R> extends Function<L, R> { |
| 69 | + } |
| 70 | + |
| 71 | + ValueTransformer<String, Integer> f1 = (str) -> str.hashCode(); |
| 72 | + String f1Name = getLambdaName(f1.getClass()); |
| 73 | + |
| 74 | + assertNotEquals("The two stable lambda names should not be the same as they reference the same method but implement different interfaces", f0Name, f1Name); |
| 75 | + } |
| 76 | + |
| 77 | + private static String getLambdaName(Class<?> clazz) { |
| 78 | + ResolvedJavaType type = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess().lookupJavaType(clazz); |
| 79 | + String name = LambdaUtils.findStableLambdaName(type); |
| 80 | + assertLambdaName(name); |
| 81 | + return name; |
59 | 82 | }
|
60 | 83 |
|
61 | 84 | private static void assertLambdaName(String name) {
|
|
0 commit comments