Package net.bytebuddy.utility
Class JavaConstant.Dynamic
- java.lang.Object
-
- net.bytebuddy.utility.JavaConstant.Dynamic
-
- All Implemented Interfaces:
ConstantValue
,JavaConstant
- Enclosing interface:
- JavaConstant
public static class JavaConstant.Dynamic extends java.lang.Object implements JavaConstant
Represents a dynamically resolved constant pool entry of a class file. This feature is supported for class files in version 11 and newer.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.bytebuddy.utility.JavaConstant
JavaConstant.Dynamic, JavaConstant.MethodHandle, JavaConstant.MethodType, JavaConstant.Simple<T>, JavaConstant.Visitor<T>
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<JavaConstant>
arguments
A list of the arguments to the dynamic constant.private JavaConstant.MethodHandle
bootstrap
A handle representation of the bootstrap method.static java.lang.String
DEFAULT_NAME
The default name of a dynamic constant.private java.lang.String
name
The name of the dynamic constant.private TypeDescription
typeDescription
A description of the represented value's type.
-
Constructor Summary
Constructors Constructor Description Dynamic(java.lang.String name, TypeDescription typeDescription, JavaConstant.MethodHandle bootstrap, java.util.List<JavaConstant> arguments)
Creates a dynamic resolved constant.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
accept(JavaConstant.Visitor<T> visitor)
Applies the supplied visitor to this constant type with its respective callback.static JavaConstant.Dynamic
bootstrap(java.lang.String name, java.lang.reflect.Constructor<?> constructor, java.lang.Object... constant)
Binds the supplied bootstrap constructor for the resolution of a dynamic constant.static JavaConstant.Dynamic
bootstrap(java.lang.String name, java.lang.reflect.Constructor<?> constructor, java.util.List<?> constants)
Binds the supplied bootstrap constructor for the resolution of a dynamic constant.static JavaConstant.Dynamic
bootstrap(java.lang.String name, java.lang.reflect.Method method, java.lang.Object... constant)
Binds the supplied bootstrap method for the resolution of a dynamic constant.static JavaConstant.Dynamic
bootstrap(java.lang.String name, java.lang.reflect.Method method, java.util.List<?> constants)
Binds the supplied bootstrap method for the resolution of a dynamic constant.static JavaConstant.Dynamic
bootstrap(java.lang.String name, MethodDescription.InDefinedShape bootstrapMethod, java.lang.Object... constant)
Binds the supplied bootstrap method or constructor for the resolution of a dynamic constant.static JavaConstant.Dynamic
bootstrap(java.lang.String name, MethodDescription.InDefinedShape bootstrap, java.util.List<?> arguments)
Binds the supplied bootstrap method or constructor for the resolution of a dynamic constant.boolean
equals(java.lang.Object object)
java.util.List<JavaConstant>
getArguments()
Returns a list of the arguments to the dynamic constant.JavaConstant.MethodHandle
getBootstrap()
Returns a handle representation of the bootstrap method.java.lang.String
getName()
Returns the name of the dynamic constant.TypeDescription
getTypeDescription()
Returns a description of the type of this constant.int
hashCode()
static JavaConstant
ofArrayVarHandle(java.lang.Class<?> type)
Resolves a var handle constant for an array.static JavaConstant
ofArrayVarHandle(TypeDescription typeDescription)
Resolves a var handle constant for an array.static JavaConstant.Dynamic
ofAsm(TypePool typePool, org.objectweb.asm.ConstantDynamic constantDynamic)
Resolves an ASMConstantDynamic
to aJavaConstant.Dynamic
.static JavaConstant
ofEnumeration(java.lang.Enum<?> enumeration)
Returns aEnum
value constant.static JavaConstant
ofEnumeration(EnumerationDescription enumerationDescription)
Returns aEnum
value constant.static JavaConstant.Dynamic
ofField(java.lang.reflect.Field field)
Returns astatic
,final
field constant.static JavaConstant.Dynamic
ofField(FieldDescription.InDefinedShape fieldDescription)
Returns astatic
,final
field constant.static JavaConstant.Dynamic
ofInvocation(java.lang.reflect.Constructor<?> constructor, java.lang.Object... constant)
Represents a constant that is resolved by invoking a constructor.static JavaConstant.Dynamic
ofInvocation(java.lang.reflect.Constructor<?> constructor, java.util.List<?> constants)
Represents a constant that is resolved by invoking a constructor.static JavaConstant.Dynamic
ofInvocation(java.lang.reflect.Method method, java.lang.Object... constant)
Represents a constant that is resolved by invoking astatic
factory method.static JavaConstant.Dynamic
ofInvocation(java.lang.reflect.Method method, java.util.List<?> constants)
Represents a constant that is resolved by invoking astatic
factory method.static JavaConstant.Dynamic
ofInvocation(MethodDescription.InDefinedShape methodDescription, java.lang.Object... constant)
Represents a constant that is resolved by invoking astatic
factory method or a constructor.static JavaConstant.Dynamic
ofInvocation(MethodDescription.InDefinedShape methodDescription, java.util.List<?> constants)
Represents a constant that is resolved by invoking astatic
factory method or a constructor.static JavaConstant.Dynamic
ofNullConstant()
Returns a constantnull
value of typeObject
.static JavaConstant
ofPrimitiveType(java.lang.Class<?> type)
Returns aClass
constant for a primitive type.static JavaConstant
ofPrimitiveType(TypeDescription typeDescription)
Returns aClass
constant for a primitive type.static JavaConstant
ofVarHandle(java.lang.reflect.Field field)
Resolves a var handle constant for a field.static JavaConstant
ofVarHandle(FieldDescription.InDefinedShape fieldDescription)
Resolves a var handle constant for a field.java.lang.Object
toDescription()
Returns this constant as a Javajava.lang.constant.ConstantDesc
if the current VM is of at least version 12.StackManipulation
toStackManipulation()
Returns a stack manipulation loading this value.java.lang.String
toString()
JavaConstant
withType(java.lang.Class<?> type)
Resolves thisJavaConstant.Dynamic
constant to resolve the returned instance to the supplied type.JavaConstant
withType(TypeDescription typeDescription)
Resolves thisJavaConstant.Dynamic
constant to resolve the returned instance to the supplied type.
-
-
-
Field Detail
-
DEFAULT_NAME
public static final java.lang.String DEFAULT_NAME
The default name of a dynamic constant.- See Also:
- Constant Field Values
-
name
private final java.lang.String name
The name of the dynamic constant.
-
typeDescription
private final TypeDescription typeDescription
A description of the represented value's type.
-
bootstrap
private final JavaConstant.MethodHandle bootstrap
A handle representation of the bootstrap method.
-
arguments
private final java.util.List<JavaConstant> arguments
A list of the arguments to the dynamic constant.
-
-
Constructor Detail
-
Dynamic
public Dynamic(java.lang.String name, TypeDescription typeDescription, JavaConstant.MethodHandle bootstrap, java.util.List<JavaConstant> arguments)
Creates a dynamic resolved constant.- Parameters:
name
- The name of the dynamic constant.typeDescription
- A description of the represented value's type.bootstrap
- A handle representation of the bootstrap method.arguments
- A list of the arguments to the dynamic constant.
-
-
Method Detail
-
ofAsm
public static JavaConstant.Dynamic ofAsm(TypePool typePool, org.objectweb.asm.ConstantDynamic constantDynamic)
Resolves an ASMConstantDynamic
to aJavaConstant.Dynamic
.- Parameters:
typePool
- The type pool to use for resolving type descriptions.constantDynamic
- The ASM constant to translate.- Returns:
- An appropriate
JavaConstant
.
-
ofNullConstant
public static JavaConstant.Dynamic ofNullConstant()
Returns a constantnull
value of typeObject
.- Returns:
- A dynamically resolved null constant.
-
ofPrimitiveType
public static JavaConstant ofPrimitiveType(java.lang.Class<?> type)
Returns aClass
constant for a primitive type.- Parameters:
type
- The primitive type to represent.- Returns:
- A dynamically resolved primitive type constant.
-
ofPrimitiveType
public static JavaConstant ofPrimitiveType(TypeDescription typeDescription)
Returns aClass
constant for a primitive type.- Parameters:
typeDescription
- The primitive type to represent.- Returns:
- A dynamically resolved primitive type constant.
-
ofEnumeration
public static JavaConstant ofEnumeration(java.lang.Enum<?> enumeration)
Returns aEnum
value constant.- Parameters:
enumeration
- The enumeration value to represent.- Returns:
- A dynamically resolved enumeration constant.
-
ofEnumeration
public static JavaConstant ofEnumeration(EnumerationDescription enumerationDescription)
Returns aEnum
value constant.- Parameters:
enumerationDescription
- The enumeration value to represent.- Returns:
- A dynamically resolved enumeration constant.
-
ofField
public static JavaConstant.Dynamic ofField(java.lang.reflect.Field field)
Returns astatic
,final
field constant.- Parameters:
field
- The field to represent a value of.- Returns:
- A dynamically resolved field value constant.
-
ofField
public static JavaConstant.Dynamic ofField(FieldDescription.InDefinedShape fieldDescription)
Returns astatic
,final
field constant.- Parameters:
fieldDescription
- The field to represent a value of.- Returns:
- A dynamically resolved field value constant.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(java.lang.reflect.Method method, java.lang.Object... constant)
Represents a constant that is resolved by invoking astatic
factory method.- Parameters:
method
- The method to invoke to create the represented constant value.constant
- The method's constant arguments.- Returns:
- A dynamic constant that is resolved by the supplied factory method.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(java.lang.reflect.Method method, java.util.List<?> constants)
Represents a constant that is resolved by invoking astatic
factory method.- Parameters:
method
- The method to invoke to create the represented constant value.constants
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that is resolved by the supplied factory method.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(java.lang.reflect.Constructor<?> constructor, java.lang.Object... constant)
Represents a constant that is resolved by invoking a constructor.- Parameters:
constructor
- The constructor to invoke to create the represented constant value.constant
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that is resolved by the supplied constuctor.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(java.lang.reflect.Constructor<?> constructor, java.util.List<?> constants)
Represents a constant that is resolved by invoking a constructor.- Parameters:
constructor
- The constructor to invoke to create the represented constant value.constants
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that is resolved by the supplied constuctor.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(MethodDescription.InDefinedShape methodDescription, java.lang.Object... constant)
Represents a constant that is resolved by invoking astatic
factory method or a constructor.- Parameters:
methodDescription
- The method or constructor to invoke to create the represented constant value.constant
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that is resolved by the supplied factory method or constructor.
-
ofInvocation
public static JavaConstant.Dynamic ofInvocation(MethodDescription.InDefinedShape methodDescription, java.util.List<?> constants)
Represents a constant that is resolved by invoking astatic
factory method or a constructor.- Parameters:
methodDescription
- The method or constructor to invoke to create the represented constant value.constants
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that is resolved by the supplied factory method or constructor.
-
ofVarHandle
public static JavaConstant ofVarHandle(java.lang.reflect.Field field)
Resolves a var handle constant for a field.- Parameters:
field
- The field to represent a var handle for.- Returns:
- A dynamic constant that represents the created var handle constant.
-
ofVarHandle
public static JavaConstant ofVarHandle(FieldDescription.InDefinedShape fieldDescription)
Resolves a var handle constant for a field.- Parameters:
fieldDescription
- The field to represent a var handle for.- Returns:
- A dynamic constant that represents the created var handle constant.
-
ofArrayVarHandle
public static JavaConstant ofArrayVarHandle(java.lang.Class<?> type)
Resolves a var handle constant for an array.- Parameters:
type
- The array type for which the var handle is resolved.- Returns:
- A dynamic constant that represents the created var handle constant.
-
ofArrayVarHandle
public static JavaConstant ofArrayVarHandle(TypeDescription typeDescription)
Resolves a var handle constant for an array.- Parameters:
typeDescription
- The array type for which the var handle is resolved.- Returns:
- A dynamic constant that represents the created var handle constant.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, java.lang.reflect.Method method, java.lang.Object... constant)
Binds the supplied bootstrap method for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.method
- The bootstrap method to invoke.constant
- The arguments for the bootstrap method represented as primitive wrapper types,String
,TypeDescription
orJavaConstant
values or their loaded forms.- Returns:
- A dynamic constant that represents the bootstrapped method's result.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, java.lang.reflect.Method method, java.util.List<?> constants)
Binds the supplied bootstrap method for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.method
- The bootstrap method to invoke.constants
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that represents the bootstrapped method's result.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, java.lang.reflect.Constructor<?> constructor, java.lang.Object... constant)
Binds the supplied bootstrap constructor for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.constructor
- The bootstrap constructor to invoke.constant
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that represents the bootstrapped constructor's result.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, java.lang.reflect.Constructor<?> constructor, java.util.List<?> constants)
Binds the supplied bootstrap constructor for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.constructor
- The bootstrap constructor to invoke.constants
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that represents the bootstrapped constructor's result.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, MethodDescription.InDefinedShape bootstrapMethod, java.lang.Object... constant)
Binds the supplied bootstrap method or constructor for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.bootstrapMethod
- The bootstrap method or constructor to invoke.constant
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that represents the bootstrapped method's or constructor's result.
-
bootstrap
public static JavaConstant.Dynamic bootstrap(java.lang.String name, MethodDescription.InDefinedShape bootstrap, java.util.List<?> arguments)
Binds the supplied bootstrap method or constructor for the resolution of a dynamic constant.- Parameters:
name
- The name of the bootstrap constant that is provided to the bootstrap method or constructor.bootstrap
- The bootstrap method or constructor to invoke.arguments
- The constant values passed to the bootstrap method. Values can be represented either asTypeDescription
, asJavaConstant
, asString
or a primitiveint
,long
,float
ordouble
represented as wrapper type.- Returns:
- A dynamic constant that represents the bootstrapped method's or constructor's result.
-
getName
public java.lang.String getName()
Returns the name of the dynamic constant.- Returns:
- The name of the dynamic constant.
-
getBootstrap
public JavaConstant.MethodHandle getBootstrap()
Returns a handle representation of the bootstrap method.- Returns:
- A handle representation of the bootstrap method.
-
getArguments
public java.util.List<JavaConstant> getArguments()
Returns a list of the arguments to the dynamic constant.- Returns:
- A list of the arguments to the dynamic constant.
-
withType
public JavaConstant withType(java.lang.Class<?> type)
Resolves thisJavaConstant.Dynamic
constant to resolve the returned instance to the supplied type. The type must be a subtype of the bootstrap method's return type. Constructors cannot be resolved to a different type.- Parameters:
type
- The type to resolve the bootstrapped value to.- Returns:
- This dynamic constant but resolved to the supplied type.
-
withType
public JavaConstant withType(TypeDescription typeDescription)
Resolves thisJavaConstant.Dynamic
constant to resolve the returned instance to the supplied type. The type must be a subtype of the bootstrap method's return type. Constructors cannot be resolved to a different type.- Parameters:
typeDescription
- The type to resolve the bootstrapped value to.- Returns:
- This dynamic constant but resolved to the supplied type.
-
toDescription
public java.lang.Object toDescription()
Returns this constant as a Javajava.lang.constant.ConstantDesc
if the current VM is of at least version 12. If the current VM is of an older version and does not support the type, an exception is thrown.- Specified by:
toDescription
in interfaceJavaConstant
- Returns:
- This constant as a Java
java.lang.constant.ConstantDesc
.
-
accept
public <T> T accept(JavaConstant.Visitor<T> visitor)
Applies the supplied visitor to this constant type with its respective callback.- Specified by:
accept
in interfaceJavaConstant
- Type Parameters:
T
- The type of the value that is returned by the visitor.- Parameters:
visitor
- The visitor to dispatch.- Returns:
- The value that is returned by the supplied visitor.
-
getTypeDescription
public TypeDescription getTypeDescription()
Returns a description of the type of this constant.- Specified by:
getTypeDescription
in interfaceConstantValue
- Returns:
- A description of the type of this constant.
-
toStackManipulation
public StackManipulation toStackManipulation()
Returns a stack manipulation loading this value.- Specified by:
toStackManipulation
in interfaceConstantValue
- Returns:
- A stack manipulation loading this value.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(@MaybeNull java.lang.Object object)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-