public final class StringConcatFactory extends Object
Methods to facilitate the creation of String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. These methods are typically used as bootstrap methods for
invokedynamic
call sites, to support the string concatenation feature of the Java Programming Language.
Indirect access to the behavior specified by the provided
MethodHandle
proceeds in order through two phases:
CallSite
holds the MethodHandle
pointing to the exact concatenation method. The concatenation methods may be shared among different CallSite
s, e.g. if linkage methods produce them as pure functions.
MethodHandle
is invoked with the static arguments and any additional dynamic arguments provided on invocation, as if by MethodHandle.invoke(Object...)
. This class provides two forms of linkage methods: a simple version (makeConcat(java.lang.invoke.MethodHandles.Lookup, String, MethodType)
) using only the dynamic arguments, and an advanced version (makeConcatWithConstants(java.lang.invoke.MethodHandles.Lookup, String, MethodType, String, Object...)
using the advanced forms of capturing the constant arguments. The advanced strategy can produce marginally better invocation bytecode, at the expense of exploding the number of shapes of string concatenation methods present at runtime, because those shapes would include constant static arguments as well.
There is a JVM limit (classfile structural constraint): no method can call with more than 255 slots. This limits the number of static and dynamic arguments one can pass to bootstrap method. Since there are potential concatenation strategies that use MethodHandle
combinators, we need to reserve a few empty slots on the parameter lists to capture the temporal results. This is why bootstrap methods in this factory do not accept more than 200 argument slots. Users requiring more than 200 argument slots in concatenation are expected to split the large concatenation in smaller expressions.
Modifier and Type | Field | Description |
---|---|---|
static final int |
MAX_INDY_CONCAT_ARG_SLOTS |
Preview. Maximum number of argument slots in String Concat call. |
Modifier and Type | Method | Description |
---|---|---|
static CallSite |
makeConcat |
Facilitates the creation of optimized String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. |
static CallSite |
makeConcatWithConstants |
Facilitates the creation of optimized String concatenation methods, that can be used to efficiently concatenate a known number of arguments of known types, possibly after type adaptation and partial evaluation of arguments. |
static MethodHandle |
makeConcatWithTemplate |
Preview. Simplified concatenation method to facilitate StringTemplate PREVIEW concatenation. |
static List |
makeConcatWithTemplateCluster |
Preview. This method breaks up large concatenations into separate MethodHandles based on the number of slots required per MethodHandle . |
static MethodHandle |
makeConcatWithTemplateGetters |
Preview. This method creates a MethodHandle expecting one input, the receiver of the supplied getters. |
public static final int MAX_INDY_CONCAT_ARG_SLOTS
MAX_INDY_CONCAT_ARG_SLOTS
is a preview API of the Java platform. public static CallSite makeConcat(MethodHandles.Lookup lookup, String name, MethodType concatType) throws StringConcatException
invokedynamic
call sites, to support the string concatenation feature of the Java Programming Language. When the target of the CallSite
returned from this method is invoked, it returns the result of String concatenation, taking all function arguments passed to the linkage method as inputs for concatenation. The target signature is given by concatType
. For a target accepting:
Assume the linkage arguments are as follows:
concatType
, describing the CallSite
signatureThen the following linkage invariants must hold:
concatType
is less than or equal to 200concatType
is assignable from String
lookup
- Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic
, this is stacked automatically by the VM.name
- The name of the method to implement. This name is arbitrary, and has no meaning for this linkage method. When used with invokedynamic
, this is provided by the NameAndType
of the InvokeDynamic
structure and is stacked automatically by the VM.concatType
- The expected signature of the CallSite
. The parameter types represent the types of concatenation arguments; the return type is always assignable from String
. When used with invokedynamic
, this is provided by the NameAndType
of the
InvokeDynamic
structure and is stacked automatically by the VM.concatType
.StringConcatException
- If any of the linkage invariants described here are violated, or the lookup context does not have private access privileges.NullPointerException
- If any of the incoming arguments is null. This will never happen when a bootstrap method is called with invokedynamic.public static CallSite makeConcatWithConstants(MethodHandles.Lookup lookup, String name, MethodType concatType, String recipe, Object... constants) throws StringConcatException
invokedynamic
call sites, to support the string concatenation feature of the Java Programming Language. When the target of the CallSite
returned from this method is invoked, it returns the result of String concatenation, taking all function arguments and constants passed to the linkage method as inputs for concatenation. The target signature is given by concatType
, and does not include constants. For a target accepting:
The concatenation recipe is a String description for the way to construct a concatenated String from the arguments and constants. The recipe is processed from left to right, and each character represents an input to concatenation. Recipe characters mean:
toString
to perform a one-time String conversion.Assume the linkage arguments are as follows:
concatType
, describing the CallSite
signaturerecipe
, describing the String recipeconstants
, the vararg array of constantsThen the following linkage invariants must hold:
concatType
is less than or equal to 200concatType
is equal to number of \1 tags in recipe
concatType
is assignable from String
, and matches the return type of the returned MethodHandle
constants
is equal to number of \2 tags in recipe
lookup
- Represents a lookup context with the accessibility privileges of the caller. Specifically, the lookup context must have full privilege access. When used with invokedynamic
, this is stacked automatically by the VM.name
- The name of the method to implement. This name is arbitrary, and has no meaning for this linkage method. When used with invokedynamic
, this is provided by the NameAndType
of the InvokeDynamic
structure and is stacked automatically by the VM.concatType
- The expected signature of the CallSite
. The parameter types represent the types of dynamic concatenation arguments; the return type is always assignable from String
. When used with
invokedynamic
, this is provided by the
NameAndType
of the InvokeDynamic
structure and is stacked automatically by the VM.recipe
- Concatenation recipe, described above.constants
- A vararg parameter representing the constants passed to the linkage method.concatType
.StringConcatException
- If any of the linkage invariants described here are violated, or the lookup context does not have private access privileges.NullPointerException
- If any of the incoming arguments is null, or any constant in recipe
is null. This will never happen when a bootstrap method is called with invokedynamic.public static MethodHandle makeConcatWithTemplate(List<String> fragments, List<Class<?>> ptypes) throws StringConcatException
makeConcatWithTemplate
is a preview API of the Java platform. makeConcatWithTemplate
when preview features are enabled.StringTemplate
PREVIEW concatenation. This method returns a single concatenation method that interleaves fragments and values. fragment|value|fragment|value|...|value|fragment. The number of fragments must be one more that the number of ptypes. The total number of slots used by the ptypes must be less than or equal to MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW.fragments
- list of string fragmentsptypes
- list of expression typesMethodHandle
for concatenationStringConcatException
- If any of the linkage invariants are violated.NullPointerException
- If any of the incoming arguments is null.IllegalArgumentException
- If the number of value slots exceed MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW.public static List<MethodHandle> makeConcatWithTemplateCluster(List<String> fragments, List<Class<?>> ptypes, int maxSlots) throws StringConcatException
makeConcatWithTemplateCluster
is a preview API of the Java platform. makeConcatWithTemplateCluster
when preview features are enabled.MethodHandles
based on the number of slots required per MethodHandle
. Each MethodHandle
after the first will have an extra String
slot for the result from the previous MethodHandle
. makeConcatWithTemplate(java.util.List<java.lang.String>, java.util.List<java.lang.Class<?>>)
PREVIEW is used to construct the MethodHandles
. The total number of slots used by the ptypes is open ended. However, care must be given when combining the MethodHandles
so that the combine total does not exceed the 255 slot limit.fragments
- list of string fragmentsptypes
- list of expression typesmaxSlots
- maximum number of slots per MethodHandle
.MethodHandles
IllegalArgumentException
- If maxSlots is not between 1 and MAX_INDY_CONCAT_ARG_SLOTS.StringConcatException
- If any of the linkage invariants are violated.NullPointerException
- If any of the incoming arguments is null.IllegalArgumentException
- If the number of value slots exceed MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW.public static MethodHandle makeConcatWithTemplateGetters(List<String> fragments, List<MethodHandle> getters, int maxSlots) throws StringConcatException
makeConcatWithTemplateGetters
is a preview API of the Java platform. makeConcatWithTemplateGetters
when preview features are enabled.MethodHandle
expecting one input, the receiver of the supplied getters. This method uses makeConcatWithTemplateCluster(java.util.List<java.lang.String>, java.util.List<java.lang.Class<?>>, int)
PREVIEW to create the intermediate MethodHandles
.fragments
- list of string fragmentsgetters
- list of getter MethodHandles
maxSlots
- maximum number of slots per MethodHandle
in cluster.MethodHandle
for concatenationIllegalArgumentException
- If maxSlots is not between 1 and MAX_INDY_CONCAT_ARG_SLOTS or if the getters don't use the same argument typeStringConcatException
- If any of the linkage invariants are violatedNullPointerException
- If any of the incoming arguments is nullIllegalArgumentException
- If the number of value slots exceed MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW.
© 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/StringConcatFactory.html
MAX_INDY_CONCAT_ARG_SLOTS
when preview features are enabled.