Skip to content

Commit 89b0a06

Browse files
authored
Merge pull request #148 from DougGregor/array-types-not-covariant
Java2Swift: Swift does not support overriding with covariant array result types
2 parents 719ae11 + f192342 commit 89b0a06

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

Sources/Java2SwiftLib/JavaClassTranslator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,12 @@ extension Type {
819819
// If both are classes, check for subclassing.
820820
if let selfClass = self.as(JavaClass<JavaObject>.self),
821821
let otherClass = other.as(JavaClass<JavaObject>.self) {
822+
// If either is a Java array, then this cannot be a subtype relationship
823+
// in Swift.
824+
if selfClass.isArray() || otherClass.isArray() {
825+
return false
826+
}
827+
822828
return selfClass.isSubclass(of: otherClass)
823829
}
824830

Tests/Java2SwiftTests/Java2SwiftTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,35 @@ class Java2SwiftTests: XCTestCase {
547547
]
548548
)
549549
}
550+
551+
func testCovariantInJavaNotInSwiftOverride3() throws {
552+
try assertTranslatedClass(
553+
NIOByteBuffer.self,
554+
swiftTypeName: "NIOByteBuffer",
555+
asClass: true,
556+
translatedClasses: [
557+
"java.lang.Object" : ("JavaObject", "JavaKit"),
558+
"java.lang.Class" : ("JavaClass", "JavaKit"),
559+
"java.nio.Buffer": ("NIOBuffer", "JavaKitNIO"),
560+
"java.nio.ByteBuffer": ("NIOByteBuffer", "JavaKitNIO"),
561+
],
562+
expectedChunks: [
563+
"import JavaKitNIO",
564+
"""
565+
@JavaClass("java.nio.ByteBuffer")
566+
open class NIOByteBuffer: NIOBuffer {
567+
""",
568+
"""
569+
@JavaMethod
570+
open func array() -> [Int8]
571+
""",
572+
"""
573+
@JavaMethod
574+
open override func arrayOffset() -> Int32
575+
""",
576+
]
577+
)
578+
}
550579
}
551580

552581
@JavaClass("java.lang.ClassLoader")
@@ -597,6 +626,16 @@ public struct Executable {
597626
public struct TypeVariable<D: AnyJavaObject> {
598627
}
599628

629+
@JavaClass("java.nio.Buffer")
630+
open class NIOBuffer: JavaObject {
631+
632+
}
633+
634+
@JavaClass("java.nio.ByteBuffer")
635+
open class NIOByteBuffer: NIOBuffer {
636+
637+
}
638+
600639
/// Translate a Java class and assert that the translated output contains
601640
/// each of the expected "chunks" of text.
602641
func assertTranslatedClass<JavaClassType: AnyJavaObject>(

0 commit comments

Comments
 (0)