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 8281323
 27  * @summary Check emission of Preload attribute to make sure javac does not emit unneeded entries.
 28  * @modules jdk.jdeps/com.sun.tools.classfile
 29  * @enablePreview
 30  * @run main NoUnnecessaryPreloadsTest
 31  */
 32 
 33 import com.sun.tools.classfile.*;
 34 import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
 35 
 36 public class NoUnnecessaryPreloadsTest {
 37 
 38     public value class PreLoadTest1 {
 39         byte b;
 40         public PreLoadTest1(byte b) {
 41             this.b = b;
 42         }
 43     }
 44 
 45     public class PreLoadTest2 {
 46         static class Inner1 {
 47             static value class Inner2 {}
 48             Inner2 inner;
 49         }
 50     }
 51 
 52     public static void main(String[] args) throws Exception {
 53 
 54         // There should be no Preload attribute in NoUnnecessaryPreloadsTest.class
 55         ClassFile cls = ClassFile.read(NoUnnecessaryPreloadsTest.class.getResourceAsStream("NoUnnecessaryPreloadsTest.class"));
 56 
 57         if (cls == null) {
 58             throw new AssertionError("Could not locate the class files");
 59         }
 60 
 61         /* Check emission of Preload attribute */
 62         Preload_attribute preloads = (Preload_attribute) cls.attributes.get(Attribute.Preload);
 63         if (preloads != null) {
 64             throw new AssertionError("Unexpected Preload attribute!");
 65         }
 66 
 67         // There should be no Preload attribute in NoUnnecessaryPreloadsTest$PreloadTest1.class
 68         cls = ClassFile.read(NoUnnecessaryPreloadsTest.class.getResourceAsStream("NoUnnecessaryPreloadsTest$PreLoadTest1.class"));
 69 
 70         if (cls == null) {
 71             throw new AssertionError("Could not locate the class files");
 72         }
 73 
 74         /* Check emission of Preload attribute */
 75         preloads = (Preload_attribute) cls.attributes.get(Attribute.Preload);
 76         if (preloads != null) {
 77             throw new AssertionError("Unexpected Preload attribute!");
 78         }
 79 
 80         // There should be no Preload attribute in NoUnnecessaryPreloadsTest$PreloadTest2.class
 81         cls = ClassFile.read(NoUnnecessaryPreloadsTest.class.getResourceAsStream("NoUnnecessaryPreloadsTest$PreLoadTest2.class"));
 82 
 83         if (cls == null) {
 84             throw new AssertionError("Could not locate the class files");
 85         }
 86 
 87         /* Check emission of Preload attribute */
 88         preloads = (Preload_attribute) cls.attributes.get(Attribute.Preload);
 89         if (preloads != null) {
 90             throw new AssertionError("Unexpected Preload attribute!");
 91         }
 92 
 93         // There should be no Preload attribute in NoUnnecessaryPreloadsTest$PreloadTest2$Inner2.class
 94         cls = ClassFile.read(NoUnnecessaryPreloadsTest.class.getResourceAsStream("NoUnnecessaryPreloadsTest$PreLoadTest2$Inner1$Inner2.class"));
 95 
 96         if (cls == null) {
 97             throw new AssertionError("Could not locate the class files");
 98         }
 99 
100         /* Check emission of Preload attribute */
101         preloads = (Preload_attribute) cls.attributes.get(Attribute.Preload);
102         if (preloads != null) {
103             throw new AssertionError("Unexpected Preload attribute!");
104         }
105 
106         // There should be ONE Preload attribute entry in NoUnnecessaryPreloadsTest$PreloadTest2$Inner1.class
107         cls = ClassFile.read(NoUnnecessaryPreloadsTest.class.getResourceAsStream("NoUnnecessaryPreloadsTest$PreLoadTest2$Inner1.class"));
108 
109         if (cls == null) {
110             throw new AssertionError("Could not locate the class files");
111         }
112 
113         /* Check emission of Preload attribute */
114         preloads = (Preload_attribute) cls.attributes.get(Attribute.Preload);
115         if (preloads == null) {
116             throw new AssertionError("Missing Preload attribute!");
117         }
118 
119         if (preloads.number_of_classes != 1) {
120             throw new AssertionError("Incorrect number of Preload classes");
121         }
122 
123         CONSTANT_Class_info clsInfo = cls.constant_pool.getClassInfo(preloads.value_class_info_index[0]);
124         if (!clsInfo.getName().equals("NoUnnecessaryPreloadsTest$PreLoadTest2$Inner1$Inner2")) {
125             throw new AssertionError("Expected Preload class entry is missing, but found " + clsInfo.getName());
126         }
127     }
128 }