Module java.base
Package java.lang.runtime

Class TemplateRuntime

java.lang.Object
java.lang.runtime.TemplateRuntime

public final classTemplateRuntimeextendsObject
TemplateRuntime is a preview API of the Java platform.
Programs can only useTemplateRuntime when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Manages string template bootstrap methods. These methods may be used, for example, by Java compiler implementations to createStringTemplatePREVIEW instances. For example, the java compiler will translate the following code;
int x = 10;int y = 20;StringTemplate st = RAW."\{x} + \{y} = \{x + y}";
to byte code that invokes thenewStringTemplate(java.lang.invoke.MethodHandles.Lookup, java.lang.String, java.lang.invoke.MethodType, java.lang.String...) bootstrap method to construct aCallSite that accepts two integers and produces a newStringTemplatePREVIEW instance.
MethodHandles.Lookup lookup = MethodHandles.lookup();MethodType mt = MethodType.methodType(StringTemplate.class, int.class, int.class);CallSite cs = TemplateRuntime.newStringTemplate(lookup, "", mt, "", " + ", " = ", "");...int x = 10;int y = 20;StringTemplate st = (StringTemplate)cs.getTarget().invokeExact(x, y);
If the string template requires more thanStringConcatFactory.MAX_INDY_CONCAT_ARG_SLOTSPREVIEW value slots, then the java compiler will use thenewLargeStringTemplate(java.lang.invoke.MethodHandles.Lookup, java.lang.String, java.lang.invoke.MethodType) bootstrap method instead. For example, the java compiler will translate the following code;
int[] a = new int[1000], b = new int[1000];...StringTemplate st = """     \{a[0]} - \{b[0]}     \{a[1]} - \{b[1]}     ...     \{a[999]} - \{b[999]}     """;
to byte code that invokes thenewLargeStringTemplate(java.lang.invoke.MethodHandles.Lookup, java.lang.String, java.lang.invoke.MethodType) bootstrap method to construct aCallSite that accepts an array of integers and produces a newStringTemplatePREVIEW instance.
MethodType mt = MethodType.methodType(StringTemplate.class, String[].class, Object[].class);CallSite cs = TemplateRuntime.newStringTemplate(lookup, "", mt);...int[] a = new int[1000], b = new int[1000];...StringTemplate st = (StringTemplate)cs.getTarget().invokeExact(        new String[] { "", " - ", "\n", " - ", "\n", ... " - ", "\n" },        new Object[] { a[0], b[0], a[1], b[1], ..., a[999], b[999]}        );
Since:
21