Debian Codegraph @

src/com/google/errorprone/annotations/RestrictedApi.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 java.lang.annotation.Annotation;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Target;
22
23 // TODO(b/157082874): Allow restricting entire classes.
24 /**
25 * Restrict this method to callsites with a allowlist annotation.
26 *
27 * <p>Callers that are not allowlisted will cause a configurable compiler diagnostic. Allowlisting
28 * can either allow the call outright, or make the compiler emit a warning when the API is called.
29 * Paths matching a regular expression, e.g. unit tests, can also be excluded.
30 *
31 * <p>The following example shows a hypothetical, potentially unsafe {@code Foo.bar} method. It is
32 * marked with the {@code @RestrictedApi} annotations such that callers annotated with
33 * {@code @LegacyUnsafeFooBar} raise a warning, whereas the {@code @ReviewedFooBar} annotation
34 * silently allows the call.
35 *
36 * <p>The {@code @LegacyUnsafeFooBar} annotation can be used to allow existing call sites until they
37 * are refactored, while prohibiting new call-sites. Call-sites determined to be acceptable, for
38 * example through code review, could be marked {@code @ReviewedFooBar}.
39 *
40 * <pre>{@code
41 * public @interface LegacyUnsafeFooBar{}
42 *
43 * public @interface ReviewedFooBar{
44 * public string reviewer();
45 * public string comments();
46 * }
47 *
48 * public class Foo {
49 * @RestrictedApi(
50 * explanation="You could shoot yourself in the foot with Foo.bar if you aren't careful",
51 * link="http://edsger.dijkstra/foo_bar_consider_harmful.html",
52 * allowedOnPath="testsuite/.*", // Unsafe behavior in tests is ok.
53 * allowlistAnnotations = {ReviewedFooBar.class},
54 * allowlistWithWarningAnnotations = {LegacyUnsafeFooBar.class})
55 * public void bar() {
56 * if (complicatedCondition) {
57 * shoot_your_foot();
58 * } else {
59 * solve_your_problem();
60 * }
61 * }
62 * boolean complicatedCondition = true;
63 *
64 * @ReviewedFooBar(
65 * reviewer="bangert",
66 * comments="Makes sure complicatedCondition isn't true, so bar is safe!"
67 * )
68 * public void safeBar() {
69 * if (!complicatedCondition) {
70 * bar();
71 * }
72 * }
73 *
74 * @LegacyUnsafeFooBar
75 * public void someOldCode() {
76 * // ...
77 * bar()
78 * // ...
79 * }
80 * }
81 * }</pre>
82 */
83 @Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
84 public @interface RestrictedApi {
85 /** Explanation why the API is restricted, to be inserted into the compiler output. */
86 String explanation();
87
88 /** Optional link explaining why the API is restricted. */
89 String link() default "";
90
91 /**
92 * Allow the restricted API on paths matching this regular expression.
93 *
94 * <p>Leave empty (the default) to enforce the API restrictions on all paths.
95 */
96 String allowedOnPath() default "";
97
98 /** Allow calls to the restricted API in methods or classes with this annotation. */
99 Class<? extends Annotation>[] allowlistAnnotations() default {};
100
101 /**
102 * Emit warnings, not errors, on calls to the restricted API for callers with this annotation.
103 *
104 * <p>This should only be used if callers should aggressively move away from this API (or change
105 * to a allowlist annotation after review). Too many warnings will lead to ALL warnings being
106 * ignored, so tread very carefully.
107 */
108 Class<? extends Annotation>[] allowlistWithWarningAnnotations() default {};
109 }