1 /*
2  * Copyright 2025 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.annotation.keep
18 
19 import kotlin.reflect.KClass
20 
21 /**
22  * Generate a conditional keep rule for code that indirectly accesses a method.
23  *
24  * This annotation should be used in code which uses reflection (or a similar indirect means, such
25  * as JNI) to call a method.
26  *
27  * The generated keep rule will be conditional, which indicates to optimizers / shrinkers that it
28  * should only keep the target method in the final application if the annotated code is reachable in
29  * the final application.
30  *
31  * @see GenerateKeepForConstructor
32  * @see GenerateKeepForField
33  */
34 @Retention(AnnotationRetention.BINARY)
35 @Repeatable
36 @Target(
37     AnnotationTarget.CLASS,
38     AnnotationTarget.FIELD,
39     AnnotationTarget.FUNCTION,
40     AnnotationTarget.CONSTRUCTOR,
41 )
42 public annotation class GenerateKeepForMethod(
43     /**
44      * Class containing the field accessed by reflection.
45      *
46      * Mutually exclusive with [className].
47      */
48     val classConstant: KClass<*> = Unspecified::class,
49 
50     /**
51      * Class name (or pattern) containing the field accessed by reflection.
52      *
53      * Mutually exclusive with [classConstant].
54      */
55     val className: String = "",
56 
57     /** Method name (or pattern) accessed by reflection. */
58     val methodName: String,
59 
60     /**
61      * Defines which method to keep by specifying set of parameter classes passed.
62      *
63      * Defaults to `[ Unspecified::class ]`, which will keep all methods.
64      *
65      * Mutually exclusive with [paramClassNames].
66      */
67     val params: Array<KClass<*>> = [Unspecified::class],
68 
69     /**
70      * Defines which method to keep by specifying set of parameter classes passed.
71      *
72      * Defaults to `[""]`, which will keep all methods.
73      *
74      * Mutually exclusive with [params].
75      */
76     val paramClassNames: Array<String> = [""],
77 
78     /**
79      * Class of field accessed by reflection.
80      *
81      * Ignored if not specified.
82      *
83      * Mutually exclusive with [returnClassName].
84      */
85     val returnClass: KClass<*> = Unspecified::class,
86 
87     /**
88      * Class (or class pattern) of field accessed by reflection.
89      *
90      * Ignored if not specified.
91      *
92      * Mutually exclusive with [returnClass].
93      */
94     val returnClassName: String = "",
95 )
96