Background
Working on some projects with different language levels, Java 8 and Java 15+, some string operations can't be copied and pasted to reuse.
Question
How to automatically replace the 'old' String.format(string, args) with the Java 13 + string.formatted(args) and vise versa.
Example:
"%s World!".formatted("Hello")
to
String.format("%s World!", "Hello")
and vise versa.
Answer
- Navigate to Edit | Find | Replace Structurally...,
- Copy the text
For String.format(string, args) -> string.formatted(args) copy the below text:
<replaceConfiguration name="'String.format()' call can be replaced with 'formatted()' expression" text="String.format($string$, $args$)" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="$string$.formatted($args$)">
<constraint name="__context__" within="" contains="" />
<constraint name="string" within="" contains="" />
<constraint name="args" maxCount="2147483647" within="" contains="" />
</replaceConfiguration>
For string.formatted(args) -> String.format(string, args) copy the below text:
<replaceConfiguration name="'formatted()' expression can be replaced with 'String.format()' call" text="$string$.formatted($args$)" recursive="false" type="JAVA" pattern_context="default" reformatAccordingToStyle="true" shortenFQN="true" replacement="String.format($string$, $args$)">
<constraint name="__context__" within="" contains="" />
<constraint name="string" within="" contains="" />
<constraint name="args" maxCount="2147483647" within="" contains="" />
</replaceConfiguration>
- Click the button Insert Template from Clipboard(cmd/ctrl+V), then click the Find button at the bottom right of the dialog to test it.
- You can click the save button
to save as an inspection or as a template to project or the IDE.
Comments
0 comments
Please sign in to leave a comment.