Module java.base

Class TemplateRuntime

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

public final class TemplateRuntime extends Object
TemplateRuntime is a preview API of the Java platform.
Programs can only use TemplateRuntime 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 create StringTemplatePREVIEW 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 the newStringTemplate(java.lang.invoke.MethodHandles.Lookup, java.lang.String, java.lang.invoke.MethodType, java.lang.String...) bootstrap method to construct a CallSite that accepts two integers and produces a new StringTemplatePREVIEW 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 than StringConcatFactory.MAX_INDY_CONCAT_ARG_SLOTSPREVIEW value slots, then the java compiler will use the newLargeStringTemplate(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 the newLargeStringTemplate(java.lang.invoke.MethodHandles.Lookup, java.lang.String, java.lang.invoke.MethodType) bootstrap method to construct a CallSite that accepts an array of integers and produces a new StringTemplatePREVIEW 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