1 Debian patch touch this file
-
Fix ivy dependencies.
- Author
- Bas Couwenberg <[email protected]>
- Bug
- https://josm.openstreetmap.de/ticket/24839
- Forwarded
- not-needed
| •1 | /* |
| 2 | * Copyright 2023 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.ElementType.TYPE; |
| 20 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 21 | |
| 22 | import java.lang.annotation.Documented; |
| 23 | import java.lang.annotation.Inherited; |
| 24 | import java.lang.annotation.Retention; |
| 25 | import java.lang.annotation.Target; |
| 26 | |
| 27 | /** |
| 28 | * This annotation indicates that the class/interface it is applied to is thread safe |
| 29 | * |
| 30 | * <p>An object is thread safe if no sequences of accesses (like reads and writes to public fields |
| 31 | * or calls to public methods) may put the object into an invalid state, or cause it to violate its |
| 32 | * contract, regardless of the interleaving of those actions at runtime. |
| 33 | * |
| 34 | * <p>This annotation has two related-but-distinct purposes: |
| 35 | * |
| 36 | * <ul> |
| 37 | * <li>For humans: it indicates that the class/interface (and subclasses) is thread-safe |
| 38 | * <li>For machines: it causes the annotated class/interface -- and all of its subtypes -- to be |
| 39 | * validated by the {@code com.google.errorprone.bugpatterns.threadsafety.ThreadSafeChecker} |
| 40 | * {@code BugChecker}. |
| 41 | * </ul> |
| 42 | * |
| 43 | * Note that passing the checks performed by the {@code ThreadSafeChecker} is neither necessary nor |
| 44 | * sufficient to guarantee the thread safety of a class. In fact, it is not possible to determine |
| 45 | * thread safety through static code analysis alone, and the goal of {@code ThreadSafeChecker} is to |
| 46 | * steer the code towards using standard thread-safe patterns, and then to assist the developer in |
| 47 | * avoiding common mistakes. It is not meant as a substitute for diligent code review by a |
| 48 | * knowledgeable developer. |
| 49 | * |
| 50 | * <p>Also note that the only easy way to guarantee thread-safety of a class is to make it |
| 51 | * immutable, and you should do that whenever possible (or at the least, make as much of the class |
| 52 | * be immutable). Otherwise, writing a thread-safe class is inherently tricky and error prone, and |
| 53 | * keeping it thread-safe is even more so. |
| 54 | * |
| 55 | * <p>The remainder of this javadoc describes the heuristics enforced by {@code ThreadSafeChecker} |
| 56 | * and the related {@code com.google.errorprone.bugpatterns.threadsafety.GuardedByChecker} and |
| 57 | * {@code com.google.errorprone.bugpatterns.threadsafety.ImmutableChecker} on which the former |
| 58 | * relies. |
| 59 | * |
| 60 | * <p>The {@code ThreadSafeChecker} heuristics enforce that every field meets at least one of these |
| 61 | * requirements: |
| 62 | * |
| 63 | * <ul> |
| 64 | * <li>It is both {@code final} and its type is deemed inherently deeply thread-safe; and/or |
| 65 | * <li>it is annotated with either {@link com.google.errorprone.annotations.concurrent.GuardedBy} |
| 66 | * (some other annotations named {@code GuardedBy} also work, though this the preferred); |
| 67 | * </ul> |
| 68 | * |
| 69 | * Below, more details about what is meant by "deemed inherently deeply thread-safe" are presented, |
| 70 | * and, afterwards, more about {@code GuardedBy}. |
| 71 | * |
| 72 | * <p>A type is deemed inherently deeply thread-safe if it meets two requirements. The first |
| 73 | * requirement is that it meets at least one of these four conditions: |
| 74 | * |
| 75 | * <ul> |
| 76 | * <li>it is listed as a well-known immutable type in {@code |
| 77 | * com.google.errorprone.bugpatterns.threadsafety.WellKnownMutability} (e.g. a field of type |
| 78 | * {@link String}); and/or |
| 79 | * <li>it is listed as a well-known thread-safe type in {@code |
| 80 | * com.google.errorprone.bugpatterns.threadsafety.WellKnownThreadSafety} (e.g. a field of type |
| 81 | * {@link java.util.concurrent.atomic.AtomicBoolean}); and/or |
| 82 | * <li>it is annotated with {@link Immutable}; and/or |
| 83 | * <li>it is annotated with {@link ThreadSafe}. |
| 84 | * </ul> |
| 85 | * |
| 86 | * <p>This first requirement means the type is at least inherently shallowly thread-safe. |
| 87 | * |
| 88 | * <p>Fields annotated with {@code javax.annotation.concurrent.GuardedBy} are likely the meat of a |
| 89 | * mutable thread-safe class: these are things that need to be mutated, but should be done so in a |
| 90 | * safe manner -- i.e., (most likely) in critical sections of code that protect their access by |
| 91 | * means of a lock. See more information in that annotation's javadoc. |
| 92 | * |
| 93 | * <p>As stated before, the heuristics above are not sufficient to guarantee the thread safety of a |
| 94 | * class. Also as stated before, thread-safety is tricky, and requires diligent analysis by skilled |
| 95 | * people. That said, we provide here a few examples of common examples of ways to break these |
| 96 | * heuristics, so as to help you avoid them: |
| 97 | * |
| 98 | * <ul> |
| 99 | * <li>a non-private {@code @GuardedBy} field -- i.e.if a non-private {@code @GuardedBy} field is |
| 100 | * accessed outside the class, the code that enforces {@code @GuardedBy} will not prevent |
| 101 | * unprotected access and/or modifications to the field; |
| 102 | * <li>indirect access to the field. There are several ways in which code may access the objects |
| 103 | * stored in the field indirectly (i.e. not directly referencing the field). In all these |
| 104 | * cases, {@code @GuardedBy} offers no enforcement. Here's some examples: |
| 105 | * <ul> |
| 106 | * <li>if the {@code @GuardedBy} field instance is part of an object by a method (e.g. a |
| 107 | * simple getter method or constructor parameter); |
| 108 | * <li>if a method takes an out-parameter and the method calls a method in that |
| 109 | * out-parameter passing the instance of the {@code @GuardedBy} field, and that instance |
| 110 | * is stored in the out-parameter (i.e. a simple setter method); |
| 111 | * </ul> |
| 112 | * <li>methods that perform multiple operations -- e.g., if a class {@code Foo} contains a {@code |
| 113 | * AtomicInteger} (which is an inherently deeply thread-safe data structure), the following |
| 114 | * code makes this class not thread-safe: |
| 115 | * <pre>{@code |
| 116 | * private void incrementMyAtomicInteger() { |
| 117 | * myAtomicInteger.set(myAtomicInteger.get() + 1); |
| 118 | * } |
| 119 | * |
| 120 | * }</pre> |
| 121 | * </ul> |
| 122 | * |
| 123 | * Also see https://errorprone.info/bugpattern/ThreadSafe |
| 124 | */ |
| 125 | // TODO(b/112275411): when fixed, delete the comment above about non-private fields |
| 126 | @Target({TYPE}) |
| 127 | @Retention(RUNTIME) |
| 128 | // Note: besides abiding by the standard behavior of `@Inherited`, the behavior enforced by |
| 129 | // the static analysis effectively applies not only to classes that extend a class annotated with |
| 130 | // @ThreadSafe but *also* applies to classes that implement an annotated interface. This is because |
| 131 | // a class that implements an interface annotated with `@ThreadSafe` either: a. is anonymous, in |
| 132 | // which case the static analysis is implemented to run against it; or b. it is not anonymous, in |
| 133 | // which case the static analysis enforces that the class _also_ have an `@ThreadSafe` annotation. |
| 134 | @Inherited |
| 135 | @Documented |
| 136 | public @interface ThreadSafe {} |