1 /*
  2  * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8235369 8235550 8247444 8320575
 27  * @summary reflection test for records
 28  * @build R10
 29  * @compile RecordReflectionTest.java
 30  * @run junit/othervm RecordReflectionTest
 31  */
 32 
 33 import java.lang.annotation.*;
 34 import java.lang.reflect.*;
 35 import java.util.List;
 36 import static org.junit.jupiter.api.Assertions.*;
 37 import org.junit.jupiter.api.Test;
 38 import org.junit.jupiter.params.ParameterizedTest;
 39 import org.junit.jupiter.params.provider.MethodSource;
 40 
 41 public class RecordReflectionTest {
 42 
 43     class NoRecord {}
 44 
 45     record R1() {}
 46 
 47     record R2(int i, int j) {}
 48 
 49     record R3(List<String> ls) {}
 50 
 51     record R4(R1 r1, R2 r2, R3 r3) {}
 52 
 53     record R5(String... args) {}
 54 
 55     record R6(long l, String... args) implements java.io.Serializable {}
 56 
 57     record R7(String s1, String s2, String... args) {}
 58 
 59     record R8<A, B>(A a, B b) implements java.io.Serializable { }
 60 
 61     record R9(List<String> ls) {
 62         R9 {} // compact constructor, will contain a mandated parameter
 63     }
 64 
 65     /* record R10 is defined in an accompaning jcod file, defined as:
 66     record R10(List<String> ls) { // in this case there wasn't be any compact constructor and thus no mandated param
 67     }
 68     */
 69 
 70     record R11(int i, List<String> ls) {
 71         R11 {} // compact constructor, will contain mandated parameters
 72     }
 73 
 74     record R12(List<String> ls, int i) {
 75         R12 {} // compact constructor, will contain mandated parameters
 76     }
 77 
 78     record R13(List<String> ls1, int i, List<String> ls2) {
 79         R13 {} // compact constructor, will contain mandated parameters
 80     }
 81 
 82     public static Object[][] recordClassData() {
 83         return List.of(R1.class,
 84                        R2.class,
 85                        R3.class,
 86                        R4.class,
 87                        R5.class,
 88                        R6.class,
 89                        R7.class,
 90                        R8.class,
 91                        R9.class,
 92                        R10.class,
 93                        R11.class,
 94                        R12.class,
 95                        R13.class
 96         ).stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
 97     }
 98 
 99     @ParameterizedTest
100     @MethodSource("recordClassData")
101     public void testIsRecord(Class<?> cls) {
102         String message = cls.toGenericString();
103         assertTrue(cls.isRecord());
104         assertSame(Record.class, cls.getSuperclass());
105         assertNotNull(cls.getRecordComponents());
106         assertTrue(message.contains("record"), message);
107     }
108 
109     public static List<Class<?>> notRecordClasses() {
110         return List.of(NoRecord.class,
111                        NoRecord[].class,
112                        Record.class,  // java.lang.Record is not itself a record class
113                        Record[].class,
114                        byte.class,
115                        byte[].class,
116                        int.class,
117                        int[].class,
118                        long.class,
119                        long[].class);
120     }
121 
122     @ParameterizedTest
123     @MethodSource("notRecordClasses")
124     public void testNotARecordClass(Class<?> cls) {
125         assertFalse(cls.isRecord());
126         assertNotSame(Record.class, cls.getSuperclass());
127         assertNull(cls.getRecordComponents());
128     }
129 
130     public static Object[][] reflectionData() {
131         return new Object[][] {
132             new Object[] { new R1(),
133                            0,
134                            null,
135                            null,
136                            null },
137             new Object[] { new R2(1, 2),
138                            2,
139                            new Object[]{ 1, 2 },
140                            new String[]{ "i", "j" },
141                            new String[]{ "int", "int"} },
142             new Object[] { new R3(List.of("1")),
143                            1,
144                            new Object[]{ List.of("1") },
145                            new String[]{ "ls" },
146                            new String[]{ "java.util.List<java.lang.String>"} },
147             new Object[] { new R4(new R1(), new R2(6, 7), new R3(List.of("s"))),
148                            3,
149                            new Object[]{ new R1(), new R2(6, 7), new R3(List.of("s")) },
150                            new String[]{ "r1", "r2", "r3" },
151                            new String[]{ R1.class.toString(), R2.class.toString(), R3.class.toString()} },
152             new Object[] { new R9(List.of("1")),
153                         1,
154                         new Object[]{ List.of("1") },
155                         new String[]{ "ls" },
156                         new String[]{ "java.util.List<java.lang.String>"} },
157             /* R10 has exactly the same definition as R9 but the parameter of the compact constructor doesn't have
158              * the mandated flag, nevertheless we should be able to load the same generic information
159              */
160             new Object[] { new R10(List.of("1")),
161                         1,
162                         new Object[]{ List.of("1") },
163                         new String[]{ "ls" },
164                         new String[]{ "java.util.List<java.lang.String>"} },
165             new Object[] { new R11(1, List.of("1")),
166                         2,
167                         new Object[]{ 1, List.of("1") },
168                         new String[]{ "i", "ls" },
169                         new String[]{ "int", "java.util.List<java.lang.String>"} },
170             new Object[] { new R12(List.of("1"), 1),
171                         2,
172                         new Object[]{ List.of("1"), 1 },
173                         new String[]{ "ls", "i" },
174                         new String[]{ "java.util.List<java.lang.String>", "int"} },
175             new Object[] { new R13(List.of("1"), 1, List.of("2")),
176                         3,
177                         new Object[]{ List.of("1"), 1, List.of("2") },
178                         new String[]{ "ls1", "i", "ls2" },
179                         new String[]{ "java.util.List<java.lang.String>", "int", "java.util.List<java.lang.String>"} },
180         };
181     }
182 
183     @ParameterizedTest
184     @MethodSource("reflectionData")
185     public void testRecordReflection(Object recordOb,
186                                      int numberOfComponents,
187                                      Object[] values,
188                                      String[] names,
189                                      String[] signatures)
190         throws ReflectiveOperationException
191     {
192         Class<?> recordClass = recordOb.getClass();
193         assertTrue(recordClass.isRecord());
194         RecordComponent[] recordComponents = recordClass.getRecordComponents();
195         assertEquals(numberOfComponents, recordComponents.length);
196         int i = 0;
197         for (RecordComponent rc : recordComponents) {
198             assertEquals(names[i], rc.getName());
199             assertEquals(rc.getAccessor().getReturnType(), rc.getType());
200             assertEquals(values[i], rc.getAccessor().invoke(recordOb));
201             assertEquals(signatures[i], rc.getAccessor().getGenericReturnType().toString(),
202                          String.format("signature of method \"%s\" different from expected signature \"%s\"",
203                                  rc.getAccessor().getGenericReturnType(), signatures[i]));
204             i++;
205         }
206         // now let's check constructors
207         var constructor = recordClass.getDeclaredConstructors()[0];
208         i = 0;
209         for (var p: constructor.getParameters()) {
210             assertEquals(signatures[i], p.getParameterizedType().toString(),
211                     String.format("signature of method \"%s\" different from expected signature \"%s\"",
212                             p.getType().toString(), signatures[i]));
213             i++;
214         }
215         // similar as above but testing another API
216         i = 0;
217         for (var p : constructor.getGenericParameterTypes()) {
218             assertEquals(signatures[i], p.toString(),
219                     String.format("signature of method \"%s\" different from expected signature \"%s\"",
220                             p.toString(), signatures[i]));
221             i++;
222         }
223     }
224 
225     @Retention(RetentionPolicy.RUNTIME)
226     @Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD })
227     @interface RCA {}
228 
229     record AnnotatedRec(@RCA int i) {}
230 
231     @Test
232     public void testDeclAnnotationsInRecordComp() throws Throwable {
233         Class<?> recordClass = AnnotatedRec.class;
234         RecordComponent rc = recordClass.getRecordComponents()[0];
235         Annotation[] annos = rc.getAnnotations();
236         assertEquals(1, annos.length);
237         assertEquals("@RecordReflectionTest.RCA()", annos[0].toString());
238 
239         Field f = recordClass.getDeclaredField("i");
240         assertEquals(1, f.getAnnotations().length);
241         assertEquals(annos[0].toString(), f.getAnnotations()[0].toString());
242     }
243 
244     @Retention(RetentionPolicy.RUNTIME)
245     @Target({ElementType.TYPE_USE})
246     @interface TYPE_USE {}
247 
248     record TypeAnnotatedRec(@TYPE_USE int i) {}
249 
250     @Test
251     public void testTypeAnnotationsInRecordComp() throws Throwable {
252         Class<?> recordClass = TypeAnnotatedRec.class;
253         RecordComponent rc = recordClass.getRecordComponents()[0];
254         AnnotatedType at = rc.getAnnotatedType();
255         Annotation[] annos = at.getAnnotations();
256         assertEquals(1, annos.length);
257         assertEquals("@RecordReflectionTest.TYPE_USE()", annos[0].toString());
258 
259         Field f = recordClass.getDeclaredField("i");
260         assertEquals(1, f.getAnnotatedType().getAnnotations().length);
261         assertEquals(annos[0].toString(), f.getAnnotatedType().getAnnotations()[0].toString());
262     }
263 
264     @Test
265     public void testReadOnlyFieldInRecord() throws Throwable {
266         R2 o = new R2(1, 2);
267         Class<?> recordClass = R2.class;
268         String fieldName = "i";
269         Field f = recordClass.getDeclaredField(fieldName);
270         assertTrue(f.trySetAccessible());
271         assertNotNull(f.get(o));
272         assertThrows(IllegalAccessException.class, () -> f.set(o, null));
273     }
274 }