Skip to content

Java2Swift: Swift does not support overriding with covariant array result types #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/Java2SwiftLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,12 @@ extension Type {
// If both are classes, check for subclassing.
if let selfClass = self.as(JavaClass<JavaObject>.self),
let otherClass = other.as(JavaClass<JavaObject>.self) {
// If either is a Java array, then this cannot be a subtype relationship
// in Swift.
if selfClass.isArray() || otherClass.isArray() {
return false
}

return selfClass.isSubclass(of: otherClass)
}

Expand Down
39 changes: 39 additions & 0 deletions Tests/Java2SwiftTests/Java2SwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,35 @@ class Java2SwiftTests: XCTestCase {
]
)
}

func testCovariantInJavaNotInSwiftOverride3() throws {
try assertTranslatedClass(
NIOByteBuffer.self,
swiftTypeName: "NIOByteBuffer",
asClass: true,
translatedClasses: [
"java.lang.Object" : ("JavaObject", "JavaKit"),
"java.lang.Class" : ("JavaClass", "JavaKit"),
"java.nio.Buffer": ("NIOBuffer", "JavaKitNIO"),
"java.nio.ByteBuffer": ("NIOByteBuffer", "JavaKitNIO"),
],
expectedChunks: [
"import JavaKitNIO",
"""
@JavaClass("java.nio.ByteBuffer")
open class NIOByteBuffer: NIOBuffer {
""",
"""
@JavaMethod
open func array() -> [Int8]
""",
"""
@JavaMethod
open override func arrayOffset() -> Int32
""",
]
)
}
}

@JavaClass("java.lang.ClassLoader")
Expand Down Expand Up @@ -597,6 +626,16 @@ public struct Executable {
public struct TypeVariable<D: AnyJavaObject> {
}

@JavaClass("java.nio.Buffer")
open class NIOBuffer: JavaObject {

}

@JavaClass("java.nio.ByteBuffer")
open class NIOByteBuffer: NIOBuffer {

}

/// Translate a Java class and assert that the translated output contains
/// each of the expected "chunks" of text.
func assertTranslatedClass<JavaClassType: AnyJavaObject>(
Expand Down