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