1 /*
2  * Copyright 2021 The Android Open Source Project
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 androidx.compose.runtime
18 
19 /**
20  * The [Composable] function is declared to expect an applier with the name [applier]. The [applier]
21  * name can be an arbitrary string but is expected to be a fully qualified name of a class that is
22  * annotated by [ComposableTargetMarker] containing a descriptive name to be used in diagnostic
23  * messages.
24  *
25  * The [applier] name is used in diagnostic messages but, if it refers to a marked annotation,
26  * [ComposableTargetMarker.description] is used instead of the class name.
27  *
28  * The Compose compiler plugin can, in most cases, infer this or an equivalent
29  * [ComposableInferredTarget], for composable functions. For example, if a composable function calls
30  * another composable function then both must be of the same group of composable functions (that is,
31  * have declared or inferred the same [applier] value). This means that, if the function called is
32  * already determined to be in a group, them the function that calls it must also be in the same
33  * group. If two functions are called of different groups then the Compose compiler plugin will
34  * generate an diagnostic message describing which group was received and which group was expected.
35  *
36  * The grouping of composable functions corresponds to the instance of [Applier] that is required to
37  * be used by the [Composer] to apply changes to the composition. The [Applier] is checked at
38  * runtime to ensure the [Applier] that is expected by a composable function is the one supplied at
39  * runtime. This annotation, and the corresponding validation performed by the Compose compiler
40  * plugin, can detect mismatches at compile time, and issue a diagnostic message when calling a
41  * [Composable] function will result in the [Applier] check failing.
42  *
43  * In most cases this annotation can be inferred. However, this annotation is required for
44  * [Composable] functions that call [ComposeNode] directly, for abstract methods, such as interfaces
45  * functions (which do not contain a body from which the plugin can infer the annotation), when
46  * using a composable lambda in sub-composition, or when a composable lambda is stored in a class
47  * field or global variable.
48  *
49  * Leaving the annotation off in such cases will result in the compiler ignoring the function and it
50  * will not emit the diagnostic when the function is called incorrectly.
51  *
52  * @param applier The applier name used during composable call checking. This is usually inferred by
53  *   the compiler. This can be an arbitrary string value but is expected to be a fully qualified
54  *   name of a class that is marked with [ComposableTargetMarker].
55  */
56 @Retention(AnnotationRetention.BINARY)
57 @Target(
58     AnnotationTarget.FILE,
59     AnnotationTarget.CLASS,
60     AnnotationTarget.FUNCTION,
61     AnnotationTarget.PROPERTY_GETTER,
62     AnnotationTarget.TYPE,
63     AnnotationTarget.TYPE_PARAMETER,
64 )
65 annotation class ComposableTarget(val applier: String)
66