Class ConstantBootstraps
java.lang.Object
java.lang.invoke.ConstantBootstraps
Bootstrap methods for dynamically-computed constants.
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.
- Since:
- 11
-
Method Summary
Modifier and TypeMethodDescriptionstatic VarHandlearrayVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> arrayClass) Finds aVarHandlefor an array type.static <E extends Enum<E>>
EenumConstant(MethodHandles.Lookup lookup, String name, Class<E> type) Returns anenumconstant of the type specified bytypewith the name specified byname.static ObjectexplicitCast(MethodHandles.Lookup lookup, String name, Class<?> dstType, Object value) Applies a conversion from a source type to a destination type.static VarHandlefieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType) Finds aVarHandlefor an instance field.static ObjectgetStaticFinal(MethodHandles.Lookup lookup, String name, Class<?> type) 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 ObjectgetStaticFinal(MethodHandles.Lookup lookup, String name, Class<?> type, Class<?> declaringClass) Returns the value of a static final field.static Objectinvoke(MethodHandles.Lookup lookup, String name, Class<?> type, MethodHandle handle, Object... args) Returns the result of invoking a method handle with the provided arguments.static ObjectnullConstant(MethodHandles.Lookup lookup, String name, Class<?> type) Returns anullobject reference for the reference type specified bytype.static Class<?> primitiveClass(MethodHandles.Lookup lookup, String name, Class<?> type) Returns aClassmirror for the primitive type whose type descriptor is specified byname.static VarHandlestaticFieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType) Finds aVarHandlefor a static field.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
nullConstant
Returns anullobject reference for the reference type specified bytype.- Parameters:
lookup- unusedname- unusedtype- a reference type- Returns:
- a
nullvalue - Throws:
IllegalArgumentException- iftypeis not a reference type
-
primitiveClass
Returns aClassmirror for the primitive type whose type descriptor is specified byname.- Parameters:
lookup- unusedname- the descriptor (JVMS §4.3) of the desired primitive typetype- the required result type (must beClass.class)- Returns:
- the
Classmirror - Throws:
IllegalArgumentException- if the name is not a descriptor for a primitive type or the type is notClass.class
-
enumConstant
public static <E extends Enum<E>> E enumConstant(MethodHandles.Lookup lookup, String name, Class<E> type) Returns anenumconstant of the type specified bytypewith the name specified byname.- Type Parameters:
E- The enum type for which a constant value is to be returned- Parameters:
lookup- 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- theClassobject describing the enum type for which a constant is to be returned- Returns:
- the enum constant of the specified enum type with the specified name
- Throws:
IllegalAccessError- 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 type- See Also:
-
getStaticFinal
public static Object getStaticFinal(MethodHandles.Lookup lookup, String name, Class<?> type, Class<?> declaringClass) Returns the value of a static final field.- Parameters:
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 declared- Returns:
- the value of the field
- Throws:
IllegalAccessError- 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 notfinal
-
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.) This is a simplified form ofgetStaticFinal(MethodHandles.Lookup, String, Class, Class)for the case where a class declares distinguished constant instances of itself.- Parameters:
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 field- Returns:
- the value of the field
- Throws:
IllegalAccessError- 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 notfinal- See Also:
-
invoke
public static Object invoke(MethodHandles.Lookup lookup, String name, Class<?> type, MethodHandle handle, Object... args) throws Throwable Returns the result of invoking a method handle with the provided arguments.This method behaves as if the method handle to be invoked is the result of adapting the given method handle, via
MethodHandle.asType(MethodType), to adjust the return type to the desired type.- Parameters:
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 withMethodHandle.invokeWithArguments(Object...). Each argument may benull.- Returns:
- the result of invoking the method handle
- Throws:
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 invocation
-
fieldVarHandle
public static VarHandle fieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType) Finds aVarHandlefor an instance field.- Parameters:
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 beClass<VarHandle>)declaringClass- the class in which the field is declaredfieldType- the type of the field- Returns:
- the
VarHandle - Throws:
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 notVarHandle
-
staticFieldVarHandle
public static VarHandle staticFieldVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> declaringClass, Class<?> fieldType) Finds aVarHandlefor a static field.- Parameters:
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 beClass<VarHandle>)declaringClass- the class in which the field is declaredfieldType- the type of the field- Returns:
- the
VarHandle - Throws:
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 notVarHandle
-
arrayVarHandle
public static VarHandle arrayVarHandle(MethodHandles.Lookup lookup, String name, Class<VarHandle> type, Class<?> arrayClass) Finds aVarHandlefor an array type.- Parameters:
lookup- the lookup context describing the class performing the operation (normally stacked by the JVM)name- unusedtype- the required result type (must beClass<VarHandle>)arrayClass- the type of the array- Returns:
- the
VarHandle - Throws:
IllegalAccessError- if the component type of the array is not accessible to the class performing the operationIllegalArgumentException- if the type is notVarHandle
-
explicitCast
public static Object explicitCast(MethodHandles.Lookup lookup, String name, Class<?> dstType, Object value) throws ClassCastException Applies a conversion from a source type to a destination type.Given a destination type
dstTypeand an input valuevalue, one of the following will happen:- If
dstTypeisvoid.class, aClassCastExceptionis thrown. - If
dstTypeisObject.class,valueis returned as is.
Otherwise one of the following conversions is applied to
value:- If
dstTypeis a reference type, a reference cast is applied tovalueas if by callingdstType.cast(value). - Otherwise,
dstTypeis a primitive type:- If
valueis null, the default value (JVMS §2.3) ofdstTypeis returned. - If the runtime type of
valueis a primitive wrapper type (such asInteger), a Java unboxing conversion is applied (JLS §5.1.8).- If the runtime type is
Boolean, the unboxing result is then converted toint, wheretruebecomes1andfalsebecomes0.
- If
dstTypeis notboolean, the cast converts directly todstType. - If
dstTypeisboolean, the cast converts toint, and the resultingbooleanis produced by testing whether the least significant bit of the castintis 1.
- If the runtime type is
- Otherwise, a
ClassCastExceptionis thrown.
- If
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);- Parameters:
lookup- unusedname- unuseddstType- the destination type of the conversionvalue- the value to be converted, may be null- Returns:
- the converted value
- Throws:
ClassCastException- whendstTypeisvoid; whendstTypeis a reference type, and the reference cast fails; or whendstTypeis primitive, andvalueis an instance of a reference type that is not a wrapper class- Since:
- 15
- If
-