diff --git a/Sources/Java2SwiftLib/JavaClassTranslator.swift b/Sources/Java2SwiftLib/JavaClassTranslator.swift index afcac639..b90a538a 100644 --- a/Sources/Java2SwiftLib/JavaClassTranslator.swift +++ b/Sources/Java2SwiftLib/JavaClassTranslator.swift @@ -819,6 +819,12 @@ extension Type { // If both are classes, check for subclassing. if let selfClass = self.as(JavaClass.self), let otherClass = other.as(JavaClass.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) } diff --git a/Tests/Java2SwiftTests/Java2SwiftTests.swift b/Tests/Java2SwiftTests/Java2SwiftTests.swift index 445c501c..259193b1 100644 --- a/Tests/Java2SwiftTests/Java2SwiftTests.swift +++ b/Tests/Java2SwiftTests/Java2SwiftTests.swift @@ -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") @@ -597,6 +626,16 @@ public struct Executable { public struct TypeVariable { } +@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(