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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary reflection test for records
  29  * @modules jdk.compiler/com.sun.tools.javac.util
  30  */
  31 
  32 import java.lang.reflect.*;
  33 import java.util.List;
  34 import com.sun.tools.javac.util.Assert;
  35 
  36 public class RecordReflectionTest {
  37 
  38     class NoRecord {}
  39 
  40     record R1() {}
  41 
  42     record R2(int i, int j) {}
  43 
  44     record R3(List<String> ls) {}
  45 
  46     public static void main(String... args) throws Throwable {
  47         Class<?> noRecordClass = NoRecord.class;
  48         Assert.check(!noRecordClass.isRecord());
  49         Assert.check(noRecordClass.getRecordAccessors().length == 0);
  50 
  51         RecordReflectionTest recordReflectionTest = new RecordReflectionTest();
  52         recordReflectionTest.checkRecordReflection(new R1(), 0, null, null);
  53         recordReflectionTest.checkRecordReflection(new R2(1, 2), 2, new Object[]{1, 2}, new String[]{"int", "int"});
  54         recordReflectionTest.checkRecordReflection(new R3(List.of("1")), 1, new Object[]{List.of("1")}, new String[]{"java.util.List<java.lang.String>"});
  55     }
  56 
  57     void checkRecordReflection(Object recordOb, int numberOfComponents, Object[] values, String[] signatures) throws Throwable {
  58         Class<?> recordClass = recordOb.getClass();
  59         Assert.check(recordClass.isRecord());
  60         Method[] accessors = recordClass.getRecordAccessors();
  61         Assert.check(accessors.length == numberOfComponents);
  62         int i = 0;
  63         for (Method m : accessors) {
  64             Assert.check(m.invoke(recordOb).equals(values[i]));
  65             Assert.check(m.getGenericReturnType().toString().equals(signatures[i]), String.format("signature of method \"%s\" different from expected signature \"%s\"", m.getGenericReturnType(), signatures[i]));
  66             i++;
  67         }
  68     }
  69 }