1 /*
2 * Copyright (c) 2020, 2024, 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 testng/othervm IsRecordTest
30 */
31
32 import java.lang.classfile.ClassFile;
33 import java.lang.constant.ClassDesc;
34 import java.lang.reflect.AccessFlag;
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 org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import static java.lang.System.out;
44 import static java.lang.classfile.ClassFile.ACC_ABSTRACT;
45 import static java.lang.classfile.ClassFile.ACC_FINAL;
46 import static java.lang.constant.ConstantDescs.CD_int;
47 import static org.testng.Assert.assertEquals;
48 import static org.testng.Assert.assertFalse;
49 import static org.testng.Assert.assertTrue;
50
51 public class IsRecordTest {
52
53 @DataProvider(name = "scenarios")
54 public 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 @Test(dataProvider = "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(cls.isRecord(), expectIsRecord);
96 var getRecordComponents = cls.getRecordComponents();
97 assertTrue(expectIsRecord ? getRecordComponents != null : 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 @Test(dataProvider = "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 assertEquals(supFooCls.getRecordComponents(), null);
131 assertEquals(subFooCls.getRecordComponents(), null);
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(EmptyRecord.class.getRecordComponents().length, 0);
141
142 record FooRecord (int x) { }
143 assertTrue(FooRecord.class.isRecord());
144 assertTrue(FooRecord.class.getRecordComponents() != null);
145
146 final record FinalFooRecord (int x) { }
147 assertTrue(FinalFooRecord.class.isRecord());
148 assertTrue(FinalFooRecord.class.getRecordComponents() != null);
149
150 class A { }
151 assertFalse(A.class.isRecord());
152 assertFalse(A.class.getRecordComponents() != null);
153
154 final class B { }
155 assertFalse(B.class.isRecord());
156 assertFalse(B.class.getRecordComponents() != null);
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 }
--- EOF ---