Skip to content

Commit 9a4d20e

Browse files
Documentation update
1 parent 0685f55 commit 9a4d20e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Sources/SwiftJavaDocumentation/Documentation.docc/SwiftJavaCommandLineTool.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,106 @@ The project is still very early days, however the general outline of using this
214214

215215
You can then use Swift libraries in Java just by calling the appropriate methods and initializers.
216216

217+
#### Enable Java Callbacks Mode
218+
219+
> **NOTE**: This feature requires disabling the SwiftPM Sandbox and is only supported in 'jni' mode.
220+
221+
The `enableJavaCallbacks` mode is an advanced feature that allows Java classes to implement Swift protocols. When enabled, JExtract generates additional Java interface wrappers that enable bidirectional interoperability - not only can Java code call Swift functions, but Java classes can also implement Swift protocols and be passed back to Swift.
222+
223+
**Key characteristics:**
224+
- **Mode restriction**: Only available in JNI mode (will throw an error if used with FFM mode)
225+
- **Sandbox requirement**: Requires disabling SwiftPM sandbox (`--disable-sandbox` or equivalent)
226+
- **Protocol implementation**: Enables Java classes to implement Swift protocols
227+
- **Bidirectional communication**: Allows callbacks from Swift back to Java implementations
228+
229+
**How to enable:**
230+
231+
In your `swift-java.config` file:
232+
```json
233+
{
234+
"enableJavaCallbacks": true,
235+
"mode": "jni"
236+
}
237+
```
238+
239+
Or via command line:
240+
```bash
241+
swift-java jextract --enable-java-callbacks --mode jni ...
242+
```
243+
244+
**How it works:**
245+
246+
When `enableJavaCallbacks` is enabled, JExtract generates:
247+
1. **Interface wrappers**: Java interfaces that correspond to Swift protocols
248+
2. **Protocol implementations**: Generated Java classes that can be passed to Swift
249+
3. **Bridge code**: JNI bridge code that allows Swift to call back into Java implementations
250+
251+
**Example usage:**
252+
253+
```swift
254+
// Swift code defining a protocol
255+
public protocol MySwiftProtocol {
256+
func handleData(_ data: String)
257+
func processNumber(_ value: Int) -> Bool
258+
}
259+
260+
// Swift code that uses the protocol
261+
public class SwiftProcessor {
262+
private var delegate: MySwiftProtocol?
263+
264+
public func setDelegate(_ delegate: MySwiftProtocol) {
265+
self.delegate = delegate
266+
}
267+
268+
public func doWork() {
269+
delegate?.handleData("Hello from Swift")
270+
let result = delegate?.processNumber(42) ?? false
271+
print("Result: \(result)")
272+
}
273+
}
274+
```
275+
276+
```java
277+
// Java code that implements the Swift protocol
278+
public class MyJavaImplementation implements MySwiftProtocol {
279+
@Override
280+
public void handleData(String data) {
281+
System.out.println("Java received: " + data);
282+
}
283+
284+
@Override
285+
public boolean processNumber(int value) {
286+
System.out.println("Java processing: " + value);
287+
return value > 0;
288+
}
289+
}
290+
291+
// Java usage
292+
public class Main {
293+
public static void main(String[] args) {
294+
MyJavaImplementation javaImpl = new MyJavaImplementation();
295+
SwiftProcessor processor = new SwiftProcessor();
296+
297+
// Pass Java implementation to Swift
298+
processor.setDelegate(javaImpl);
299+
processor.doWork();
300+
}
301+
}
302+
```
303+
304+
**Limitations:**
305+
- Only supported in JNI mode
306+
- Requires SwiftPM sandbox to be disabled
307+
- Performance overhead compared to direct JNI calls
308+
- Limited to protocol methods (not all Swift features)
309+
310+
**When to use:**
311+
- When you need Java classes to implement Swift protocols
312+
- For event-driven architectures where Swift needs to call back into Java
313+
- When building hybrid applications that require tight integration between languages
314+
315+
For a complete working example, see `Samples/SwiftJavaExtractJNISampleApp` which demonstrates this feature in action.
316+
217317
### Generating Java bindings for Swift libraries
218318

219319
This repository also includes the `jextract-swift` tool which is similar to the JDK's [`jextract`](https://github.com/openjdk/jextract/).

0 commit comments

Comments
 (0)