Shorn
Shorn

Reputation: 21476

Is there a way to apply IDEA "malformed format string" intention to custom methods?

When writing code of the form:

String.format("blah %s %s", value1);

IntelliJ will warn that there is a mismatch between the number of format patterns and actual arguments: too few arguments for format string (found:1, expected: 2).

Is there any way to apply this logic to custom methods? For example:

/** @see String#format(String, Object...) */
public String myMethod(String msg, Object... args){
  return String.format("blah " + msg, args);
}
...

myMethod("%s %s", value1);

The point being that I'd like IDEA to tell me I messed up.

As shown in the example, I already document these methods that delegate to String.format().

Ideally, I'd prefer to avoid duplicating that documentation - though I'd be willing to transform it into a custom format, or possibly an annotation (preferably not an IDEA annotation though).

Upvotes: 4

Views: 2441

Answers (2)

Mike Nakis
Mike Nakis

Reputation: 62005

Stephen Friedrich's answer points in the right direction, but a screenshot is not enough to understand how to use the feature. Here is how to get it to work, in a bit more detail:

  1. In the "Additional formatter classes" box you enter any custom classes that you have created which contain a custom format() method. So, in the OP's case, if their myMethod() method belonged to some com.acme.MyClass class, then they would enter com.acme.MyClass there.

That would be all that the OP would need to do if their custom formatting method was called format(). However, their custom formatting method had a different name, so they would also need to do the second step.

  1. In the "Additional formatter methods" box you enter the names of any custom methods that you have created which work like format(). So, in the OP's case, the string myMethod would be added there. Note that you are not expected to include a class qualifier, or parentheses, or the arguments, just the name of the method.

From the looks of it, each method name entered in the "Additional formatter methods" box applies to all of the classes entered in the "Additional formatter classes" box, even if only one of the classes actually contains such a method. Furthermore, there will be no error even if none of the classes contains such a method, or if methods with such a name exist, but they do not have the necessary signature of void (String format, Object... arguments).

So, it appears that this feature has been implemented in a very hacky way, of the kind that we are not used to seeing from JetBrains. Also it is interesting to note that hackiness in the implementation of a feature makes it harder for the user to figure out how to use the feature.

I think this feature would have best been implemented with an extra annotation on the formatting method. (But let's not argue about this here, this is a Q&A site.)

Upvotes: 7

eekboom
eekboom

Reputation: 5812

You can configure additional "formatter methods" in the inspection settings:

enter image description here

If you configure the inspection profile to "Project Default", and check in the related files (in .idea folder), then everybody who checks out the project and also uses IDEA will benefit from that.

Upvotes: 3

Related Questions