File: RecordReflectionTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (276 lines) | stat: -rw-r--r-- 11,223 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8235369 8235550 8247444 8320575
 * @summary reflection test for records
 * @build R10
 * @compile RecordReflectionTest.java
 * @run testng/othervm RecordReflectionTest
 * @run testng/othervm/java.security.policy=allPermissions.policy RecordReflectionTest
 */

import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.List;
import org.testng.annotations.*;
import static org.testng.Assert.*;

@Test
public class RecordReflectionTest {

    class NoRecord {}

    record R1() {}

    record R2(int i, int j) {}

    record R3(List<String> ls) {}

    record R4(R1 r1, R2 r2, R3 r3) {}

    record R5(String... args) {}

    record R6(long l, String... args) implements java.io.Serializable {}

    record R7(String s1, String s2, String... args) {}

    record R8<A, B>(A a, B b) implements java.io.Serializable { }

    record R9(List<String> ls) {
        R9 {} // compact constructor, will contain a mandated parameter
    }

    /* record R10 is defined in an accompaning jcod file, defined as:
    record R10(List<String> ls) { // in this case there wasn't be any compact constructor and thus no mandated param
    }
    */

    record R11(int i, List<String> ls) {
        R11 {} // compact constructor, will contain mandated parameters
    }

    record R12(List<String> ls, int i) {
        R12 {} // compact constructor, will contain mandated parameters
    }

    record R13(List<String> ls1, int i, List<String> ls2) {
        R13 {} // compact constructor, will contain mandated parameters
    }

    @DataProvider(name = "recordClasses")
    public Object[][] recordClassData() {
        return List.of(R1.class,
                       R2.class,
                       R3.class,
                       R4.class,
                       R5.class,
                       R6.class,
                       R7.class,
                       R8.class,
                       R9.class,
                       R10.class,
                       R11.class,
                       R12.class,
                       R13.class
        ).stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
    }

    @Test(dataProvider = "recordClasses")
    public void testIsRecord(Class<?> cls) {
        String message = cls.toGenericString();
        assertTrue(cls.isRecord());
        assertTrue(cls.getSuperclass() == java.lang.Record.class);
        assertTrue(cls.getRecordComponents() != null);
        assertTrue(message.contains("record"), message);
    }

    @DataProvider(name = "notRecordClasses")
    public Object[][] notRecordClasses() {
        return List.of(NoRecord.class,
                       NoRecord[].class,
                       Record.class,  // java.lang.Record is not itself a record class
                       Record[].class,
                       byte.class,
                       byte[].class,
                       int.class,
                       int[].class,
                       long.class,
                       long[].class)
                   .stream().map(c -> new Object[] {c}).toArray(Object[][]::new);
    }

    @Test(dataProvider = "notRecordClasses")
    public void testNotARecordClass(Class<?> cls) {
        assertFalse(cls.isRecord());
        assertFalse(cls.getSuperclass() == java.lang.Record.class);
        assertTrue(cls.getRecordComponents() == null);
    }

    @DataProvider(name = "reflectionData")
    public Object[][] reflectionData() {
        return new Object[][] {
            new Object[] { new R1(),
                           0,
                           null,
                           null,
                           null },
            new Object[] { new R2(1, 2),
                           2,
                           new Object[]{ 1, 2 },
                           new String[]{ "i", "j" },
                           new String[]{ "int", "int"} },
            new Object[] { new R3(List.of("1")),
                           1,
                           new Object[]{ List.of("1") },
                           new String[]{ "ls" },
                           new String[]{ "java.util.List<java.lang.String>"} },
            new Object[] { new R4(new R1(), new R2(6, 7), new R3(List.of("s"))),
                           3,
                           new Object[]{ new R1(), new R2(6, 7), new R3(List.of("s")) },
                           new String[]{ "r1", "r2", "r3" },
                           new String[]{ R1.class.toString(), R2.class.toString(), R3.class.toString()} },
            new Object[] { new R9(List.of("1")),
                        1,
                        new Object[]{ List.of("1") },
                        new String[]{ "ls" },
                        new String[]{ "java.util.List<java.lang.String>"} },
            /* R10 has exactly the same definition as R9 but the parameter of the compact constructor doesn't have
             * the mandated flag, nevertheless we should be able to load the same generic information
             */
            new Object[] { new R10(List.of("1")),
                        1,
                        new Object[]{ List.of("1") },
                        new String[]{ "ls" },
                        new String[]{ "java.util.List<java.lang.String>"} },
            new Object[] { new R11(1, List.of("1")),
                        2,
                        new Object[]{ 1, List.of("1") },
                        new String[]{ "i", "ls" },
                        new String[]{ "int", "java.util.List<java.lang.String>"} },
            new Object[] { new R12(List.of("1"), 1),
                        2,
                        new Object[]{ List.of("1"), 1 },
                        new String[]{ "ls", "i" },
                        new String[]{ "java.util.List<java.lang.String>", "int"} },
            new Object[] { new R13(List.of("1"), 1, List.of("2")),
                        3,
                        new Object[]{ List.of("1"), 1, List.of("2") },
                        new String[]{ "ls1", "i", "ls2" },
                        new String[]{ "java.util.List<java.lang.String>", "int", "java.util.List<java.lang.String>"} },
        };
    }

