@@ -19,6 +19,7 @@ enum SwiftWrappedError: Error {
1919 case message ( String )
2020}
2121
22+ // snippet.implementation
2223@ JavaImplementation ( " com.example.swift.HelloSwift " )
2324extension HelloSwift : HelloSwiftNativeMethods {
2425 @ JavaMethod
@@ -27,20 +28,24 @@ extension HelloSwift: HelloSwiftNativeMethods {
2728 let answer = self . sayHelloBack ( i + j )
2829 print ( " Swift got back \( answer ) from Java " )
2930
31+ // snippet.staticFieldAccess
3032 print ( " We expect the above value to be the initial value, \( self . javaClass . initialValue ) " )
33+ // snippet.end
3134
3235 print ( " Updating Java field value to something different " )
3336 self . value = 2.71828
3437
3538 let newAnswer = self . sayHelloBack ( 17 )
3639 print ( " Swift got back updated \( newAnswer ) from Java " )
3740
41+ // snippet.classDefinition
3842 let newHello = HelloSwift ( environment : javaEnvironment )
3943 print ( " Swift created a new Java instance with the value \( newHello . value ) " )
4044
4145 let name = newHello . name
4246 print ( " Hello to \( name ) " )
4347 newHello . greet ( " Swift 👋🏽 How's it going " )
48+ // snippet.end
4449
4550 self . name = " a 🗑️-collected language "
4651 _ = self . sayHelloBack ( 42 )
@@ -49,9 +54,12 @@ extension HelloSwift: HelloSwiftNativeMethods {
4954 let value = predicate . test ( JavaInteger ( 3 ) )
5055 print ( " Running a JavaPredicate from swift 3 < 10 = \( value ) " )
5156
57+ // snippet.arraysWrapper
5258 let strings = doublesToStrings ( [ 3.14159 , 2.71828 ] )
5359 print ( " Converting doubles to strings: \( strings ) " )
60+ // snippet.end
5461
62+ // snippet.castPattern
5563 // Try downcasting
5664 if let helloSub = self . as ( HelloSubclass . self ) {
5765 print ( " Hello from the subclass! " )
@@ -61,6 +69,7 @@ extension HelloSwift: HelloSwiftNativeMethods {
6169 } else {
6270 fatalError ( " Expected subclass here " )
6371 }
72+ // snippet.end
6473
6574 // Check escaped name
6675 assert ( self . `init` ( 42 ) == 42 )
@@ -70,25 +79,34 @@ extension HelloSwift: HelloSwiftNativeMethods {
7079 assert ( newHello . is ( HelloSwift . self ) )
7180 assert ( ! newHello . is ( HelloSubclass . self ) )
7281
73- // Create a new instance.
82+ // snippet.inheritance
83+ // Create a new instance of the subclass; Swift mirrors the Java hierarchy.
7484 let helloSubFromSwift = HelloSubclass ( " Hello from Swift " , environment : javaEnvironment )
7585 helloSubFromSwift . greetMe ( )
86+ // snippet.end
7687
88+ // snippet.throwingMethods
7789 do {
7890 try throwMessage ( " I am an error " )
7991 } catch {
8092 print ( " Caught Java error: \( error ) " )
8193 }
94+ // snippet.end
8295
83- // Make sure that the thread safe class is sendable
96+ // snippet.sendableConformance
97+ // Java classes annotated with @ThreadSafe surface as Sendable on the Swift side.
8498 let helper = ThreadSafeHelperClass ( environment : javaEnvironment )
8599 let threadSafe : Sendable = helper
100+ _ = threadSafe
101+ // snippet.end
86102
87103 checkOptionals ( helper : helper )
88104
89105 return i * j
90106 }
107+ // snippet.end
91108
109+ // snippet.optionalsWrapper
92110 func checkOptionals ( helper : ThreadSafeHelperClass ) {
93111 let text : JavaString ? = helper . textOptional
94112 let value : String ? = helper . getValueOptional ( Optional < JavaString > . none )
@@ -101,6 +119,7 @@ extension HelloSwift: HelloSwiftNativeMethods {
101119 print ( " Optional double function returned \( doubleOpt ) " )
102120 print ( " Optional long function returned \( longOpt ) " )
103121 }
122+ // snippet.end
104123
105124 @ JavaMethod
106125 func throwMessageFromSwift ( _ message : String ) throws -> String {
0 commit comments