1 /*
  2  * Copyright (c) 2019, 2022, 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
 27  * @summary reflection test for records
 28  * @compile RecordReflectionTest.java
 29  * @run testng/othervm RecordReflectionTest
 30  * @run testng/othervm/java.security.policy=allPermissions.policy RecordReflectionTest
 31  */
 32 
 33 import java.lang.annotation.*;
 34 import java.lang.reflect.*;
 35 import java.util.List;
 36 import org.testng.annotations.*;
 37 import static org.testng.Assert.*;
 38 
 39 @Test
 40 public class RecordReflectionTest {
 41 
 42     class NoRecord {}
 43 
 44     record R1() {}
 45 
 46     record R2(int i, int j) {}
 47 
 48     record R3(List<String> ls) {}
 49 
 50     record R4(R1 r1, R2 r2, R3 r3) {}
 51 
 52     record R5(String... args) {}
 53 
 54     record R6(long l, String... args) implements java.io.Serializable {}
 55 
 56     record R7(String s1, String s2, String... args) {}
 57 
 58     record R8<A, B>(A a, B b) implements java.io.Serializable { }
 59 
 60     @DataProvider(name = "recordClasses")
 61     public Object[][] recordClassData() {
 62         return List.of(R1.class,
 63                        R2.class,
 64                        R3.class,
 65                        R4.class,
 66                        R5.class,
 67                        R6.class,
 68                        R7.class,
 69                        R8.class)
 70                    .stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
 71     }
 72 
 73     @Test(dataProvider = "recordClasses")
 74     public void testIsRecord(Class<?> cls) {
 75         String message = cls.toGenericString();
 76         assertTrue(cls.isRecord());
 77         assertTrue(cls.getSuperclass() == java.lang.Record.class);
 78         assertTrue(cls.getRecordComponents() != null);
 79         assertTrue(message.contains("record"), message);
 80     }
 81 
 82     @DataProvider(name = "notRecordClasses")
 83     public Object[][] notRecordClasses() {
 84         return List.of(NoRecord.class,
 85                        NoRecord[].class,
 86                        Record.class,  // java.lang.Record is not itself a record class
 87                        Record[].class,
 88                        byte.class,
 89                        byte[].class,
 90                        int.class,
 91                        int[].class,
 92                        long.class,
 93                        long[].class)
 94                    .stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
 95     }
 96 
 97     @Test(dataProvider = "notRecordClasses")
 98     public void testNotARecordClass(Class<?> cls) {
 99         assertFalse(cls.isRecord());
100         assertFalse(cls.getSuperclass() == java.lang.Record.class);
101         assertTrue(cls.getRecordComponents() == null);
102     }
103 
104     @DataProvider(name = "reflectionData")
105     public Object[][] reflectionData() {
106         return new Object[][] {
107             new Object[] { new R1(),
108                            0,
109                            null,
110                            null,
111                            null },
112             new Object[] { new R2(1, 2),
113                            2,
114                            new Object[]{ 1, 2 },
115                            new String[]{ "i", "j" },
116                            new String[]{ "int", "int"} },
117             new Object[] { new R3(List.of("1")),
118                            1,
119                            new Object[]{ List.of("1") },
120                            new String[]{ "ls" },
121                            new String[]{ "java.util.List<java.lang.String>"} },
122             new Object[] { new R4(new R1(), new R2(6, 7), new R3(List.of("s"))),
123                            3,
124                            new Object[]{ new R1(), new R2(6, 7), new R3(List.of("s")) },
125                            new String[]{ "r1", "r2", "r3" },
126                            new String[]{ R1.class.toString(), R2.class.toString(), R3.class.toString()} },
127         };
128     }
129 
130     @Test(dataProvider = "reflectionData")
131     public void testRecordReflection(Object recordOb,
132                                      int numberOfComponents,
133                                      Object[] values,
134                                      String[] names,
135                                      String[] signatures)
136         throws ReflectiveOperationException
137     {
138         Class<?> recordClass = recordOb.getClass();
139         assertTrue(recordClass.isRecord());
140         RecordComponent[] recordComponents = recordClass.getRecordComponents();
141         assertEquals(recordComponents.length, numberOfComponents);
142         int i = 0;
143         for (RecordComponent rc : recordComponents) {
144             assertEquals(rc.getName(), names[i]);
145             assertEquals(rc.getType(), rc.getAccessor().getReturnType());
146             assertEquals(rc.getAccessor().invoke(recordOb), values[i]);
147             assertEquals(rc.getAccessor().getGenericReturnType().toString(), signatures[i],
148                          String.format("signature of method \"%s\" different from expected signature \"%s\"",
149                                  rc.getAccessor().getGenericReturnType(), signatures[i]));
150             i++;
151         }
152     }
153 
154     @Retention(RetentionPolicy.RUNTIME)
155     @Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD })
156     @interface RCA {}
157 
158     record AnnotatedRec(@RCA int i) {}
159 
160     public void testDeclAnnotationsInRecordComp() throws Throwable {
161         Class<?> recordClass = AnnotatedRec.class;
162         RecordComponent rc = recordClass.getRecordComponents()[0];
163         Annotation[] annos = rc.getAnnotations();
164         assertEquals(annos.length, 1);
165         assertEquals(annos[0].toString(), "@RecordReflectionTest.RCA()");
166 
167         Field f = recordClass.getDeclaredField("i");
168         assertEquals(f.getAnnotations().length, 1);
169         assertEquals(f.getAnnotations()[0].toString(), annos[0].toString());
170     }
171 
172     @Retention(RetentionPolicy.RUNTIME)
173     @Target({ElementType.TYPE_USE})
174     @interface TYPE_USE {}
175 
176     record TypeAnnotatedRec(@TYPE_USE int i) {}
177 
178     public void testTypeAnnotationsInRecordComp() throws Throwable {
179         Class<?> recordClass = TypeAnnotatedRec.class;
180         RecordComponent rc = recordClass.getRecordComponents()[0];
181         AnnotatedType at = rc.getAnnotatedType();
182         Annotation[] annos = at.getAnnotations();
183         assertEquals(annos.length, 1);
184         assertEquals(annos[0].toString(), "@RecordReflectionTest.TYPE_USE()");
185 
186         Field f = recordClass.getDeclaredField("i");
187         assertEquals(f.getAnnotatedType().getAnnotations().length, 1);
188         assertEquals(f.getAnnotatedType().getAnnotations()[0].toString(), annos[0].toString());
189     }
190 
191     public void testReadOnlyFieldInRecord() throws Throwable {
192         R2 o = new R2(1, 2);
193         Class<?> recordClass = R2.class;
194         String fieldName = "i";
195         Field f = recordClass.getDeclaredField(fieldName);
196         assertTrue(f.trySetAccessible());
197         assertTrue(f.get(o) != null);
198         try {
199             f.set(o, null);
200             assertTrue(false, "should fail to set " + fieldName);
201         } catch (IllegalAccessException e) {
202         }
203     }
204 
205 }