JDK-8325948 : StringTemplate - FormatProcessor behavior is inconsistent for invalid format specifiers
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 21,22,23
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • Submitted: 2024-02-15
  • Updated: 2024-03-18
  • Resolved: 2024-03-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23Resolved
Related Reports
Duplicate :  
Relates :  
Description
Consider the following code:

void main(String[] args) {
        System.out.println(java.util.FormatProcessor.FMT.process(StringTemplate.RAW."%8\{args.length}"));
        System.out.println(java.util.FormatProcessor.FMT."%8\{args.length}");
}

Instead of %8s, it uses %8 only. However, running the code results in the following output:

       %s
Exception in thread "main" java.lang.BootstrapMethodError: bootstrap method initialization exception
        at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:188)
        at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:316)
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:275)
        at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:265)
        at Specialized.main(Specialized.java:3)
Caused by: java.lang.IllegalArgumentException: fragments size not equal ptypes size plus one
        at java.base/java.lang.invoke.StringConcatFactory.makeConcatWithTemplate(StringConcatFactory.java:1080)
        at java.base/java.util.FormatterBuilder.build(FormatterBuilder.java:480)
        at java.base/java.util.FormatProcessor.linkage(FormatProcessor.java:204)
        at java.base/java.lang.runtime.TemplateRuntime.processStringTemplate(TemplateRuntime.java:229)
        at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:167)
        ... 4 more

The reason is that the behavior of the specialized MethodHandle differds from the behavior of the process method. I'm not sure what the expected behavior should be, but from reading the javadoc, I think both variants are incorrect.
Comments
Fixed in clean up of FormatterBuilder in JDK-8324634
13-03-2024