How to get string.format to complain at compile time
Asked Answered
A

2

8

The compiler has access to the format string AND the required types and parameters. So I assume there would be some way to indicate missing parameters for the varargs ... even if only for a subset of cases. Is there someway for eclipse or another ide to indicate that the varargs passed might cause a problem at runtime ?

Alkali answered 17/1, 2011 at 21:38 Comment(2)
Nice idea for an eclipse enhancement, btw. It could check at least for static format strings and fixed size varargs.Marcel
Check out my answer here - #24769955 - it still is a slight work in progressWalkway
M
10

It looks as if FindBugs can solve your problem. There are some warning categories related to format strings.

Meredith answered 17/1, 2011 at 22:16 Comment(1)
For anyone else wondering where this feature went, it was dropped in version 3.1.7 of SpotBugs due to OSS licensing issues.Tedmann
V
1

The Java compiler doesn't have any built-in semantic knowledge of StringFormat parameters, so it can't check on these at compile time. For all it knows, StringFormat is just another class and String.format is just another method, and the given format string is just another string like any other.

But yeah, I feel your pain, having come across these same problems in the past couple days. What they ought to have done is make it 'less careful' about the number of parameters, and just leave trailing %s markers un-replaced.

Vena answered 17/1, 2011 at 21:46 Comment(1)
perhaps the compiler cant say...true , but maybe there are some static analysis tools out there that do that ?Alkali

© 2022 - 2024 — McMap. All rights reserved.