Skip to content

Commit f79ad46

Browse files
authored
Fix JavaIterator conformance (#600)
1 parent 49c1af7 commit f79ad46

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

Sources/SwiftJava/JavaExtensions/JavaIterator+Iterator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
extension JavaIterator: IteratorProtocol {
1616
public typealias Element = E
1717

18-
public func next() -> E? {
18+
@_implements(IteratorProtocol,next())
19+
public mutating func swiftNext() -> E? {
1920
if hasNext() {
20-
let nextResult: JavaObject? = next()
21-
return nextResult.map { $0.as(E.self)! }
21+
return next() as E
2222
}
2323

2424
return nil

Sources/SwiftJava/generated/JavaIterator.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,30 @@ import SwiftJavaJNICore
33

44
@JavaInterface("java.util.Iterator")
55
public struct JavaIterator<E: AnyJavaObject> {
6+
/// Java method `remove`.
7+
///
8+
/// ### Java method signature
9+
/// ```java
10+
/// public default void java.util.Iterator.remove()
11+
/// ```
612
@JavaMethod
713
public func remove()
814

15+
/// Java method `hasNext`.
16+
///
17+
/// ### Java method signature
18+
/// ```java
19+
/// public abstract boolean java.util.Iterator.hasNext()
20+
/// ```
921
@JavaMethod
1022
public func hasNext() -> Bool
1123

12-
@JavaMethod
13-
public func next() -> JavaObject!
24+
/// Java method `next`.
25+
///
26+
/// ### Java method signature
27+
/// ```java
28+
/// public abstract E java.util.Iterator.next()
29+
/// ```
30+
@JavaMethod(typeErasedResult: "E!")
31+
public func next() -> E!
1432
}

Tests/SwiftJavaTests/BasicRuntimeTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import JavaNet
16+
import JavaUtil
1617
import SwiftJava
1718
import XCTest // NOTE: Workaround for https://github.com/swiftlang/swift-java/issues/43
1819

@@ -96,6 +97,17 @@ class BasicRuntimeTests: XCTestCase {
9697

9798
XCTAssertEqual(string, "https://swift.org")
9899
}
100+
101+
func testListIterator() throws {
102+
let environment = try jvm.environment()
103+
104+
let javaList = try XCTUnwrap(ArrayList<JavaInteger>(environment: environment).as(List<JavaInteger>.self))
105+
_ = javaList.add(JavaInteger(0, environment: environment))
106+
_ = javaList.add(JavaInteger(1, environment: environment))
107+
_ = javaList.add(JavaInteger(2, environment: environment))
108+
109+
XCTAssertEqual(javaList.map { $0.intValue() }, [0, 1, 2])
110+
}
99111
}
100112

101113
@JavaClass("org.swift.javakit.Nonexistent")

0 commit comments

Comments
 (0)