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