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 nonNullSource: TestFile =
67         TestFiles.java(
68             """
69     package android.annotation;
70     import java.lang.annotation.*;
71     import static java.lang.annotation.ElementType.*;
72     import static java.lang.annotation.RetentionPolicy.CLASS;
73     /**
74      * Denotes that a parameter, field or method return value can never be null.
75      * @paramDoc This value must never be {@code null}.
76      * @returnDoc This value will never be {@code null}.
77      * @hide
78      */
79     @SuppressWarnings({"WeakerAccess", "JavaDoc"})
80     @Retention(CLASS)
81     @Target({METHOD, PARAMETER, FIELD, TYPE_USE})
82     public @interface NonNull {
83     }
84     """
85         )
86 
87     val nullableSource: TestFile =
88         TestFiles.java(
89             """
90     package android.annotation;
91     import java.lang.annotation.*;
92     import static java.lang.annotation.ElementType.*;
93     import static java.lang.annotation.RetentionPolicy.CLASS;
94     /**
95      * Denotes that a parameter, field or method return value can be null.
96      * @paramDoc This value may be {@code null}.
97      * @returnDoc This value may be {@code null}.
98      * @hide
99      */
100     @SuppressWarnings({"WeakerAccess", "JavaDoc"})
101     @Retention(CLASS)
102     @Target({METHOD, PARAMETER, FIELD, TYPE_USE})
103     public @interface Nullable {
104     }
105     """
106         )
107 
108     val libcoreNonNullSource: TestFile =
109         TestFiles.java(
110             """
111     package libcore.util;
112     import static java.lang.annotation.ElementType.*;
113     import static java.lang.annotation.RetentionPolicy.SOURCE;
114     import java.lang.annotation.*;
115     @Documented
116     @Retention(SOURCE)
117     @Target({TYPE_USE})
118     public @interface NonNull {
119     }
120     """
121         )
122 
123     val libcoreNullableSource: TestFile =
124         TestFiles.java(
125             """
126     package libcore.util;
127     import static java.lang.annotation.ElementType.*;
128     import static java.lang.annotation.RetentionPolicy.SOURCE;
129     import java.lang.annotation.*;
130     @Documented
131     @Retention(SOURCE)
132     @Target({TYPE_USE})
133     public @interface Nullable {
134     }
135     """
136         )
137 
138     /**
139      * The version of the Jetbrains nullness annotations used by metalava is not type-use, but the
140      * latest version is.
141      */
142     val jetbrainsNullableTypeUseSource: TestFile =
143         TestFiles.java(
144             """
145     package org.jetbrains.annotations;
146     @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 })
147     public @interface Nullable {}
148             """
149         )
150 
151     /** TYPE_USE version of [com.android.tools.metalava.intRangeAnnotationSource] */
152     val intRangeTypeUseSource =
153         java(
154             """
155         package androidx.annotation;
156         import java.lang.annotation.*;
157         import static java.lang.annotation.ElementType.*;
158         import static java.lang.annotation.RetentionPolicy.SOURCE;
159         @Retention(SOURCE)
160         @Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE,TYPE_USE})
161         public @interface IntRange {
162             long from() default Long.MIN_VALUE;
163             long to() default Long.MAX_VALUE;
164         }
165         """
166         )
167 
168     val supportParameterName =
169         java(
170             """
171                 package androidx.annotation;
172                 import java.lang.annotation.*;
173                 import static java.lang.annotation.ElementType.*;
174                 import static java.lang.annotation.RetentionPolicy.SOURCE;
175                 @SuppressWarnings("WeakerAccess")
176                 @Retention(SOURCE)
177                 @Target({METHOD, PARAMETER, FIELD})
178                 public @interface ParameterName {
179                     String value();
180                 }
181             """
182         )
183 }
184