1 /*
2  * Copyright (C) 2018 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 dalvik.annotation.codegen;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 
23 import java.lang.annotation.ElementType;
24 import java.lang.annotation.Repeatable;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.lang.annotation.Target;
28 
29 /**
30  * Indicates to the platform toolchain that there is an upcoming public SDK API change for a method.
31  * The API change will add a more specific return type in a subclass for an overridden method.
32  *
33  * <p>When this annotation is present, the toolchain is required to generate on-device bytecode for
34  * an overloaded implementation of the method which is marked as synthetic, returns the more
35  * specific type given in {@link CovariantReturnType#returnType()}, and which delegates to the
36  * method being annotated.
37  *
38  * <p>At some future point in time the public Android API can change so that the public API method
39  * returns the new type. Android platform developers can be sure that all releases after the API
40  * version specified in {@link CovariantReturnType#presentAfter()} have the new method signature on
41  * device and will therefore be compatible with code compiled against the newest public Android API
42  * stubs.
43  *
44  * <p>Once the actual method signature is updated to the new form and this annotation is removed
45  * there will be a synthetic overload for the method with the more general type provided (as normal)
46  * by the Java compiler ensuring compatibility with code compiled against the old stubs.
47  *
48  * <p>When adding this annotation to platform classes the developer should also add CTS tests to
49  * ensure the expected methods are present on all Android devices.
50  *
51  * <p>Notes on scope and limitations:
52  * <ul>
53  * <li>This annotation must have no effect on generated API stubs.</li>
54  * <li>This annotation does not allow the declared exceptions to be made more specific for the
55  * generated synthetic method. This could be added later.</li>
56  * <li>Any type parameters on the annotated method need not be copied.</li>
57  * <li>This annotation is <em>not</em> expected to be treated by the toolchain as inherited. All
58  * layers of platform class hierarchy must specify {@link CovariantReturnType} for all the overloads
59  * that have to be generated. The annotation is marked as repeatable for this reason.</li>
60  * </ul>
61  *
62  * @hide
63  */
64 @Repeatable(CovariantReturnType.CovariantReturnTypes.class)
65 @Retention(RetentionPolicy.CLASS)
66 @Target({ ElementType.METHOD})
67 @SystemApi(client = MODULE_LIBRARIES)
68 public @interface CovariantReturnType {
69 
70     /**
71      * The return type of the synthetic method to generate. Must be a subclass of the return type
72      * of the method being annotated.
73      *
74      * @hide
75      */
76     @SystemApi(client = MODULE_LIBRARIES)
returnType()77     Class<?> returnType();
78 
79     /**
80      * The last Android API level not to have the generated synthetic method. The annotation can be
81      * removed and the actual return type updated when support for this API level is dropped.
82      *
83      * @hide
84      */
85     @SystemApi(client = MODULE_LIBRARIES)
presentAfter()86     int presentAfter();
87 
88     /** @hide */
89     @Retention(RetentionPolicy.CLASS)
90     @Target({ElementType.METHOD})
91     @SystemApi(client = MODULE_LIBRARIES)
92     @interface CovariantReturnTypes {
93         /** @hide */
94         @SystemApi(client = MODULE_LIBRARIES)
value()95         CovariantReturnType[] value();
96     }
97 }
98