1 /*
2  * Copyright (c) 2018, 2019, 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.constant;
26 
27 import java.lang.Enum.EnumDesc;
28 import java.lang.invoke.MethodHandle;
29 import java.lang.invoke.MethodHandles;
30 import java.lang.invoke.MethodType;
31 import java.lang.invoke.VarHandle.VarHandleDesc;
32 
33 /**
34  * A <a href="package-summary.html#nominal">nominal descriptor</a> for a loadable
35  * constant value, as defined in JVMS 4.4. Such a descriptor can be resolved via
36  * {@link ConstantDesc#resolveConstantDesc(MethodHandles.Lookup)} to yield the
37  * constant value itself.
38  *
39  * <p>Class names in a nominal descriptor, like class names in the constant pool
40  * of a classfile, must be interpreted with respect to a particular class
41  * loader, which is not part of the nominal descriptor.
42  *
43  * <p>Static constants that are expressible natively in the constant pool ({@link String},
44  * {@link Integer}, {@link Long}, {@link Float}, and {@link Double}) implement
45  * {@link ConstantDesc}, and serve as nominal descriptors for themselves.
46  * Native linkable constants ({@link Class}, {@link MethodType}, and
47  * {@link MethodHandle}) have counterpart {@linkplain ConstantDesc} types:
48  * {@link ClassDesc}, {@link MethodTypeDesc}, and {@link MethodHandleDesc}.
49  * Other constants are represented by subtypes of {@link DynamicConstantDesc}.
50  *
51  * <p>APIs that perform generation or parsing of bytecode are encouraged to use
52  * {@linkplain ConstantDesc} to describe the operand of an {@code ldc} instruction
53  * (including dynamic constants), the static bootstrap arguments of
54  * dynamic constants and {@code invokedynamic} instructions, and other
55  * bytecodes or classfile structures that make use of the constant pool.
56  *
57  * <p>Constants describing various common constants (such as {@link ClassDesc}
58  * instances for platform types) can be found in {@link ConstantDescs}.
59  *
60  * <p>Implementations of {@linkplain ConstantDesc} should be immutable
61  * and their behavior should not rely on object identity.
62  *
63  * <p>Non-platform classes should not implement {@linkplain ConstantDesc} directly.
64  * Instead, they should extend {@link DynamicConstantDesc} (as {@link EnumDesc}
65  * and {@link VarHandleDesc} do.)
66  *
67  * <p>Nominal descriptors should be compared using the
68  * {@link Object#equals(Object)} method. There is no guarantee that any
69  * particular entity will always be represented by the same descriptor instance.
70  *
71  * @see Constable
72  * @see ConstantDescs
73  *
74  * @jvms 4.4 The Constant Pool
75  *
76  * @since 12
77  */
78 public sealed interface ConstantDesc
79         permits ClassDesc,
80                 MethodHandleDesc,
81                 MethodTypeDesc,
82                 Double,
83                 DynamicConstantDesc,
84                 Float,
85                 Integer,
86                 Long,
87                 String {
88     /**
89      * Resolves this descriptor reflectively, emulating the resolution behavior
90      * of JVMS 5.4.3 and the access control behavior of JVMS 5.4.4.  The resolution
91      * and access control context is provided by the {@link MethodHandles.Lookup}
92      * parameter.  No caching of the resulting value is performed.
93      *
94      * @param lookup The {@link MethodHandles.Lookup} to provide name resolution
95      *               and access control context
96      * @return the resolved constant value
97      * @throws ReflectiveOperationException if a class, method, or field
98      * could not be reflectively resolved in the course of resolution
99      * @throws LinkageError if a linkage error occurs
100      *
101      * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors
102      * that are not representable by {@linkplain MethodType}, such as methods with
103      * more than 255 parameter slots, so attempts to resolve these may result in errors.
104      *
105      * @jvms 5.4.3 Resolution
106      * @jvms 5.4.4 Access Control
107      */
resolveConstantDesc(MethodHandles.Lookup lookup)108     Object resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;
109 }
110