Is there an inspection in IntelliJ which will detect when I have written
final String[] args = new String[]{"a", "b"};
instead of the shorter (and equally valid)
final String[] args = {"a", "b"};
I can't find one and I would like a weak warning when I use the more verbose form.
CodePudding user response:
Yes, there is:
Java | Code style issues | Redundant 'new' expression in constant array creation
CodePudding user response:
There actually is an inspection that suggest an opposite:
Java | Code style issues | Array creation without 'new' expression: Reports array initializers without new array expressions and suggests adding them.
The one that you are asking for doesn't exist, probably because it has limitations - such arrays can only be assigned to variables, but, for example, can not be passed to a method as an argument.
Upd. But you can always write one yourself. You can import the one I written for you:
<replaceConfiguration name="test" text="$ResourceType$[] $resource$ = new $ResourceType$[]{$Parameters$}; " recursive="false" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="$ResourceType$[] $resource$ = {$Parameters$}; ">
<constraint name="__context__" within="" contains="" />
<constraint name="ResourceType" within="" contains="" />
<constraint name="resource" within="" contains="" />
<constraint name="Parameters" minCount="0" maxCount="2147483647" within="" contains="" />
</replaceConfiguration>
This screenshot explains the logic of replace template:
The logic is very basic, the only tricky part is to add count filter over $Parameters$
so that it works for any amount of arguments.