forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsWrapJavaTests.swift
More file actions
445 lines (395 loc) · 11.6 KB
/
GenericsWrapJavaTests.swift
File metadata and controls
445 lines (395 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@_spi(Testing) import SwiftJava
import SwiftJavaToolLib
import JavaUtilJar
import SwiftJavaShared
import JavaNet
import SwiftJavaConfigurationShared
import Subprocess
import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43
final class GenericsWrapJavaTests: XCTestCase {
func testWrapJavaGenericMethod_singleGeneric() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class Item<T> {
final T value;
Item(T item) {
this.value = item;
}
}
class Pair<First, Second> { }
class ExampleSimpleClass {
<KeyType> KeyType getGeneric(Item<KeyType> key) { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Item",
"com.example.Pair",
"com.example.ExampleSimpleClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
import CSwiftJavaJNI
import SwiftJava
""",
"""
@JavaClass("com.example.Pair")
open class Pair<First: AnyJavaObject, Second: AnyJavaObject>: JavaObject {
""",
"""
@JavaClass("com.example.ExampleSimpleClass")
open class ExampleSimpleClass: JavaObject {
""",
"""
@JavaMethod(typeErasedResult: "KeyType!")
open func getGeneric<KeyType: AnyJavaObject>(_ arg0: Item<KeyType>?) -> KeyType!
""",
]
)
}
// This is just a warning in Java, but a hard error in Swift, so we must 'prune' generic params
func testWrapJavaGenericMethod_pruneNotUsedGenericParam() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class Item<T> {
final T value;
Item(T item) {
this.value = item;
}
}
class Pair<First, Second> { }
final class ExampleSimpleClass {
// use in return type
<KeyType, NotUsedParam> KeyType getGeneric() {
return null;
}
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Item",
"com.example.Pair",
"com.example.ExampleSimpleClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaMethod(typeErasedResult: "KeyType!")
open func getGeneric<KeyType: AnyJavaObject>() -> KeyType!
""",
]
)
}
func testWrapJavaGenericMethod_multipleGenerics() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class Item<T> {
final T value;
Item(T item) {
this.value = item;
}
}
class Pair<First, Second> { }
class ExampleSimpleClass {
<KeyType, ValueType> Pair<KeyType, ValueType> getPair(String name, Item<KeyType> key, Item<ValueType> value) { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Item",
"com.example.Pair",
"com.example.ExampleSimpleClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
import CSwiftJavaJNI
import SwiftJava
""",
"""
@JavaClass("com.example.Item")
open class Item<T: AnyJavaObject>: JavaObject {
""",
"""
@JavaClass("com.example.Pair")
open class Pair<First: AnyJavaObject, Second: AnyJavaObject>: JavaObject {
""",
"""
@JavaClass("com.example.ExampleSimpleClass")
open class ExampleSimpleClass: JavaObject {
""",
"""
@JavaMethod
open func getPair<KeyType: AnyJavaObject, ValueType: AnyJavaObject>(_ arg0: String, _ arg1: Item<KeyType>?, _ arg2: Item<ValueType>?) -> Pair<KeyType, ValueType>!
""",
]
)
}
func test_Java2Swift_returnType_generic() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
// Mini decls in order to avoid warnings about some funcs we're not yet importing cleanly
final class List<T> {}
final class Map<T, U> {}
final class Number {}
class GenericClass<T> {
public T getClassGeneric() { return null; }
public <M> M getMethodGeneric() { return null; }
public <M> Map<T, M> getMixedGeneric() { return null; }
public String getNonGeneric() { return null; }
public List<T> getParameterizedClassGeneric() { return null; }
public List<? extends Number> getWildcard() { return null; }
public T[] getGenericArray() { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Map",
"com.example.List",
"com.example.Number",
"com.example.GenericClass",
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaMethod(typeErasedResult: "T!")
open func getClassGeneric() -> T!
""",
"""
@JavaMethod(typeErasedResult: "M!")
open func getMethodGeneric<M: AnyJavaObject>() -> M!
""",
"""
@JavaMethod
open func getMixedGeneric<M: AnyJavaObject>() -> Map<T, M>!
""",
"""
@JavaMethod
open func getNonGeneric() -> String
""",
"""
@JavaMethod
open func getParameterizedClassGeneric() -> List<T>!
""",
"""
@JavaMethod
open func getWildcard() -> List<Number>!
""",
"""
@JavaMethod
open func getGenericArray() -> [T?]
""",
]
)
}
func testWrapJavaGenericSuperclass() async throws {
return // FIXME: we need this
let classpathURL = try await compileJava(
"""
package com.example;
class ByteArray {}
class CompressingStore extends AbstractStore<ByteArray, byte[], byte[]> {}
abstract class AbstractStore<K, V, T> {} // implements Store<K, V, T> {}
// interface Store<K, V, T> {}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.ByteArray",
// TODO: what if we visit in other order, does the wrap-java handle it
// "com.example.Store",
"com.example.AbstractStore",
"com.example.CompressingStore",
],
classpath: [classpathURL],
expectedChunks: [
"""
import CSwiftJavaJNI
import SwiftJava
""",
"""
@JavaClass("com.example.ByteArray")
open class ByteArray: JavaObject {
""",
"""
@JavaClass("com.example.CompressingStore")
open class CompressingStore: AbstractStore<ByteArray, [UInt8], [UInt8]> {
"""
]
)
}
func test_wrapJava_genericMethodTypeErasure_returnType() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
final class Kappa<T> {
public T get() { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Kappa",
],
classpath: [classpathURL],
expectedChunks: [
"""
import CSwiftJavaJNI
import SwiftJava
""",
"""
@JavaClass("com.example.Kappa")
open class Kappa<T: AnyJavaObject>: JavaObject {
""",
"""
@JavaMethod(typeErasedResult: "T!")
open func get() -> T!
}
"""
]
)
}
func test_wrapJava_genericMethodTypeErasure_ofNullableOptional_staticMethods() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
final class Optional<T> {
public static <T> Optional<T> ofNullable(T value) { return null; }
public static <T> T nonNull(T value) { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Optional"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaClass("com.example.Optional")
open class Optional<T: AnyJavaObject>: JavaObject {
""",
"""
extension JavaClass {
""",
"""
@JavaStaticMethod
public func ofNullable<T: AnyJavaObject>(_ arg0: T?) -> Optional<T>! where ObjectType == Optional<T>
}
""",
"""
@JavaStaticMethod(typeErasedResult: "T!")
public func nonNull<T: AnyJavaObject>(_ arg0: T?) -> T! where ObjectType == Optional<T>
"""
]
)
}
func test_wrapJava_genericMethodTypeErasure_customInterface_staticMethods() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
interface MyInterface {}
final class Public {
public static <T extends MyInterface> void useInterface(T myInterface) { }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.MyInterface",
"com.example.Public"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaInterface("com.example.MyInterface")
public struct MyInterface {
""",
"""
@JavaClass("com.example.Public")
open class Public: JavaObject {
""",
"""
extension JavaClass<Public> {
""",
"""
@JavaStaticMethod
public func useInterface<T: AnyJavaObject>(_ arg0: T?)
}
"""
]
)
}
// TODO: this should be improved some more, we need to generated a `: Map` on the Swift side
func test_wrapJava_genericMethodTypeErasure_genericExtendsMap() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
final class Map<T, U> {}
final class Something {
public <M extends Map<String, String>> M putIn(M map) { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Map",
"com.example.Something",
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaClass("com.example.Something")
open class Something: JavaObject {
""",
"""
@JavaMethod(typeErasedResult: "M!")
open func putIn<M: AnyJavaObject>(_ arg0: M?) -> M!
""",
]
)
}
func testWrapJavaGenericMethod_parameterizedParameterType() async throws {
let classpathURL = try await compileJava(
"""
package com.example;
class Resolver<T> { }
class Serializer<T> { }
class Store {
public <ValueType> Store
bootstrapStore(
String name,
Resolver<ValueType> resolver,
Serializer<ValueType> serializer
) { return null; }
}
""")
try assertWrapJavaOutput(
javaClassNames: [
"com.example.Resolver",
"com.example.Serializer",
"com.example.Store"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaMethod
open func bootstrapStore<ValueType: AnyJavaObject>(_ arg0: String, _ arg1: Resolver<ValueType>?, _ arg2: Serializer<ValueType>?) -> Store!
""",
]
)
}
}