public final class ConstantBootstraps extends Object
The bootstrap methods in this class will throw a NullPointerException
for any reference argument that is null
, unless the argument is specified to be unused or specified to accept a null
value.
Modifier and Type | Method | Description |
---|---|---|
static VarHandle |
arrayVarHandle |
Finds a VarHandle for an array type. |
static <E extends Enum<E>> |
enumConstant |
Returns an enum constant of the type specified by type with the name specified by name . |
static Object |
explicitCast |
Applies a conversion from a source type to a destination type. |
static VarHandle |
fieldVarHandle |
Finds a VarHandle for an instance field. |
static Object |
getStaticFinal |
Returns the value of a static final field declared in the class which is the same as the field's type (or, for primitive-valued fields, declared in the wrapper class.) |
static Object |
getStaticFinal |
Returns the value of a static final field. |
static Object |
invoke |
Returns the result of invoking a method handle with the provided arguments. |
static Object |
nullConstant |
Returns a null object reference for the reference type specified by type . |
static Class |
primitiveClass |
Returns a Class mirror for the primitive type whose type descriptor is specified by name . |
static VarHandle |
staticFieldVarHandle |
Finds a VarHandle for a static field. |
public static Object nullConstant(MethodHandles.Lookup lookup, String name, Class<?> type)
null
object reference for the reference type specified by type
.lookup
- unusedname
- unusedtype
- a reference typenull
valueIllegalArgumentException
- if type
is not a reference typepublic static Class<?> primitiveClass(MethodHandles.Lookup lookup, String name, Class<?> type)
Class
mirror for the primitive type whose type descriptor is specified by name
.lookup
- unusedname
- the descriptor (JVMS 4.3) of the desired primitive typetype
- the required result type (must be Class.class
)Class
mirrorIllegalArgumentException
- if the name is not a descriptor for a primitive type or the type is not Class.class
public static <E extends Enum<E>> E enumConstant(MethodHandles.Lookup lookup, String name, Class<E> type)
enum
constant of the type specified by type
with the name specified by name
.E
- The enum type for which a constant value is to be returnedlookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- the name of the constant to return, which must exactly match an enum constant in the specified type.type
- the Class
object describing the enum type for which a constant is to be returnedIllegalAccessError
- if the declaring class or the field is not accessible to the class performing the operationIllegalArgumentException
- if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum typepublic static Object getStaticFinal(MethodHandles.Lookup lookup, String name, Class<?> type, Class<?> declaringClass)
lookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- the name of the fieldtype
- the type of the fielddeclaringClass
- the class in which the field is declaredIllegalAccessError
- if the declaring class or the field is not accessible to the class performing the operationNoSuchFieldError
- if the specified field does not existIncompatibleClassChangeError
- if the specified field is not final
public static Object getStaticFinal(MethodHandles.Lookup lookup, String name, Class<?> type)
getStaticFinal(MethodHandles.Lookup, String, Class, Class)
for the case where a class declares distinguished constant instances of itself.lookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- the name of the fieldtype
- the type of the fieldIllegalAccessError
- if the declaring class or the field is not accessible to the class performing the operationNoSuchFieldError
- if the specified field does not existIncompatibleClassChangeError
- if the specified field is not final
public static Object invoke(MethodHandles.Lookup lookup, String name, Class<?> type, MethodHandle handle, Object... args) throws Throwable
This method behaves as if the method handle to be invoked is the result of adapting the given method handle, via MethodHandle.asType(java.lang.invoke.MethodType)
, to adjust the return type to the desired type.
lookup
- unusedname
- unusedtype
- the desired type of the value to be returned, which must be compatible with the return type of the method handlehandle
- the method handle to be invokedargs
- the arguments to pass to the method handle, as if with MethodHandle.invokeWithArguments(java.lang.Object...)
. Each argument may be null
.WrongMethodTypeException
- if the handle's method type cannot be adjusted to take the given number of arguments, or if the handle's return type cannot be adjusted to the desired typeClassCastException
- if an argument or the result produced by invoking the handle cannot be converted by reference castingThrowable
- anything thrown by the method handle invocationpublic static VarHandle fieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType)
VarHandle
for an instance field.lookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- the name of the fieldtype
- the required result type (must be Class<VarHandle>
)declaringClass
- the class in which the field is declaredfieldType
- the type of the fieldVarHandle
IllegalAccessError
- if the declaring class or the field is not accessible to the class performing the operationNoSuchFieldError
- if the specified field does not existIllegalArgumentException
- if the type is not VarHandle
public static VarHandle staticFieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType)
VarHandle
for a static field.lookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- the name of the fieldtype
- the required result type (must be Class<VarHandle>
)declaringClass
- the class in which the field is declaredfieldType
- the type of the fieldVarHandle
IllegalAccessError
- if the declaring class or the field is not accessible to the class performing the operationNoSuchFieldError
- if the specified field does not existIllegalArgumentException
- if the type is not VarHandle
public static VarHandle arrayVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> arrayClass)
VarHandle
for an array type.lookup
- the lookup context describing the class performing the operation (normally stacked by the JVM)name
- unusedtype
- the required result type (must be Class<VarHandle>
)arrayClass
- the type of the arrayVarHandle
IllegalAccessError
- if the component type of the array is not accessible to the class performing the operationIllegalArgumentException
- if the type is not VarHandle
public static Object explicitCast(MethodHandles.Lookup lookup, String name, Class<?> dstType, Object value) throws ClassCastException
Given a destination type dstType
and an input value value
, one of the following will happen:
dstType
is void.class
, a ClassCastException
is thrown. dstType
is Object.class
, value
is returned as is. Otherwise one of the following conversions is applied to value
:
dstType
is a reference type, a reference cast is applied to value
as if by calling dstType.cast(value)
. dstType
is a primitive type, then, if the runtime type of value
is a primitive wrapper type (such as Integer
), a Java unboxing conversion is applied 5.1.8 followed by a Java casting conversion 5.5 converting either directly to dstType
, or, if dstType
is boolean
, to int
, which is then converted to either true
or false
depending on whether the least-significant-bit is 1 or 0 respectively. If the runtime type of value
is not a primitive wrapper type a ClassCastException
is thrown. The result is the same as when using the following code:
MethodHandle id = MethodHandles.identity(dstType); MethodType mt = MethodType.methodType(dstType, Object.class); MethodHandle conv = MethodHandles.explicitCastArguments(id, mt); return conv.invoke(value);
lookup
- unusedname
- unuseddstType
- the destination type of the conversionvalue
- the value to be convertedClassCastException
- when dstType
is void
, when a cast per (1) fails, or when dstType
is a primitive type and the runtime type of value
is not a primitive wrapper type (such as Integer
)
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/invoke/ConstantBootstraps.html