Debian Codegraph @

src/com/google/errorprone/annotations/FormatMethod.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * Copyright 2016 The Error Prone Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.errorprone.annotations;
18
19 import static java.lang.annotation.RetentionPolicy.CLASS;
20
21 import java.lang.annotation.Documented;
22 import java.lang.annotation.ElementType;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.Target;
25
26 /**
27 * Annotation for a method that takes a printf-style format string as an argument followed by
28 * arguments for that format string.
29 *
30 * <p>This annotation is used in conjunction with {@link FormatString} to denote a method that takes
31 * a printf-style format string and its format arguments. In any method annotated as {@code
32 * FormatMethod} without a {@link FormatString} parameter, the first {@code String} parameter is
33 * assumed to be the format string. For example, the following two methods are equivalent:
34 *
35 * <pre>
36 * &#064;FormatMethod void log(Locale l, &#064;FormatString String logMessage, Object... args) {}
37 * &#064;FormatMethod void log(Locale l, String logMessage, Object... args) {}
38 * </pre>
39 *
40 * <p>Using {@link FormatMethod} on a method header will ensure the following for the parameters
41 * passed to the method:
42 *
43 * <ol>
44 * <li>A format string is either:
45 * <ul>
46 * <li>A compile time constant value (see {@link CompileTimeConstant} for more info).
47 * <p>The following example is valid:
48 * <pre>
49 * public class Foo {
50 * static final String staticFinalLogMessage = "foo";
51 * &#064;FormatMethod void log(&#064;FormatString String format, Object... args) {}
52 * void validLogs() {
53 * log("String literal");
54 * log(staticFinalLogMessage);
55 * }
56 * }</pre>
57 * <p>However the following would be invalid:
58 * <pre>
59 * public class Foo{
60 * &#064;FormatMethod void log(&#064;FormatString String format, Object... args) {}
61 * void invalidLog(String notCompileTimeConstant) {
62 * log(notCompileTimeConstant);
63 * }
64 * }</pre>
65 * <li>An effectively final variable that was assigned to a compile time constant value.
66 * This is to permit the following common case:
67 * <pre>
68 * String format = "Some long format string: %s";
69 * log(format, arg);
70 * </pre>
71 * <li>Another {@link FormatString} annotated parameter. Ex:
72 * <pre>
73 * public class Foo {
74 * static final String staticFinalLogMessage = "foo";
75 * &#064;FormatMethod void log(&#064;FormatString String format, Object... args) {}
76 * &#064;FormatMethod void validLog(&#064;FormatString String format, Object... args) {
77 * log(format, args);
78 * }
79 * }</pre>
80 * </ul>
81 * <li>The format string will be valid for the input format arguments. In the case that the actual
82 * format string parameter has a compile time constant value, this will compare the actual
83 * format string value to the types of the passed in format arguments to ensure validity. In
84 * the case that the actual format string parameter is a parameter that was annotated {@link
85 * FormatString} itself, this will ensure that the types of the arguments passed to the callee
86 * match the types of the arguments in the caller.
87 * </ol>
88 */
89 @Documented
90 @Retention(CLASS)
91 @Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
92 public @interface FormatMethod {}