1 /*
 2  * Copyright (c) 2024, 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.lang.classfile.attribute;
26 
27 import java.lang.classfile.AttributeMapper;
28 import java.lang.classfile.Attributes;
29 import java.lang.classfile.ClassFile;
30 import java.lang.classfile.constantpool.Utf8Entry;
31 import java.lang.constant.ClassDesc;
32 import java.util.Arrays;
33 import java.util.List;
34 
35 import java.lang.classfile.Attribute;
36 import java.lang.classfile.ClassElement;
37 import java.lang.classfile.constantpool.ClassEntry;
38 
39 import jdk.internal.classfile.impl.AbstractPoolEntry;
40 import jdk.internal.classfile.impl.BoundAttribute;
41 import jdk.internal.classfile.impl.UnboundAttribute;
42 import jdk.internal.classfile.impl.Util;
43 import jdk.internal.javac.PreviewFeature;
44 
45 /**
46  * Models the {@link Attributes#loadableDescriptors() LoadableDescriptors}
47  * attribute (JVMS {@jvms 4.7.32}), which suggests the JVM may load mentioned
48  * types before the {@code class} file carrying this attribute is loaded.
49  * <p>
50  * This attribute only appears on classes, and does not permit {@linkplain
51  * AttributeMapper#allowMultiple multiple instances} in a class.  It has a
52  * data dependency on the {@linkplain AttributeMapper.AttributeStability#CP_REFS
53  * constant pool}.
54  * <p>
55  * The attribute was introduced in the Java SE Platform version XX, major
56  * version {@value ClassFile#JAVA_25_VERSION}. (FIXME)
57  *
58  * @see Attributes#loadableDescriptors()
59  * @jvms 4.7.32 The {@code LoadableDescriptors} Attribute
60  * @since Valhalla
61  */
62 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS)
63 public sealed interface LoadableDescriptorsAttribute
64         extends Attribute<LoadableDescriptorsAttribute>, ClassElement
65         permits BoundAttribute.BoundLoadableDescriptorsAttribute, UnboundAttribute.UnboundLoadableDescriptorsAttribute {
66 
67     /**
68      * {@return the list of loadable descriptors}
69      */
70     List<Utf8Entry> loadableDescriptors();
71 
72     /**
73      * {@return the list of loadable descriptors, as nominal descriptors}
74      */
75     default List<ClassDesc> loadableDescriptorSymbols() {
76         return Util.mappedList(loadableDescriptors(), Util::fieldTypeSymbol);
77     }
78 
79     /**
80      * {@return a {@code LoadableDescriptors} attribute}
81      * @param loadableDescriptors the loadable descriptors
82      */
83     static LoadableDescriptorsAttribute of(List<Utf8Entry> loadableDescriptors) {
84         return new UnboundAttribute.UnboundLoadableDescriptorsAttribute(loadableDescriptors);
85     }
86 
87     /**
88      * {@return a {@code LoadableDescriptors} attribute}
89      * @param loadableDescriptors the loadable descriptors
90      */
91     static LoadableDescriptorsAttribute of(Utf8Entry... loadableDescriptors) {
92         return of(List.of(loadableDescriptors));
93     }
94 }