1 import jdk.incubator.code.Op;
2
3 import java.lang.reflect.Method;
4 import jdk.incubator.code.CodeReflection;
5 import org.junit.jupiter.api.Assertions;
6 import org.junit.jupiter.api.Test;
7
8 import java.util.Arrays;
9 import java.util.Optional;
10 import java.util.stream.IntStream;
11
12 /*
13 * @test
14 * @summary test that invoking Method::getCodeModel returns the same instance.
15 * @modules jdk.incubator.code
16 * @run junit CodeModelSameInstanceTest
17 */
18 public class CodeModelSameInstanceTest {
19
20 @CodeReflection
21 static int add(int a, int b) {
22 return a + b;
23 }
24
25 @Test
26 public void test() {
27 Optional<Method> om = Arrays.stream(this.getClass().getDeclaredMethods()).filter(m -> m.getName().equals("add"))
28 .findFirst();
29 Method m = om.get();
30 Object[] codeModels = IntStream.range(0, 1024).mapToObj(_ -> Op.ofMethod(m)).toArray();
31 for (int i = 1; i < codeModels.length; i++) {
32 Assertions.assertSame(codeModels[i-1], codeModels[i]);
33 }
34 }
35 }