1 /* 2 * Copyright (c) 2022, 2025, 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 8280164 8334313 27 * @summary Check emission of LoadableDescriptors attribute 28 * @enablePreview 29 * @run main LoadableDescriptorsAttributeTest 30 */ 31 32 import java.lang.classfile.Attributes; 33 import java.lang.classfile.ClassFile; 34 import java.lang.classfile.ClassModel; 35 import java.lang.classfile.constantpool.Utf8Entry; 36 import java.util.Set; 37 import java.util.stream.Collectors; 38 39 public class LoadableDescriptorsAttributeTest { 40 41 value class V1 {} 42 value class V2 {} 43 value class V3 {} 44 value class V4 {} 45 value class V5 {} 46 value class V6 {} 47 value class V7 {} 48 value class V8 {} 49 value class V9 {} 50 abstract value class V10 {} 51 52 static final value class X { 53 final V1 [] v1 = null; // field descriptor, encoding array type - no LoadableDescriptors. 54 V2 foo() { // method descriptor encoding value type, to be preloaded 55 return null; 56 } // return type is value type so should be preloaded 57 void foo(V3 v3) { // method descriptor encoding value type, to be preloaded 58 } 59 void foo(int x) { 60 V4 [] v4 = null; // local variable encoding array type - no preload. 61 } 62 void goo(V6[] v6) { // parameter uses value type but as array component - no preload. 63 V5 v5 = null; // no preload as value type is used for local type. 64 if (v5 == null) { 65 // ... 66 } else { 67 V5 [] v52 = null; 68 } 69 } 70 final V7 v7 = null; // field descriptor uses value type - to be preloaded. 71 V8 [] goo(V9 [] v9) { // neither V8 nor V9 call for preload being array component types 72 return null; 73 } 74 V10 v10 = null; // abstract shouldn't be in the loadable descriptors attr 75 } 76 // So we expect ONLY V2, V3, V7 to be in LoadableDescriptors list 77 78 public static void main(String[] args) throws Exception { 79 ClassModel cls; 80 try (var in = LoadableDescriptorsAttributeTest.class.getResourceAsStream("LoadableDescriptorsAttributeTest$X.class")) { 81 cls = ClassFile.of().parse(in.readAllBytes()); 82 } 83 84 /* Check emission of LoadableDescriptors attribute */ 85 var descriptors = cls.findAttribute(Attributes.loadableDescriptors()).orElseThrow(); 86 if (descriptors.loadableDescriptors().size() != 3) { 87 throw new AssertionError("Expected 3 loadable descriptors, found: " + descriptors.loadableDescriptors()); 88 } 89 90 Set<String> expected = Set.of( 91 "LLoadableDescriptorsAttributeTest$V2;", 92 "LLoadableDescriptorsAttributeTest$V3;", 93 "LLoadableDescriptorsAttributeTest$V7;" 94 ); 95 96 Set<String> found = descriptors.loadableDescriptors() 97 .stream() 98 .map(Utf8Entry::stringValue) 99 .collect(Collectors.toSet()); 100 101 if (!expected.equals(found)) { 102 throw new AssertionError("LoadableDescriptors mismatch, found: " + found); 103 } 104 } 105 }