RFR: 7903727: Remove the reliance on String Templates feature
Maurizio Cimadamore
mcimadamore at openjdk.org
Mon May 20 10:13:13 UTC 2024
On Mon, 20 May 2024 10:02:09 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> This PR aims to replace the usage of string templates with `String::format`, since there will be no string template feature in JDK 23.
>> I tried to keep similar indentation and convert them in-place, to make reviewing the changes easier.
>
> src/main/java/org/openjdk/jextract/impl/HeaderFileBuilder.java line 410:
>
>> 408: return %s.SEGMENT.get(%s.LAYOUT, 0L);
>> 409: }
>> 410: """, type.getSimpleName(), javaName, holderClass, holderClass));
>
> In these cases, an ad-hoc formatter might work as well. Similar to `MessageFormat` which allows the user to pass arguments with indices like `{0}` and `{1}`. Example:
>
>
> jshell> MessageFormat.format("{0} {0} {1}", "Hello", "World!");
> $7 ==> "Hello Hello World!"
This might also help improving readability for messages containing lots of `%s` (as at least you can look at the number being referenced, and then count the arguments passed to the `format` method to find the one you want. Also, this allows to refactor code more robustly - e.g. you can pass _new_ trailing parameters to `format`, and maybe have them used _before_ the others:
jshell> MessageFormat.format("{0} {2} {0} {1}", "Hello", "World!", "Maurizio");
$7 ==> "Hello Maurizio Hello World!"
E.g. thanks to explicit numbers, the formatting is not tied to the order in which the arguments are passed to the `format` method.
-------------
PR Review Comment: https://git.openjdk.org/jextract/pull/244#discussion_r1606545826
More information about the jextract-dev
mailing list