xref: /aosp_15_r20/tools/metalava/metalava/src/test/java/com/android/tools/metalava/PolymorphicMethodsTest.kt (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
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
18 
19 import com.android.tools.metalava.model.text.FileFormat
20 import com.android.tools.metalava.testing.java
21 import org.junit.Test
22 
23 class PolymorphicMethodsTest : DriverTest() {
24 
25     @Test
Test MethodHandlenull26     fun `Test MethodHandle`() {
27         check(
28             format = FileFormat.V2,
29             sourceFiles =
30                 arrayOf(
31                     java(
32                         """
33                             package java.lang.invoke;
34 
35                             public abstract class MethodHandle {
36                                 public final native Object invokeNative(Object... args);
37 
38                                 // The following should not end up with a `native` modifier as it is
39                                 // not native.
40                                 public final Object invokeNotNative(Object... args);
41 
42                                 // The following should not end up with a `native` modifier as it
43                                 // does not take a single variable arity parameter it takes two
44                                 // parameters.
45                                 public final native Object invokeNotSingleParameter(String name, Object[] args);
46 
47                                 // The following should not end up with a `native` modifier as it
48                                 // does not take a single variable arity parameter it does take one
49                                 // parameter, but it's not a variable arity parameter.
50                                 public final native Object invokeNotVarArgs(Object[] args);
51 
52                                 // The following should not end up with a `native` modifier as while
53                                 // it erases to `Object[]` that is not its declared type.
54                                 @SafeVarargs
55                                 public final native <T> Object invokeNotDeclaredObjectVarArgsTypes(T... args);
56                             }
57                         """
58                             .trimIndent()
59                     ),
60                     java(
61                         """
62                             package java.lang.invoke;
63 
64                             public abstract class NotMethodHandle {
65                                 // The following should not end up with a `native` modifier as it's
66                                 // in the wrong class.
67                                 public final native Object invokeNative(Object... args);
68                             }
69                         """
70                             .trimIndent()
71                     ),
72                 ),
73             api =
74                 """
75                     // Signature format: 2.0
76                     package java.lang.invoke {
77                       public abstract class MethodHandle {
78                         ctor public MethodHandle();
79                         method public final native Object invokeNative(java.lang.Object...);
80                         method @java.lang.SafeVarargs public final <T> Object invokeNotDeclaredObjectVarArgsTypes(T...);
81                         method public final Object invokeNotNative(java.lang.Object...);
82                         method public final Object invokeNotSingleParameter(String, Object[]);
83                         method public final Object invokeNotVarArgs(Object[]);
84                       }
85                       public abstract class NotMethodHandle {
86                         ctor public NotMethodHandle();
87                         method public final Object invokeNative(java.lang.Object...);
88                       }
89                     }
90                 """
91                     .trimIndent(),
92         )
93     }
94 
95     @Test
Test VarHandlenull96     fun `Test VarHandle`() {
97         check(
98             format = FileFormat.V2,
99             sourceFiles =
100                 arrayOf(
101                     java(
102                         """
103                             package java.lang.invoke;
104 
105                             public abstract class VarHandle {
106                                 public final native Object get(Object... args);
107                                 public final native boolean compareAndSet(Object... args);
108                                 public final native void set(Object... args);
109                             }
110                         """
111                             .trimIndent()
112                     ),
113                 ),
114             api =
115                 """
116                     // Signature format: 2.0
117                     package java.lang.invoke {
118                       public abstract class VarHandle {
119                         ctor public VarHandle();
120                         method public final native boolean compareAndSet(java.lang.Object...);
121                         method public final native Object get(java.lang.Object...);
122                         method public final native void set(java.lang.Object...);
123                       }
124                     }
125                 """
126                     .trimIndent(),
127         )
128     }
129 }
130