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