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