Clients can create new carrier instances by describing a carrier shape, that is, a method type whose parameter types describe the types of the carrier component values, or by providing the parameter types directly.
// Create a carrier for a string and an integer
CarrierElements elements = CarrierFactory.of(String.class, int.class);
// Fetch the carrier constructor MethodHandle
MethodHandle initializingConstructor = elements.initializingConstructor();
// Fetch the list of carrier component MethodHandles
List<MethodHandle> components = elements.components();
// Create an instance of the carrier with a string and an integer
Object carrier = initializingConstructor.invokeExact("abc", 10);
// Extract the first component, type string
String string = (String)components.get(0).invokeExact(carrier);
// Extract the second component, type int
int i = (int)components.get(1).invokeExact(carrier);
Carriers ensures that the same underlying carrier
class is used when the same component types are provided.
// Describe carrier using a MethodType
MethodType mt = MethodType.methodType(Object.class, String.class, int.class);
// Fetch the carrier constructor MethodHandle
MethodHandle constructor = Carriers.constructor(mt);
// Fetch the list of carrier component MethodHandles
List<MethodHandle> components = Carriers.components(mt);
Implementation Note
The strategy for storing components is deliberately left unspecified so that future improvements will not be hampered by issues of backward compatibility.
| Modifier and Type | Class and Description |
|---|---|
| private static record | Carriers.
Class used to tally and track the number of ints, longs and objects. |
| pack-priv static class | Carriers.
Instances of this class provide the |
| pack-priv static class | Carriers.
This factory class generates |
| pack-priv static class | Carriers.
Wrapper object for carrier data. |
| pack-priv static class | Carriers.
Factory for carriers that are backed by long[] and Object[]. |
| private static class | Carriers.
Shape of carrier based on counts of each of the three fundamental data types. |
| Modifier and Type | Field and Description |
|---|---|
| private static final MethodHandle | |
| private static final MethodHandle | |
| private static final MethodHandle | |
| pack-priv static final int | LONG_SLOTS
Number of integer slots used by a long. |
| private static final MethodHandle | |
| public static final int | MAX_COMPONENTS
Maximum number of components in a carrier (based on the maximum number of args to a constructor.) |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static Class | Returns: the underlying carrier class of the carrier representingmethodTypeMethodType whose parameter types supply the shape of the
carrier's componentsReturns the underlying carrier class of the carrier representing |
| pack-priv static MethodHandle | Returns: a component accessorMethodHandle for component i of the
carrier representing methodType. The receiver type of the accessor will always
be ObjectMethodType whose parameter types supply the shape of the
carrier's componentscomponent index i)Returns a component accessor |
| pack-priv static List | Returns: immutable list of component accessorMethodHandles for
all the components of the carrier representing methodType. The receiver type of
the accessors will always be ObjectMethodType whose parameter types supply the shape of the
carrier's componentsReturns immutable list of component accessor |
| pack-priv static MethodHandle | Returns: the constructorMethodHandle for the carrier representing methodType. The carrier constructor will always have a return type of ObjectMethodType whose parameter types supply the shape of the
carrier's componentsReturns the constructor |
| pack-priv static MethodHandle | Returns: the initializerMethodHandle for the carrier representing methodType. The carrier initializer will always take an Object along with
component values and a return type of ObjectMethodType whose parameter types supply the shape of the
carrier's componentsReturns the initializer |
| pack-priv static MethodHandle | Returns: the combinationMethodHandle of the constructor and initializer
for the carrier representing methodType. The carrier constructor/initializer
will always take the component values and a return type of ObjectMethodType whose parameter types supply the shape of the
carrier's componentsReturns the combination |
| pack-priv static List | Returns: list of components reshapedcarrier reshape carrierShape,carrier components to reshape components)Given components array, recast and reorder components to match shape. |
| pack-priv static MethodHandle | Returns: constructor with arguments recasted and reorderedcarrier shape carrierShape,carrier constructor to reshape initializer)Given an initializer |