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 8326879 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 * @run testng/othervm --enable-preview IsRecordTest 31 */ 32 33 import java.lang.classfile.ClassFile; 34 import java.lang.constant.ClassDesc; 35 import java.lang.reflect.AccessFlag; 36 import java.util.List; 37 import java.util.Map; 38 39 import java.lang.classfile.attribute.RecordAttribute; 40 import java.lang.classfile.attribute.RecordComponentInfo; 41 import jdk.test.lib.ByteCodeLoader; 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 import static java.lang.System.out; 45 import static java.lang.classfile.ClassFile.ACC_ABSTRACT; 46 import static java.lang.classfile.ClassFile.ACC_FINAL; 47 import static java.lang.constant.ConstantDescs.CD_int; 48 import static org.testng.Assert.assertEquals; 49 import static org.testng.Assert.assertFalse; 50 import static org.testng.Assert.assertTrue; 51 52 public class IsRecordTest { 53 54 @DataProvider(name = "scenarios") 55 public Object[][] scenarios() { 56 return new Object[][] { 57 // isFinal, isAbstract, extendJLR, withRecAttr, expectIsRecord 58 { false, false, false, true, false }, 59 { false, false, true, true, false }, 60 { false, true, false, true, false }, 61 { false, true, true, true, false }, 62 { true, false, false, true, false }, 63 { true, false, true, true, true }, 64 65 { false, false, false, false, false }, 66 { false, false, true, false, false }, 67 { false, true, false, false, false }, 68 { false, true, true, false, false }, 69 { true, false, false, false, false }, 70 { true, false, true, false, false }, 71 }; 72 } 73 74 /** 75 * Tests the valid combinations of i) final/non-final, ii) abstract/non-abstract, 76 * iii) direct subclass of j.l.Record (or not), along with the presence or 77 * absence of a record attribute. 78 */ 79 @Test(dataProvider = "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(cls.isRecord(), expectIsRecord); 97 var getRecordComponents = cls.getRecordComponents(); 98 assertTrue(expectIsRecord ? getRecordComponents != null : getRecordComponents == null); 99 } 100 101 /** 102 * Tests the valid combinations of i) final/non-final, ii) abstract/non-abstract, 103 * along with the presence or absence of a record attribute, where the class has 104 * a superclass whose superclass is j.l.Record. 105 */ 106 @Test(dataProvider = "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 assertEquals(supFooCls.getRecordComponents(), null); 132 assertEquals(subFooCls.getRecordComponents(), null); 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(EmptyRecord.class.getRecordComponents().length, 0); 142 143 record FooRecord (int x) { } 144 assertTrue(FooRecord.class.isRecord()); 145 assertTrue(FooRecord.class.getRecordComponents() != null); 146 147 final record FinalFooRecord (int x) { } 148 assertTrue(FinalFooRecord.class.isRecord()); 149 assertTrue(FinalFooRecord.class.getRecordComponents() != null); 150 151 class A { } 152 assertFalse(A.class.isRecord()); 153 assertFalse(A.class.getRecordComponents() != null); 154 155 final class B { } 156 assertFalse(B.class.isRecord()); 157 assertFalse(B.class.getRecordComponents() != null); 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 }