    @Test(dataProvider = "reflectionData")
    public void testRecordReflection(Object recordOb,
                                     int numberOfComponents,
                                     Object[] values,
                                     String[] names,
                                     String[] signatures)
        throws ReflectiveOperationException
    {
        Class<?> recordClass = recordOb.getClass();
        assertTrue(recordClass.isRecord());
        RecordComponent[] recordComponents = recordClass.getRecordComponents();
        assertEquals(recordComponents.length, numberOfComponents);
        int i = 0;
        for (RecordComponent rc : recordComponents) {
            assertEquals(rc.getName(), names[i]);
            assertEquals(rc.getType(), rc.getAccessor().getReturnType());
            assertEquals(rc.getAccessor().invoke(recordOb), values[i]);
            assertEquals(rc.getAccessor().getGenericReturnType().toString(), signatures[i],
                         String.format("signature of method \"%s\" different from expected signature \"%s\"",
                                 rc.getAccessor().getGenericReturnType(), signatures[i]));
            i++;
        }
        // now let's check constructors
        var constructor = recordClass.getDeclaredConstructors()[0];
        i = 0;
        for (var p: constructor.getParameters()) {
            assertEquals(p.getParameterizedType().toString(), signatures[i],
                    String.format("signature of method \"%s\" different from expected signature \"%s\"",
                            p.getType().toString(), signatures[i]));
            i++;
        }
        // similar as above but testing another API
        i = 0;
        for (var p : constructor.getGenericParameterTypes()) {
            assertEquals(p.toString(), signatures[i],
                    String.format("signature of method \"%s\" different from expected signature \"%s\"",
                            p.toString(), signatures[i]));
            i++;
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD })
    @interface RCA {}

    record AnnotatedRec(@RCA int i) {}

    public void testDeclAnnotationsInRecordComp() throws Throwable {
        Class<?> recordClass = AnnotatedRec.class;
        RecordComponent rc = recordClass.getRecordComponents()[0];
        Annotation[] annos = rc.getAnnotations();
        assertEquals(annos.length, 1);
        assertEquals(annos[0].toString(), "@RecordReflectionTest.RCA()");

        Field f = recordClass.getDeclaredField("i");
        assertEquals(f.getAnnotations().length, 1);
        assertEquals(f.getAnnotations()[0].toString(), annos[0].toString());
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE_USE})
    @interface TYPE_USE {}

    record TypeAnnotatedRec(@TYPE_USE int i) {}

    public void testTypeAnnotationsInRecordComp() throws Throwable {
        Class<?> recordClass = TypeAnnotatedRec.class;
        RecordComponent rc = recordClass.getRecordComponents()[0];
        AnnotatedType at = rc.getAnnotatedType();
        Annotation[] annos = at.getAnnotations();
        assertEquals(annos.length, 1);
        assertEquals(annos[0].toString(), "@RecordReflectionTest.TYPE_USE()");

        Field f = recordClass.getDeclaredField("i");
        assertEquals(f.getAnnotatedType().getAnnotations().length, 1);
        assertEquals(f.getAnnotatedType().getAnnotations()[0].toString(), annos[0].toString());
    }

    public void testReadOnlyFieldInRecord() throws Throwable {
        R2 o = new R2(1, 2);
        Class<?> recordClass = R2.class;
        String fieldName = "i";
        Field f = recordClass.getDeclaredField(fieldName);
        assertTrue(f.trySetAccessible());
        assertTrue(f.get(o) != null);
        try {
            f.set(o, null);
            assertTrue(false, "should fail to set " + fieldName);
        } catch (IllegalAccessException e) {
        }
    }
}