public final class TemplateRuntime extends Object
TemplateRuntime
is a preview API of the Java platform. StringTemplate
PREVIEW instances. For example, the java compiler will translate the following code; int x = 10;
int y = 20;
StringTemplate st = RAW."\{x} + \{y} = \{x + y}";
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 StringTemplate
PREVIEW 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);
StringConcatFactory.MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW 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]}
""";
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 StringTemplate
PREVIEW 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]}
);
Modifier and Type | Method | Description |
---|---|---|
static CallSite |
newLargeStringTemplate |
String template bootstrap method for creating large string templates, i.e., when the number of value slots exceeds StringConcatFactory.MAX_INDY_CONCAT_ARG_SLOTS PREVIEW. |
static CallSite |
newStringTemplate |
String template bootstrap method for creating string templates. |
static CallSite |
processStringTemplate |
String template bootstrap method for static final processors. |
public static CallSite newStringTemplate(MethodHandles.Lookup lookup, String name, MethodType type, String... fragments) throws Throwable
lookup
- method lookup from call sitename
- method name - not usedtype
- method type (ptypes...) -> StringTemplatefragments
- fragment array for string templateCallSite
to handle create string templateNullPointerException
- if any of the arguments is nullThrowable
- if linkage failspublic static CallSite newLargeStringTemplate(MethodHandles.Lookup lookup, String name, MethodType type) throws Throwable
StringConcatFactory.MAX_INDY_CONCAT_ARG_SLOTS
PREVIEW. The non-static arguments are the fragments array and values array.lookup
- method lookup from call sitename
- method name - not usedtype
- method type (String[], Object[]) -> StringTemplateCallSite
to handle create large string templateNullPointerException
- if any of the arguments is nullThrowable
- if linkage failspublic static CallSite processStringTemplate(MethodHandles.Lookup lookup, String name, MethodType type, MethodHandle processorGetter, String... fragments) throws Throwable
MethodHandle
to retrieve the value of the static final processor. The non-static arguments are the values.lookup
- method lookup from call sitename
- method name - not usedtype
- method type (ptypes...) -> ObjectprocessorGetter
- MethodHandle
to get static final processorfragments
- fragments from string templateCallSite
to handle string template processingNullPointerException
- if any of the arguments is nullThrowable
- if linkage fails
© 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/runtime/TemplateRuntime.html
TemplateRuntime
when preview features are enabled.