File tree Expand file tree Collapse file tree 5 files changed +41
-1
lines changed
Sources/SwiftJavaDocumentation/Documentation.docc Expand file tree Collapse file tree 5 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,14 @@ public class MySwiftClass {
5959 self . y = 5
6060 }
6161
62+ convenience public init ( throwing: Bool ) throws {
63+ if throwing {
64+ throw MySwiftError . swiftError
65+ } else {
66+ self . init ( )
67+ }
68+ }
69+
6270 deinit {
6371 p ( " deinit called! " )
6472 }
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ public struct MySwiftStruct {
2121 self . len = len
2222 }
2323
24+ public init ? ( doInit: Bool ) {
25+ if doInit {
26+ self . init ( cap: 10 , len: 10 )
27+ } else {
28+ return nil
29+ }
30+ }
31+
2432 public func getCapacity( ) -> Int64 {
2533 self . cap
2634 }
Original file line number Diff line number Diff line change @@ -40,6 +40,17 @@ void init_withParameters() {
4040 }
4141 }
4242
43+ @ Test
44+ void init_throwing () {
45+ try (var arena = SwiftArena .ofConfined ()) {
46+ Exception exception = assertThrows (Exception .class , () -> MySwiftClass .init (true , arena ));
47+ assertEquals ("swiftError" , exception .getMessage ());
48+
49+ MySwiftClass c = assertDoesNotThrow (() -> MySwiftClass .init (false , arena ));
50+ assertNotNull (c );
51+ }
52+ }
53+
4354 @ Test
4455 void sum () {
4556 try (var arena = SwiftArena .ofConfined ()) {
Original file line number Diff line number Diff line change 1818import org .swift .swiftkit .core .ConfinedSwiftMemorySession ;
1919import org .swift .swiftkit .core .SwiftArena ;
2020
21+ import java .util .Optional ;
22+
2123import static org .junit .jupiter .api .Assertions .*;
2224
2325public class MySwiftStructTest {
@@ -30,6 +32,17 @@ void init() {
3032 }
3133 }
3234
35+ @ Test
36+ void init_optional () {
37+ try (var arena = SwiftArena .ofConfined ()) {
38+ assertEquals (Optional .empty (), MySwiftStruct .init (false , arena ));
39+
40+ Optional <MySwiftStruct > optionalStruct = MySwiftStruct .init (true , arena );
41+ assertTrue (optionalStruct .isPresent ());
42+ assertEquals (10 , optionalStruct .get ().getLen ());
43+ }
44+ }
45+
3346 @ Test
3447 void getAndSetLen () {
3548 try (var arena = SwiftArena .ofConfined ()) {
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ SwiftJava's `swift-java jextract` tool automates generating Java bindings from S
4747| Swift Feature | FFM | JNI |
4848| --------------------------------------------------------------------------------------| ----------| -----|
4949| Initializers: ` class ` , ` struct ` | ✅ | ✅ |
50- | Optional Initializers / Throwing Initializers | ❌ | ❌ |
50+ | Optional Initializers / Throwing Initializers | ❌ | ✅ |
5151| Deinitializers: ` class ` , ` struct ` | ✅ | ✅ |
5252| ` enum ` | ❌ | ✅ |
5353| ` actor ` | ❌ | ❌ |
You can’t perform that action at this time.
0 commit comments