RFR: JDK-8285932 Implementation of JEP 430 String Templates (Preview) [v21]

Jim Laskey jlaskey at openjdk.org
Thu Nov 17 17:09:52 UTC 2022

On Wed, 16 Nov 2022 21:19:21 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/template/StringTemplateImplFactory.java line 202:
>>> 200:     @SuppressWarnings({"unchecked", "varargs"})
>>> 201:     private static <E> List<E> toList(E... elements) {
>>> 202:         return Collections.unmodifiableList(Arrays.asList(elements));
>> Is a defensive copy needed here?
>> The caller of `newStringTemplate` could retain a reference to the Object[] array and modify it later.
> Alternatively, `SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArrayNullsAllowed(elements)` (or `elements.clone()`) could be used here, to reduce amount of wrappers.

The `newStringTemplate` methods are internal and elements is safe. Will get rid of the generics and varargs.  

@amaembo I was using listFromTrustedArrayNullsAllowed early on but ran into an edge case (crash). Will try again.


PR: https://git.openjdk.org/jdk/pull/10889

More information about the compiler-dev mailing list