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