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