@Documented @Retention(value=CLASS) @Target(value={METHOD,CONSTRUCTOR}) public @interface FormatMethod
This annotation is used in conjunction with FormatString
to denote a method that takes
a printf-style format string and its format arguments. In any method annotated as FormatMethod
without a FormatString
parameter, the first String
parameter is
assumed to be the format string. For example, the following two methods are equivalent:
@FormatMethod void log(Locale l, @FormatString String logMessage, Object... args) {} @FormatMethod void log(Locale l, String logMessage, Object... args) {}
Using FormatMethod
on a method header will ensure the following for the parameters
passed to the method:
CompileTimeConstant
for more info).
The following example is valid:
public class Foo { static final String staticFinalLogMessage = "foo"; @FormatMethod void log(@FormatString String format, Object... args) {} void validLogs() { log("String literal"); log(staticFinalLogMessage); } }
However the following would be invalid:
public class Foo{ @FormatMethod void log(@FormatString String format, Object... args) {} void invalidLog(String notCompileTimeConstant) { log(notCompileTimeConstant); } }
String format = "Some long format string: %s"; log(format, arg);
FormatString
annotated parameter. Ex:
public class Foo { static final String staticFinalLogMessage = "foo"; @FormatMethod void log(@FormatString String format, Object... args) {} @FormatMethod void validLog(@FormatString String format, Object... args) { log(format, args); } }
FormatString
itself, this will ensure that the types of the arguments passed to the callee
match the types of the arguments in the caller.
Copyright © 2021. All rights reserved.