Skip to content

Commit 78311ba

Browse files
committed
rename genericResult: param to typeErasedResult:
1 parent 29f088a commit 78311ba

File tree

7 files changed

+55
-15
lines changed

7 files changed

+55
-15
lines changed

Sources/SwiftJava/Macros.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ public macro JavaStaticField(_ javaFieldName: String? = nil, isFinal: Bool = fal
134134
/// In order to mark a generic return type you must indicate it to the @JavaMethod macro like this:
135135
/// ```swift
136136
/// // Java: class Test<T> { public <T> get(); }
137-
/// @JavaMethod(genericResult: "T!")
137+
/// @JavaMethod(typeErasedResult: "T!")
138138
/// func get() -> T!
139139
/// ```
140140
/// This allows the macro to form a call into the get() method, which at runtime, will have an `java.lang.Object`
141141
/// returning method signature, and then, convert the result to the expected `T` type on the Swift side.
142142
@attached(body)
143143
public macro JavaMethod(
144-
genericResult: String? = nil
144+
typeErasedResult: String? = nil
145145
) = #externalMacro(module: "SwiftJavaMacros", type: "JavaMethodMacro")
146146

147147
/// Attached macro that turns a Swift method on JavaClass into one that wraps
@@ -155,7 +155,7 @@ public macro JavaMethod(
155155
/// ```
156156
@attached(body)
157157
public macro JavaStaticMethod(
158-
genericResult: String? = nil
158+
typeErasedResult: String? = nil
159159
) = #externalMacro(module: "SwiftJavaMacros", type: "JavaMethodMacro")
160160

161161
/// Macro that marks extensions to specify that all of the @JavaMethod

Sources/SwiftJava/generated/JavaOptional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import CSwiftJavaJNI
33

44
@JavaClass("java.util.Optional")
55
open class JavaOptional<T: AnyJavaObject>: JavaObject {
6-
@JavaMethod(genericResult: "T")
6+
@JavaMethod(typeErasedResult: "T")
77
open func get() -> T!
88

99
@JavaMethod

Sources/SwiftJavaMacros/JavaMethodMacro.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ extension JavaMethodMacro: BodyMacro {
6262
.as(StringLiteralExprSyntax.self),
6363
stringLiteral.segments.count == 1,
6464
case let .stringSegment(wrapperName)? = stringLiteral.segments.first {
65-
"\(wrapperName)"
65+
// TODO: Improve this unwrapping a bit;
66+
// Trim the trailing ! and ? from the type for purposes
67+
// of initializing the type wrapper in the method body
68+
if "\(wrapperName)".hasSuffix("!") ||
69+
"\(wrapperName)".hasSuffix("?") {
70+
String("\(wrapperName)".dropLast())
71+
} else {
72+
"\(wrapperName)"
73+
}
6674
} else {
6775
nil
6876
}

Sources/SwiftJavaToolLib/JavaClassTranslator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ extension JavaClassTranslator {
683683
// Do we need to record any generic information, in order to enable type-erasure for the upcalls?
684684
var parameters: [String] = []
685685
if hasTypeEraseGenericResultType {
686-
parameters.append("genericResult: \"\(resultType)\"")
686+
parameters.append("typeErasedResult: \"\(resultType)\"")
687687
}
688688
// TODO: generic parameters?
689689

Tests/SwiftJavaMacrosTests/JavaClassMacroTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class JavaKitMacroTests: XCTestCase {
301301
assertMacroExpansion("""
302302
@JavaClass("java.lang.Optional")
303303
open class JavaOptional<T: AnyJavaObject>: JavaObject {
304-
@JavaMethod(genericResult: "T")
304+
@JavaMethod(typeErasedResult: "T")
305305
open func get() -> T!
306306
}
307307
""",

Tests/SwiftJavaToolLibTests/Java2SwiftTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class Java2SwiftTests: XCTestCase {
262262
public struct MyJavaObjects {
263263
""",
264264
"""
265-
@JavaStaticMethod(genericResult: "T!")
265+
@JavaStaticMethod(typeErasedResult: "T!")
266266
public func requireNonNull<T: AnyJavaObject>(_ arg0: T?, _ arg1: MySupplier<JavaString>?) -> T
267267
""",
268268
]
@@ -475,7 +475,7 @@ class Java2SwiftTests: XCTestCase {
475475
public struct MyJavaIntFunction<R: AnyJavaObject> {
476476
""",
477477
"""
478-
@JavaMethod(genericResult: "R!")
478+
@JavaMethod(typeErasedResult: "R!")
479479
public func apply(_ arg0: Int32) -> R!
480480
""",
481481
]

Tests/SwiftJavaToolLibTests/WrapJavaTests/GenericsWrapJavaTests.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class GenericsWrapJavaTests: XCTestCase {
6262
open class ExampleSimpleClass: JavaObject {
6363
""",
6464
"""
65-
@JavaMethod(genericResult: "KeyType!")
65+
@JavaMethod(typeErasedResult: "KeyType!")
6666
open func getGeneric<KeyType: AnyJavaObject>(_ arg0: Item<KeyType>?) -> KeyType!
6767
""",
6868
]
@@ -100,7 +100,7 @@ final class GenericsWrapJavaTests: XCTestCase {
100100
classpath: [classpathURL],
101101
expectedChunks: [
102102
"""
103-
@JavaMethod(genericResult: "KeyType!")
103+
@JavaMethod(typeErasedResult: "KeyType!")
104104
open func getGeneric<KeyType: AnyJavaObject>() -> KeyType!
105105
""",
106106
]
@@ -194,11 +194,11 @@ final class GenericsWrapJavaTests: XCTestCase {
194194
classpath: [classpathURL],
195195
expectedChunks: [
196196
"""
197-
@JavaMethod(genericResult: "T!")
197+
@JavaMethod(typeErasedResult: "T!")
198198
open func getClassGeneric() -> T!
199199
""",
200200
"""
201-
@JavaMethod(genericResult: "M!")
201+
@JavaMethod(typeErasedResult: "M!")
202202
open func getMethodGeneric<M: AnyJavaObject>() -> M!
203203
""",
204204
"""
@@ -292,7 +292,7 @@ final class GenericsWrapJavaTests: XCTestCase {
292292
"""
293293
@JavaClass("com.example.Kappa")
294294
open class Kappa<T: AnyJavaObject>: JavaObject {
295-
@JavaMethod(genericResult: "T!")
295+
@JavaMethod(typeErasedResult: "T!")
296296
open func get() -> T!
297297
}
298298
"""
@@ -331,11 +331,43 @@ final class GenericsWrapJavaTests: XCTestCase {
331331
}
332332
""",
333333
"""
334-
@JavaStaticMethod(genericResult: "T!")
334+
@JavaStaticMethod(typeErasedResult: "T!")
335335
public func nonNull<T: AnyJavaObject>(_ arg0: T?) -> T! where ObjectType == Optional<T>
336336
"""
337337
]
338338
)
339339
}
340+
341+
// TODO: this should be improved some more, we need to generated a `: Map` on the Swift side
342+
func test_wrapJava_genericMethodTypeErasure_genericExtendsMap() async throws {
343+
let classpathURL = try await compileJava(
344+
"""
345+
package com.example;
346+
347+
final class Map<T, U> {}
348+
349+
final class Something {
350+
public <M extends Map<String, String>> M putIn(M map) { return null; }
351+
}
352+
""")
353+
354+
try assertWrapJavaOutput(
355+
javaClassNames: [
356+
"com.example.Map",
357+
"com.example.Something",
358+
],
359+
classpath: [classpathURL],
360+
expectedChunks: [
361+
"""
362+
@JavaClass("com.example.Something")
363+
open class Something: JavaObject {
364+
""",
365+
"""
366+
@JavaMethod(typeErasedResult: "M!")
367+
open func putIn<M: AnyJavaObject>(_ arg0: M?) -> M!
368+
""",
369+
]
370+
)
371+
}
340372

341373
}

0 commit comments

Comments
 (0)