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