1 /* 2 * Copyright (c) 2019, 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 27 * @summary reflection test for records 28 * @compile --enable-preview -source ${jdk.version} RecordReflectionTest.java 29 * @run testng/othervm --enable-preview RecordReflectionTest 30 */ 31 32 import java.lang.annotation.*; 33 import java.lang.reflect.*; 34 import java.util.List; 35 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 public void testIsRecord() { 53 assertFalse(NoRecord.class.isRecord()); 54 55 for (Class<?> c : List.of(R1.class, R2.class, R3.class)) { 56 String message = c.toGenericString(); 57 assertTrue(c.isRecord(), message); 58 assertTrue(message.contains("record") , message); 59 } 60 } 61 62 public void testGetComponentsNoRecord() { 63 assertTrue(NoRecord.class.getRecordComponents().length == 0); 64 } 65 66 @DataProvider(name = "reflectionData") 67 public Object[][] reflectionData() { 68 return new Object[][] { 69 new Object[] { new R1(), 70 0, 71 null, 72 null, 73 null }, 74 new Object[] { new R2(1, 2), 75 2, 76 new Object[]{ 1, 2 }, 77 new String[]{ "i", "j" }, 78 new String[]{ "int", "int"} }, 79 new Object[] { new R3(List.of("1")), 80 1, 81 new Object[]{ List.of("1") }, 82 new String[]{ "ls" }, 83 new String[]{ "java.util.List<java.lang.String>"} }, 84 new Object[] { new R4(new R1(), new R2(6, 7), new R3(List.of("s"))), 85 3, 86 new Object[]{ new R1(), new R2(6, 7), new R3(List.of("s")) } , 87 new String[]{ "r1", "r2", "r3" }, 88 new String[]{ R1.class.toString(), R2.class.toString(), R3.class.toString()} }, 89 }; 90 } 91 92 @Test(dataProvider = "reflectionData") 93 public void testRecordReflection(Object recordOb, 94 int numberOfComponents, 95 Object[] values, 96 String[] names, 97 String[] signatures) 98 throws ReflectiveOperationException 99 { 100 Class<?> recordClass = recordOb.getClass(); 101 assertTrue(recordClass.isRecord()); 102 RecordComponent[] recordComponents = recordClass.getRecordComponents(); 103 assertEquals(recordComponents.length, numberOfComponents); 104 int i = 0; 105 for (RecordComponent rc : recordComponents) { 106 assertEquals(rc.getName(), names[i]); 107 assertEquals(rc.getType(), rc.getAccessor().getReturnType()); 108 assertEquals(rc.getAccessor().invoke(recordOb), values[i]); 109 assertEquals(rc.getAccessor().getGenericReturnType().toString(), signatures[i], 110 String.format("signature of method \"%s\" different from expected signature \"%s\"", 111 rc.getAccessor().getGenericReturnType(), signatures[i])); 112 i++; 113 } 114 } 115 116 record R5(String... args) {} 117 record R6(long l, String... args) {} 118 record R7(String s1, String s2, String... args) {} 119 120 @Retention(RetentionPolicy.RUNTIME) 121 @Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD }) 122 @interface RCA {} 123 124 record AnnotatedRec(@RCA int i) {} 125 126 public void testDeclAnnotationsInRecordComp() throws Throwable { 127 Class<?> recordClass = AnnotatedRec.class; 128 RecordComponent rc = recordClass.getRecordComponents()[0]; 129 Annotation[] annos = rc.getAnnotations(); 130 assertEquals(annos.length, 1); 131 assertEquals(annos[0].toString(), "@RecordReflectionTest$RCA()"); 132 133 Field f = recordClass.getDeclaredField("i"); 134 assertEquals(f.getAnnotations().length, 1); 135 assertEquals(f.getAnnotations()[0].toString(), annos[0].toString()); 136 } 137 138 @Retention(RetentionPolicy.RUNTIME) 139 @Target({ElementType.TYPE_USE}) 140 @interface TYPE_USE {} 141 142 record TypeAnnotatedRec(@TYPE_USE int i) {} 143 144 public void testTypeAnnotationsInRecordComp() throws Throwable { 145 Class<?> recordClass = TypeAnnotatedRec.class; 146 RecordComponent rc = recordClass.getRecordComponents()[0]; 147 AnnotatedType at = rc.getAnnotatedType(); 148 Annotation[] annos = at.getAnnotations(); 149 assertEquals(annos.length, 1); 150 assertEquals(annos[0].toString(), "@RecordReflectionTest$TYPE_USE()"); 151 152 Field f = recordClass.getDeclaredField("i"); 153 assertEquals(f.getAnnotatedType().getAnnotations().length, 1); 154 assertEquals(f.getAnnotatedType().getAnnotations()[0].toString(), annos[0].toString()); 155 } 156 }