Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * Copyright 2015 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 method parameter and class field declarations, which denotes that corresponding
28 * actual values must be compile-time constant expressions.
29 *
30 * <p>When the formal parameter of a method or constructor is annotated with the {@link
31 * CompileTimeConstant} type annotation, the corresponding actual parameter must be an expression
32 * that satisfies one of the following conditions:
33 *
34 * <ol>
35 * <li>The expression is one for which the Java compiler can determine a constant value at compile
36 * time, or
37 * <li>the expression consists of the literal {@code null}, or
38 * <li>the expression consists of a single identifier, where the identifier is a formal method
39 * parameter or class field that is declared {@code final} and has the {@link
40 * CompileTimeConstant} annotation, or
41 * <li>the expression is a {@link String}, and formed from the concatenation of symbols which meet
42 * these conditions, or
43 * <li>the expression is a ternary condition, where both branches satisfy these conditions, or
44 * <li>the expression is an immutable collection with all values known to satisfy these conditions
45 * (for example, {@code ImmutableSet.of("a", "b", "c")}).
46 * </ol>
47 *
48 * <p>For example, the following code snippet is legal:
49 *
50 * <pre>{@code
51 * public class C {
52 * private static final String S = "Hello";
53 * void m(@CompileTimeConstant final String s) {}
54 * void n(@CompileTimeConstant final String t) {
55 * m(S + " World!");
56 * m(null);
57 * m(t);
58 * }
59 * void o(boolean enabled) {
60 * m(enabled ? "on" : "off");
61 * }
62 * }
63 * }</pre>
64 *
65 * <p>In contrast, the following is illegal:
66 *
67 * <pre>{@code
68 * public class C {
69 * void m(@CompileTimeConstant final String s) {}
70 * void n(String t) {
71 * m(t);
72 * }
73 * }
74 * }</pre>
75 *
76 * <p>When a class field is annotated with the {@link CompileTimeConstant} type annotation, the
77 * field must also be declared to be {@code final}, and the corresponding initialised value must be
78 * an expression that satisfies the conditions above.
79 *
80 * <p>For example, the following code snippet is legal:
81 *
82 * <pre>{@code
83 * public class C {
84 * @CompileTimeConstant final String s;
85 * public C(@CompileTimeConstant String s) {
86 * this.s = s;
87 * }
88 * void m(@CompileTimeConstant final String s) {}
89 * void n() {
90 * m(s);
91 * }
92 * }
93 * }</pre>
94 *
95 * <p>In contrast, the following are illegal:
96 *
97 * <pre>{@code
98 * public class C {
99 * @CompileTimeConstant String S;
100 * public C(@CompileTimeConstant String s) {
101 * this.S = s;
102 * }
103 * void m(@CompileTimeConstant final String s) { }
104 * void n() {
105 * m(S);
106 * }
107 * }
108 * }</pre>
109 *
110 * <pre>{@code
111 * public class C {
112 * @CompileTimeConstant final String S;
113 * public C(String s) {
114 * this.S = s;
115 * }
116 * }
117 * }</pre>
118 *
119 * <p>Compile-time constant values are implicitly under the control of the trust domain of the
120 * application whose source code they are part of. Hence, this annotation is useful to constrain the
121 * use of APIs that may only be safely called with values that are under application control.
122 *
123 * <p>The current implementation of the @CompileTimeConstant checker cannot reason about more
124 * complex scenarios, for example, returning compile-time-constant values from a method, or storing
125 * compile-time-constant values in a collection. APIs will typically accommodate such use cases via
126 * domain-specific types that capture domain-specific aspects of trustworthiness that arise from
127 * values being under application control.
128 *
129 * <p>These constraints are enforced by <a href="https://errorprone.info">error-prone</a>.
130 */
131 @Documented
132 @Retention(CLASS)
133 @Target({ElementType.PARAMETER, ElementType.FIELD})
134 public @interface CompileTimeConstant {}