1 /*
  2  * Copyright (c) 2019, 2024, 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 8326879 8320575
 27  * @summary reflection test for records
 28  * @build R10
 29  * @compile RecordReflectionTest.java
 30  * @run testng/othervm RecordReflectionTest
 31  * @run testng/othervm --enable-preview RecordReflectionTest
 32  * @run testng/othervm/java.security.policy=allPermissions.policy RecordReflectionTest
 33  */
 34 
 35 import java.lang.annotation.*;
 36 import java.lang.reflect.*;
 37 import java.util.List;
 38 import org.testng.annotations.*;
 39 import static org.testng.Assert.*;
 40 
 41 @Test
 42 public class RecordReflectionTest {
 43 
 44     class NoRecord {}
 45 
 46     record R1() {}
 47 
 48     record R2(int i, int j) {}
 49 
 50     record R3(List<String> ls) {}
 51 
 52     record R4(R1 r1, R2 r2, R3 r3) {}
 53 
 54     record R5(String... args) {}
 55 
 56     record R6(long l, String... args) implements java.io.Serializable {}
 57 
 58     record R7(String s1, String s2, String... args) {}
 59 
 60     record R8<A, B>(A a, B b) implements java.io.Serializable { }
 61 
 62     record R9(List<String> ls) {
 63         R9 {} // compact constructor, will contain a mandated parameter
 64     }
 65 
 66     /* record R10 is defined in an accompaning jcod file, defined as:
 67     record R10(List<String> ls) { // in this case there wasn't be any compact constructor and thus no mandated param
 68     }
 69     */
 70 
 71     record R11(int i, List<String> ls) {
 72         R11 {} // compact constructor, will contain mandated parameters
 73     }
 74 
 75     record R12(List<String> ls, int i) {
 76         R12 {} // compact constructor, will contain mandated parameters
 77     }
 78 
 79     record R13(List<String> ls1, int i, List<String> ls2) {
 80         R13 {} // compact constructor, will contain mandated parameters
 81     }
 82 
 83     @DataProvider(name = "recordClasses")
 84     public Object[][] recordClassData() {
 85         return List.of(R1.class,
 86                        R2.class,
 87                        R3.class,
 88                        R4.class,
 89                        R5.class,
 90                        R6.class,
 91                        R7.class,
 92                        R8.class,
 93                        R9.class,
 94                        R10.class,
 95                        R11.class,
 96                        R12.class,
 97                        R13.class
 98         ).stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
 99     }
100 
101     @Test(dataProvider = "recordClasses")
102     public void testIsRecord(Class<?> cls) {
103         String message = cls.toGenericString();
104         assertTrue(cls.isRecord());
105         assertTrue(cls.getSuperclass() == java.lang.Record.class);
106         assertTrue(cls.getRecordComponents() != null);
107         assertTrue(message.contains("record"), message);
108     }
109 
110     @DataProvider(name = "notRecordClasses")
111     public Object[][] notRecordClasses() {
112         return List.of(NoRecord.class,
113                        NoRecord[].class,
114                        Record.class,  // java.lang.Record is not itself a record class
115                        Record[].class,
116                        byte.class,
117                        byte[].class,
118                        int.class,
119                        int[].class,
120                        long.class,
121                        long[].class)
122                    .stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
123     }
124 
125     @Test(dataProvider = "notRecordClasses")
126     public void testNotARecordClass(Class<?> cls) {
127         assertFalse(cls.isRecord());
128         assertFalse(cls.getSuperclass() == java.lang.Record.class);
129         assertTrue(cls.getRecordComponents() == null);
130     }
131 
132     @DataProvider(name = "reflectionData")
133     public Object[][] reflectionData() {
134         return new Object[][] {
135             new Object[] { new R1(),
136                            0,
137                            null,
138                            null,
139                            null },
140             new Object[] { new R2(1, 2),
141                            2,
142                            new Object[]{ 1, 2 },
143                            new String[]{ "i", "j" },
144                            new String[]{ "int", "int"} },
145             new Object[] { new R3(List.of("1")),
146                            1,
147                            new Object[]{ List.of("1") },
148                            new String[]{ "ls" },
149                            new String[]{ "java.util.List<java.lang.String>"} },
150             new Object[] { new R4(new R1(), new R2(6, 7), new R3(List.of("s"))),
151                            3,
152                            new Object[]{ new R1(), new R2(6, 7), new R3(List.of("s")) },
153                            new String[]{ "r1", "r2", "r3" },
154                            new String[]{ R1.class.toString(), R2.class.toString(), R3.class.toString()} },
155             new Object[] { new R9(List.of("1")),
156                         1,
157                         new Object[]{ List.of("1") },
158                         new String[]{ "ls" },
159                         new String[]{ "java.util.List<java.lang.String>"} },
160             /* R10 has exactly the same definition as R9 but the parameter of the compact constructor doesn't have
161              * the mandated flag, nevertheless we should be able to load the same generic information
162              */
163             new Object[] { new R10(List.of("1")),
164                         1,
165                         new Object[]{ List.of("1") },
166                         new String[]{ "ls" },
167                         new String[]{ "java.util.List<java.lang.String>"} },
168             new Object[] { new R11(1, List.of("1")),
169                         2,
170                         new Object[]{ 1, List.of("1") },
171                         new String[]{ "i", "ls" },
172                         new String[]{ "int", "java.util.List<java.lang.String>"} },
173             new Object[] { new R12(List.of("1"), 1),
174                         2,
175                         new Object[]{ List.of("1"), 1 },
176                         new String[]{ "ls", "i" },
177                         new String[]{ "java.util.List<java.lang.String>", "int"} },
178             new Object[] { new R13(List.of("1"), 1, List.of("2")),
179                         3,
180                         new Object[]{ List.of("1"), 1, List.of("2") },
181                         new String[]{ "ls1", "i", "ls2" },
182                         new String[]{ "java.util.List<java.lang.String>", "int", "java.util.List<java.lang.String>"} },
183         };
184     }
185 
186     @Test(dataProvider = "reflectionData")
187     public void testRecordReflection(Object recordOb,
188                                      int numberOfComponents,
189                                      Object[] values,
190                                      String[] names,
191                                      String[] signatures)
192         throws ReflectiveOperationException
193     {
194         Class<?> recordClass = recordOb.getClass();
195         assertTrue(recordClass.isRecord());
196         RecordComponent[] recordComponents = recordClass.getRecordComponents();
197         assertEquals(recordComponents.length, numberOfComponents);
198         int i = 0;
199         for (RecordComponent rc : recordComponents) {
200             assertEquals(rc.getName(), names[i]);
201             assertEquals(rc.getType(), rc.getAccessor().getReturnType());
202             assertEquals(rc.getAccessor().invoke(recordOb), values[i]);
203             assertEquals(rc.getAccessor().getGenericReturnType().toString(), signatures[i],
204                          String.format("signature of method \"%s\" different from expected signature \"%s\"",
205                                  rc.getAccessor().getGenericReturnType(), signatures[i]));
206             i++;
207         }
208         // now let's check constructors
209         var constructor = recordClass.getDeclaredConstructors()[0];
210         i = 0;
211         for (var p: constructor.getParameters()) {
212             assertEquals(p.getParameterizedType().toString(), signatures[i],
213                     String.format("signature of method \"%s\" different from expected signature \"%s\"",
214                             p.getType().toString(), signatures[i]));
215             i++;
216         }
217         // similar as above but testing another API
218         i = 0;
219         for (var p : constructor.getGenericParameterTypes()) {
220             assertEquals(p.toString(), signatures[i],
221                     String.format("signature of method \"%s\" different from expected signature \"%s\"",
222                             p.toString(), signatures[i]));
223             i++;
224         }
225     }
226 
227     @Retention(RetentionPolicy.RUNTIME)
228     @Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD })
229     @interface RCA {}
230 
231     record AnnotatedRec(@RCA int i) {}
232 
233     public void testDeclAnnotationsInRecordComp() throws Throwable {
234         Class<?> recordClass = AnnotatedRec.class;
235         RecordComponent rc = recordClass.getRecordComponents()[0];
236         Annotation[] annos = rc.getAnnotations();
237         assertEquals(annos.length, 1);
238         assertEquals(annos[0].toString(), "@RecordReflectionTest.RCA()");
239 
240         Field f = recordClass.getDeclaredField("i");
241         assertEquals(f.getAnnotations().length, 1);
242         assertEquals(f.getAnnotations()[0].toString(), annos[0].toString());
243     }
244 
245     @Retention(RetentionPolicy.RUNTIME)
246     @Target({ElementType.TYPE_USE})
247     @interface TYPE_USE {}
248 
249     record TypeAnnotatedRec(@TYPE_USE int i) {}
250 
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(annos.length, 1);
257         assertEquals(annos[0].toString(), "@RecordReflectionTest.TYPE_USE()");
258 
259         Field f = recordClass.getDeclaredField("i");
260         assertEquals(f.getAnnotatedType().getAnnotations().length, 1);
261         assertEquals(f.getAnnotatedType().getAnnotations()[0].toString(), annos[0].toString());
262     }
263 
264     public void testReadOnlyFieldInRecord() throws Throwable {
265         R2 o = new R2(1, 2);
266         Class<?> recordClass = R2.class;
267         String fieldName = "i";
268         Field f = recordClass.getDeclaredField(fieldName);
269         assertTrue(f.trySetAccessible());
270         assertTrue(f.get(o) != null);
271         try {
272             f.set(o, null);
273             assertTrue(false, "should fail to set " + fieldName);
274         } catch (IllegalAccessException e) {
275         }
276     }
277 }