xref: /aosp_15_r20/external/ksp/test-utils/testData/api/checkOverride.kt (revision af87fb4bb8e3042070d2a054e912924f599b22b7)
1 /*
2  * Copyright 2020 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 // TEST PROCESSOR: CheckOverrideProcessor
19 // EXPECTED:
20 // KotlinList.get overrides JavaList.get: false
21 // KotlinList.foo overrides JavaList.foo: true
22 // KotlinList.fooo overrides JavaList.foo: false
23 // KotlinList.foo overrides KotlinList.foo: false
24 // KotlinList.equals overrides JavaList.equals: true
25 // KotlinList2.baz overrides KotlinList.baz: true
26 // KotlinList2.baz overrides KotlinList.bazz: false
27 // KotlinList2.bazz overrides KotlinList.bazz: true
28 // KotlinList2.bazz overrides KotlinList.baz: false
29 // KotlinList2.baz overrides KotlinList2.baz: false
30 // JavaImpl.getY overrides JavaImpl.getX: false
31 // JavaImpl.getY overrides MyInterface.x: false
32 // JavaImpl.getX overrides MyInterface.x: true
33 // JavaImpl.setY overrides MyInterface.y: true
34 // JavaImpl.setX overrides MyInterface.x: false
35 // JavaImpl.getY overrides JavaImpl.getY: false
36 // MyInterface.x overrides JavaImpl.getY: false
37 // MyInterface.x overrides JavaImpl.getX: false
38 // MyInterface.y overrides JavaImpl.setY: false
39 // MyInterface.y overrides MyInterface.y: false
40 // MyInterface2.receiveList overrides MyInterface2ImplWithoutType.receiveList: false
41 // MyInterface2ImplWithoutType.receiveList overrides MyInterface2.receiveList: true
42 // MyInterface2ImplWithType.receiveList overrides MyInterface2.receiveList: true
43 // MyInterface2ImplWithType.receiveList overrides MyInterface2ImplWithoutType.receiveList: true
44 // JavaDifferentReturnType.foo overrides JavaList.foo: true
45 // Base.f1 overrides MyInterface3.f1: true
46 // Base.prop overrides MyInterface3.prop: true
47 // JBase.getProp overrides MyInterface3.prop: true
48 // END
49 // FILE: a.kt
50 
51 annotation class GetAnno
52 annotation class FooAnno
53 annotation class BarAnno
54 annotation class BazAnno
55 annotation class Baz2Anno
56 annotation class BazzAnno
57 annotation class Bazz2Anno
58 
59 open class KotlinList(): JavaList() {
60     @GetAnno
getnull61     fun get(): Double {
62         return 2.0
63     }
64 
equalsnull65     override fun equals(other: Any?): Boolean {
66         return false
67     }
68 
69     @FooAnno
foonull70     override fun foo(): Int {
71         return 2
72     }
73 
74     @BarAnno
fooonull75     override fun fooo(): Int {
76         return 2
77     }
78 
79     @Baz2Anno
80     open val baz: Int get() {
81         return 1
82     }
83 
84     @Bazz2Anno
85     open val bazz: Int get() {
86         return 1
87     }
88 }
89 
90 class KotlinList2(@BazzAnno override val bazz: Int = 2): KotlinList() {
91     @BazAnno
92     override val baz: Int get() {
93         return 2
94     }
95 }
96 
97 interface MyInterface {
98     val x: Int
99     var y: Int
100 }
101 
102 enum class EnumType {
103     FOO,
104     BAR;
105 }
106 
107 interface MyInterface2<T> {
receiveListnull108     fun receiveList(argsInParent : List<T>):Unit
109 }
110 
111 interface MyInterface2ImplWithoutType<T> : MyInterface2<T> {
112     override fun receiveList(argsInParent : List<T>):Unit
113 }
114 
115 interface MyInterface2ImplWithType : MyInterface2ImplWithoutType<EnumType> {
receiveListnull116     override fun receiveList(argsInParent : List<EnumType>):Unit
117 }
118 
119 interface MyInterface3 {
120     fun f1()
121     val prop: String
122 }
123 
124 open class Base {
125     val prop: String = ""
f1null126     fun f1() {
127     }
128 }
129 
130 class BaseOverride: MyInterface3, Base() {
f2null131     fun f2() {}
132 }
133 
134 // FILE: JBaseOverride.java
<lambda>null135 public class JBaseOverride extends JBase implements MyInterface3 {
136 
137 }
138 
139 // FILE: JBase.java
140 
<lambda>null141 public class JBase {
142     public String getProp() {
143         return "JBase";
144     }
145 }
146 
147 // FILE: JavaList.java
148 
149 import java.util.*;
150 
151 public class JavaList extends List<String> {
152     @Override
153     public String get(int index) {
154         return "OK";
155     }
156 
<lambda>null157     protected int foo() {
158         return 1;
159     }
160 }
161 
162 // FILE: JavaImpl.java
163 
164 public class JavaImpl implements MyInterface {
<lambda>null165     public int getX() {
166         return 1;
167     }
168 
<lambda>null169     public int getY() {
170         return 1;
171     }
172 
173     public void setY(int value) {
174 
175     }
176 
177     // intentional override check for a val property
178     public void setX(int value) {
179         return value;
180     }
181 }
182 
183 // FILE: JavaDifferentReturnType.java
<lambda>null184 public abstract class JavaDifferentReturnType extends JavaList {
185     // intentional different return type
186     protected String foo() {
187         return "";
188     }
189 }
190