APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang.runtime

pack-priv final Class Carriers

extends Object
Class Inheritance
Imports
java.lang.invoke.MethodHandle, .MethodHandles, .MethodHandles.Lookup, .MethodType, java.util.List, .Map, .Objects, java.util.concurrent.ConcurrentHashMap, jdk.internal.misc.Unsafe, jdk.internal.util.ReferencedKeyMap

A carrier is an opaque object that can be used to store component values while avoiding primitive boxing associated with collection objects. Component values can be primitive or Object.

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);
// 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);
Alternatively, the client can use static methods when the carrier use is scattered. This is possible since 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);
// 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.

Since
21 Warning: This class is part of PreviewFeature.Feature.STRING_TEMPLATES. Do not rely on its availability.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
Carriers.CarrierCounts

Class used to tally and track the number of ints, longs and objects.

pack-priv static class
Carriers.CarrierElements

Instances of this class provide the MethodHandles to the constructor and accessors of a carrier object.

pack-priv static class
Carriers.CarrierFactory

This factory class generates CarrierElements instances containing the MethodHandles to the constructor and accessors of a carrier object.

pack-priv static class
Carriers.CarrierObject

Wrapper object for carrier data.

pack-priv static class
Carriers.CarrierObjectFactory

Factory for carriers that are backed by long[] and Object[].

private static class
Carriers.CarrierShape

Shape of carrier based on counts of each of the three fundamental data types.

Field Summary

Modifier and TypeField 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.)

Constructor Summary

AccessConstructor and Description
private
Carriers()

Constructor

Method Summary

Modifier and TypeMethod and Description
pack-priv static Class<?>

Returns:

the underlying carrier class of the carrier representing methodType
carrierClass
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
)

Returns the underlying carrier class of the carrier representing methodType.

pack-priv static MethodHandle

Returns:

a component accessor MethodHandle for component i of the carrier representing methodType. The receiver type of the accessor will always be Object
component
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
,
int
component index
i
)

Returns a component accessor MethodHandle for component i of the carrier representing methodType.

pack-priv static List<MethodHandle>

Returns:

immutable list of component accessor MethodHandles for all the components of the carrier representing methodType. The receiver type of the accessors will always be Object
components
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
)

Returns immutable list of component accessor MethodHandles for all the components of the carrier representing methodType.

pack-priv static MethodHandle

Returns:

the constructor MethodHandle for the carrier representing methodType. The carrier constructor will always have a return type of Object
constructor
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
)

Returns the constructor MethodHandle for the carrier representing methodType.

pack-priv static MethodHandle

Returns:

the initializer MethodHandle for the carrier representing methodType. The carrier initializer will always take an Object along with component values and a return type of Object
initializer
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
)

Returns the initializer MethodHandle for the carrier representing methodType.

pack-priv static MethodHandle

Returns:

the combination MethodHandle 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 Object
initializingConstructor
(MethodType
MethodType whose parameter types supply the shape of the carrier's components
methodType
)

Returns the combination MethodHandle of the constructor and initializer for the carrier representing methodType.

pack-priv static List<MethodHandle>

Returns:

list of components reshaped
reshapeComponents
(Carriers.CarrierShape
carrier reshape
carrierShape
,
MethodHandle[]
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 reordered
reshapeInitializer
(Carriers.CarrierShape
carrier shape
carrierShape
,
MethodHandle
carrier constructor to reshape
initializer
)

Given an initializer MethodHandle recast and reorder arguments to match shape.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait