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