1 /*
2  * Copyright (C) 2023 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 com.android.tools.metalava.testing
18 
19 import com.android.tools.lint.checks.infrastructure.TestFile
20 import com.android.tools.lint.checks.infrastructure.TestFiles
21 
22 object KnownSourceFiles {
23 
24     val notTypeUseNonNullSource: TestFile =
25         TestFiles.java(
26             """
27                 package not.type.use;
28                 public @interface NonNull {
29                 }
30             """
31         )
32 
33     val notTypeUseNullableSource: TestFile =
34         TestFiles.java(
35             """
36                 package not.type.use;
37                 public @interface Nullable {
38                 }
39             """
40         )
41 
42     val typeUseOnlyNonNullSource: TestFile =
43         TestFiles.java(
44             """
45                 package type.use.only;
46                 import java.lang.annotation.*;
47                 import static java.lang.annotation.ElementType.*;
48                 @Target(TYPE_USE)
49                 public @interface NonNull {
50                 }
51             """
52         )
53 
54     val typeUseOnlyNullableSource: TestFile =
55         TestFiles.java(
56             """
57                 package type.use.only;
58                 import java.lang.annotation.*;
59                 import static java.lang.annotation.ElementType.*;
60                 @Target(TYPE_USE)
61                 public @interface Nullable {
62                 }
63             """
64         )
65 
66     val androidAnnotationHide: TestFile =
67         TestFiles.java(
68             """
69                 /** @hide */
70                 package android.annotation;
71             """
72         )
73 
74     val nonNullSource: TestFile =
75         TestFiles.java(
76             """
77     package android.annotation;
78     import java.lang.annotation.*;
79     import static java.lang.annotation.ElementType.*;
80     import static java.lang.annotation.RetentionPolicy.CLASS;
81     /**
82      * Denotes that a parameter, field or method return value can never be null.
83      * @paramDoc This value must never be {@code null}.
84      * @returnDoc This value will never be {@code null}.
85      * @hide
86      */
87     @SuppressWarnings({"WeakerAccess", "JavaDoc"})
88     @Retention(CLASS)
89     @Target({METHOD, PARAMETER, FIELD, TYPE_USE})
90     public @interface NonNull {
91     }
92     """
93         )
94 
95     val nullableSource: TestFile =
96         TestFiles.java(
97             """
98     package android.annotation;
99     import java.lang.annotation.*;
100     import static java.lang.annotation.ElementType.*;
101     import static java.lang.annotation.RetentionPolicy.CLASS;
102     /**
103      * Denotes that a parameter, field or method return value can be null.
104      * @paramDoc This value may be {@code null}.
105      * @returnDoc This value may be {@code null}.
106      * @hide
107      */
108     @SuppressWarnings({"WeakerAccess", "JavaDoc"})
109     @Retention(CLASS)
110     @Target({METHOD, PARAMETER, FIELD, TYPE_USE})
111     public @interface Nullable {
112     }
113     """
114         )
115 
116     val libcodeUtilHide: TestFile =
117         TestFiles.java(
118             """
119                 /** @hide */
120                 package libcore.util;
121             """
122         )
123 
124     val libcoreNonNullSource: TestFile =
125         TestFiles.java(
126             """
127     package libcore.util;
128     import static java.lang.annotation.ElementType.*;
129     import static java.lang.annotation.RetentionPolicy.SOURCE;
130     import java.lang.annotation.*;
131     @Documented
132     @Retention(SOURCE)
133     @Target({TYPE_USE})
134     public @interface NonNull {
135     }
136     """
137         )
138 
139     val libcoreNullableSource: TestFile =
140         TestFiles.java(
141             """
142     package libcore.util;
143     import static java.lang.annotation.ElementType.*;
144     import static java.lang.annotation.RetentionPolicy.SOURCE;
145     import java.lang.annotation.*;
146     @Documented
147     @Retention(SOURCE)
148     @Target({TYPE_USE})
149     public @interface Nullable {
150     }
151     """
152         )
153 
154     /**
155      * The version of the Jetbrains nullness annotations used by metalava is not type-use, but the
156      * latest version is.
157      */
158     val jetbrainsNullableTypeUseSource: TestFile =
159         TestFiles.java(
160             """
161     package org.jetbrains.annotations;
162     @java.lang.annotation.Target({ java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD, java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.LOCAL_VARIABLE, java.lang.annotation.ElementType.TYPE_USE })
163     public @interface Nullable {}
164             """
165         )
166 
167     val androidxAnnotationHide: TestFile =
168         TestFiles.java(
169             """
170                 /** @hide */
171                 package androidx.annotation;
172             """
173         )
174 
175     /** TYPE_USE version of [com.android.tools.metalava.intRangeAnnotationSource] */
176     val intRangeTypeUseSource =
177         java(
178             """
179         package androidx.annotation;
180         import java.lang.annotation.*;
181         import static java.lang.annotation.ElementType.*;
182         import static java.lang.annotation.RetentionPolicy.SOURCE;
183         @Retention(SOURCE)
184         @Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE,TYPE_USE})
185         public @interface IntRange {
186             long from() default Long.MIN_VALUE;
187             long to() default Long.MAX_VALUE;
188         }
189         """
190         )
191 
192     val supportParameterName =
193         java(
194             """
195                 package androidx.annotation;
196                 import java.lang.annotation.*;
197                 import static java.lang.annotation.ElementType.*;
198                 import static java.lang.annotation.RetentionPolicy.SOURCE;
199                 @SuppressWarnings("WeakerAccess")
200                 @Retention(SOURCE)
201                 @Target({METHOD, PARAMETER, FIELD})
202                 public @interface ParameterName {
203                     String value();
204                 }
205             """
206         )
207 
208     val systemApiSource: TestFile =
209         TestFiles.java(
210             """
211                 package android.annotation;
212                 import static java.lang.annotation.ElementType.*;
213                 import java.lang.annotation.*;
214                 @Target({TYPE, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE, PACKAGE})
215                 @Retention(RetentionPolicy.SOURCE)
216                 public @interface SystemApi {
217                     enum Client {
218                         /**
219                          * Specifies that the intended clients of a SystemApi are privileged apps.
220                          * This is the default value for {@link #client}.
221                          */
222                         PRIVILEGED_APPS,
223 
224                         /**
225                          * Specifies that the intended clients of a SystemApi are used by classes in
226                          * <pre>BOOTCLASSPATH</pre> in mainline modules. Mainline modules can also expose
227                          * this type of system APIs too when they're used only by the non-updatable
228                          * platform code.
229                          */
230                         MODULE_LIBRARIES,
231 
232                         /**
233                          * Specifies that the system API is available only in the system server process.
234                          * Use this to expose APIs from code loaded by the system server process <em>but</em>
235                          * not in <pre>BOOTCLASSPATH</pre>.
236                          */
237                         SYSTEM_SERVER
238                     }
239 
240                     /**
241                      * The intended client of this SystemAPI.
242                      */
243                     Client client() default android.annotation.SystemApi.Client.PRIVILEGED_APPS;
244                 }
245             """
246         )
247 }
248