1 /*
  2  * Copyright (c) 2022, 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  * @modules jdk.jdeps/com.sun.tools.classfile
 29  * @enablePreview
 30  * @run main LoadableDescriptorsAttributeTest
 31  */
 32 
 33 import com.sun.tools.classfile.*;
 34 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
 35 
 36 public class LoadableDescriptorsAttributeTest {
 37 
 38     value class V1 {}
 39     value class V2 {}
 40     value class V3 {}
 41     value class V4 {}
 42     value class V5 {}
 43     value class V6 {}
 44     value class V7 {}
 45     value class V8 {}
 46     value class V9 {}
 47     abstract value class V10 {}
 48 
 49     static final value class X {
 50         final V1 [] v1 = null; // field descriptor, encoding array type - no LoadableDescriptors.
 51         V2 foo() {  // method descriptor encoding value type, to be preloaded
 52             return null;
 53         } // return type is value type so should be preloaded
 54         void foo(V3 v3) { // method descriptor encoding value type, to be preloaded
 55         }
 56         void foo(int x) {
 57             V4 [] v4 = null; // local variable encoding array type - no preload.
 58         }
 59         void goo(V6[] v6) { // parameter uses value type but as array component - no preload.
 60             V5 v5 = null;  // no preload as value type is used for local type.
 61             if (v5 == null) {
 62                 // ...
 63             } else {
 64                V5 [] v52 = null;
 65             }
 66         }
 67         final V7 v7 = null; // field descriptor uses value type - to be preloaded.
 68         V8 [] goo(V9 [] v9) { // neither V8 nor V9 call for preload being array component types
 69             return null;
 70         }
 71         V10 v10 = null; // abstract shouldn't be in the loadable descriptors attr
 72     }
 73     // So we expect ONLY V2, V3, V7 to be in LoadableDescriptors list
 74 
 75     public static void main(String[] args) throws Exception {
 76         ClassFile cls = ClassFile.read(LoadableDescriptorsAttributeTest.class.getResourceAsStream("LoadableDescriptorsAttributeTest$X.class"));
 77 
 78         if (cls == null) {
 79             throw new AssertionError("Could not locate the class files");
 80         }
 81 
 82         /* Check emission of LoadableDescriptors attribute */
 83         LoadableDescriptors_attribute descriptors = (LoadableDescriptors_attribute) cls.attributes.get(Attribute.LoadableDescriptors);
 84         if (descriptors == null) {
 85             throw new AssertionError("Missing LoadableDescriptors attribute!");
 86         }
 87         if (descriptors.number_of_descriptors != 3) {
 88             throw new AssertionError("Incorrect number of loadable descriptors, found: " + descriptors.number_of_descriptors);
 89         }
 90 
 91         int mask = 7;
 92         for (int i = 0; i < descriptors.number_of_descriptors; i++) {
 93             CONSTANT_Utf8_info clsInfo = cls.constant_pool.getUTF8Info(
 94                                   descriptors.descriptors[i]);
 95             switch (clsInfo.value) {
 96                 case "LLoadableDescriptorsAttributeTest$V2;":
 97                     mask &= ~1; break;
 98                 case "LLoadableDescriptorsAttributeTest$V3;":
 99                     mask &= ~2; break;
100                 case "LLoadableDescriptorsAttributeTest$V7;" :
101                     mask &= ~4; break;
102                 default:
103                     throw new AssertionError("Unexpected LoadableDescriptors entry!");
104             }
105         }
106         if (mask != 0) {
107           throw new AssertionError("Some LoadableDescriptors entries are missing!");
108         }
109     }
110 }