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