1 /*
2 * Copyright (c) 2020, 2026, 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 8255560
27 * @summary Class::isRecord should check that the current class is final and not abstract
28 * @library /test/lib
29 * @run junit/othervm IsRecordTest
30 */
31
32 import java.lang.classfile.ClassFile;
33 import java.lang.constant.ClassDesc;
34 import java.util.List;
35 import java.util.Map;
36
37 import java.lang.classfile.attribute.RecordAttribute;
38 import java.lang.classfile.attribute.RecordComponentInfo;
39 import jdk.test.lib.ByteCodeLoader;
40 import static java.lang.System.out;
41 import static java.lang.classfile.ClassFile.ACC_ABSTRACT;
42 import static java.lang.classfile.ClassFile.ACC_FINAL;
43 import static java.lang.constant.ConstantDescs.CD_int;
44
45 import org.junit.jupiter.api.Test;
46 import org.junit.jupiter.params.ParameterizedTest;
47 import org.junit.jupiter.params.provider.MethodSource;
48
49 import static org.junit.jupiter.api.Assertions.*;
50
51 public class IsRecordTest {
52
53 public static Object[][] scenarios() {
54 return new Object[][] {
55 // isFinal, isAbstract, extendJLR, withRecAttr, expectIsRecord
56 { false, false, false, true, false },
57 { false, false, true, true, false },
58 { false, true, false, true, false },
59 { false, true, true, true, false },
60 { true, false, false, true, false },
61 { true, false, true, true, true },
62
63 { false, false, false, false, false },
64 { false, false, true, false, false },
65 { false, true, false, false, false },
66 { false, true, true, false, false },
67 { true, false, false, false, false },
68 { true, false, true, false, false },
69 };
70 }
71
72 /**
73 * Tests the valid combinations of i) final/non-final, ii) abstract/non-abstract,
74 * iii) direct subclass of j.l.Record (or not), along with the presence or
75 * absence of a record attribute.
76 */
77 @ParameterizedTest
78 @MethodSource("scenarios")
79 public void testDirectSubClass(boolean isFinal,
80 boolean isAbstract,
81 boolean extendsJLR,
82 boolean withRecordAttr,
83 boolean expectIsRecord) throws Exception {
84 out.println("\n--- testDirectSubClass isFinal=%s, isAbstract=%s, extendsJLR=%s, withRecordAttr=%s, expectIsRecord=%s ---"
85 .formatted(isFinal, isAbstract, extendsJLR, withRecordAttr, expectIsRecord));
86
87 List<RecordComponentInfo> rc = null;
88 if (withRecordAttr)
89 rc = List.of(RecordComponentInfo.of("x", CD_int));
90 String superName = extendsJLR ? "java/lang/Record" : "java/lang/Object";
91 var classBytes = generateClassBytes("C", isFinal, isAbstract, superName, rc);
92 Class<?> cls = ByteCodeLoader.load("C", classBytes);
93 out.println("cls=%s, Record::isAssignable=%s, isRecord=%s"
94 .formatted(cls, Record.class.isAssignableFrom(cls), cls.isRecord()));
95 assertEquals(expectIsRecord, cls.isRecord());
96 assertEquals(expectIsRecord, cls.getRecordComponents() != null);
97 }
98
99 /**
100 * Tests the valid combinations of i) final/non-final, ii) abstract/non-abstract,
101 * along with the presence or absence of a record attribute, where the class has
102 * a superclass whose superclass is j.l.Record.
103 */
104 @ParameterizedTest
105 @MethodSource("scenarios")
106 public void testIndirectSubClass(boolean isFinal,
107 boolean isAbstract,
108 boolean unused1,
109 boolean withRecordAttr,
110 boolean unused2) throws Exception {
111 out.println("\n--- testIndirectSubClass isFinal=%s, isAbstract=%s withRecordAttr=%s ---"
112 .formatted(isFinal, isAbstract, withRecordAttr));
113
114 List<RecordComponentInfo> rc = null;
115 if (withRecordAttr)
116 rc = List.of(RecordComponentInfo.of("x", CD_int));
117 var supFooClassBytes = generateClassBytes("SupFoo", false, isAbstract, "java/lang/Record", rc);
118 var subFooClassBytes = generateClassBytes("SubFoo", isFinal, isAbstract, "SupFoo", rc);
119 var allClassBytes = Map.of("SupFoo", supFooClassBytes,
120 "SubFoo", subFooClassBytes);
121
122 ClassLoader loader = new ByteCodeLoader(allClassBytes, null);
123 Class<?> supFooCls = loader.loadClass("SupFoo");
124 Class<?> subFooCls = loader.loadClass("SubFoo");
125 for (var cls : List.of(supFooCls, subFooCls))
126 out.println("cls=%s, Record::isAssignable=%s, isRecord=%s"
127 .formatted(cls, Record.class.isAssignableFrom(cls), cls.isRecord()));
128 assertFalse(supFooCls.isRecord());
129 assertFalse(subFooCls.isRecord());
130 assertNull(supFooCls.getRecordComponents());
131 assertNull(subFooCls.getRecordComponents());
132 }
133
134 /** Tests record-ness properties of traditionally compiled classes. */
135 @Test
136 public void testBasicRecords() {
137 out.println("\n--- testBasicRecords ---");
138 record EmptyRecord () { }
139 assertTrue(EmptyRecord.class.isRecord());
140 assertEquals(0, EmptyRecord.class.getRecordComponents().length);
141
142 record FooRecord (int x) { }
143 assertTrue(FooRecord.class.isRecord());
144 assertNotNull(FooRecord.class.getRecordComponents());
145
146 final record FinalFooRecord (int x) { }
147 assertTrue(FinalFooRecord.class.isRecord());
148 assertNotNull(FinalFooRecord.class.getRecordComponents());
149
150 class A { }
151 assertFalse(A.class.isRecord());
152 assertNull(A.class.getRecordComponents());
153
154 final class B { }
155 assertFalse(B.class.isRecord());
156 assertNull(B.class.getRecordComponents());
157 }
158
159 // -- infra
160
161 // Generates a class with the given properties.
162 byte[] generateClassBytes(String className,
163 boolean isFinal,
164 boolean isAbstract,
165 String superName,
166 List<RecordComponentInfo> components) {
167 return ClassFile.of().build(ClassDesc.ofInternalName(className), clb -> {
168 int access = 0;
169 if (isFinal)
170 access = access | ACC_FINAL;
171 if (isAbstract)
172 access = access | ACC_ABSTRACT;
173 clb.withFlags(access);
174 clb.withSuperclass(ClassDesc.ofInternalName(superName));
175 if (components != null)
176 clb.accept(RecordAttribute.of(components));
177 });
178 }
179
180 }