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