Skip to content

Commit 21b671f

Browse files
Revise Java Callbacks Mode documentation
Updated the documentation for enabling Java Callbacks Mode, including configuration examples and usage details.
1 parent 9a4d20e commit 21b671f

File tree

1 file changed

+17
-89
lines changed

1 file changed

+17
-89
lines changed

Sources/SwiftJavaDocumentation/Documentation.docc/SwiftJavaCommandLineTool.md

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -214,105 +214,33 @@ 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
217+
### Enable Java Callbacks Mode
218218

219-
> **NOTE**: This feature requires disabling the SwiftPM Sandbox and is only supported in 'jni' mode.
219+
> **Note**
220220
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.
221+
---
222222

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-
```
223+
## Overview
243224

244-
**How it works:**
225+
One-line description of the feature.
245226

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
227+
---
250228

251-
**Example usage:**
229+
## Requirements
252230

253-
```swift
254-
// Swift code defining a protocol
255-
public protocol MySwiftProtocol {
256-
func handleData(_ data: String)
257-
func processNumber(_ value: Int) -> Bool
258-
}
231+
- Supported modes
232+
- Sandbox requirements
259233

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-
```
234+
---
275235

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-
}
236+
## How to Enable
290237

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-
}
238+
### Configuration file
239+
```json
240+
{
241+
"enableJavaCallbacks": true,
242+
"mode": "jni"
301243
}
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.
316244

317245
### Generating Java bindings for Swift libraries
318246

@@ -364,4 +292,4 @@ You can refer to the `SwiftJavaConfigurationShared/Configuration` struct to lear
364292
Configuration from the config files may be overriden or augmented by explicit command line parameters,
365293
please refer to the options documentation for details on their behavior.
366294

367-
> Note: **Comments in configuration**: The configuration is a JSON 5 file, which among other things allows `//` and `/* */` comments, so feel free to add line comments explaining rationale for some of the settings in youf configuration.
295+
> Note: **Comments in configuration**: The configuration is a JSON 5 file, which among other things allows `//` and `/* */` comments, so feel free to add line comments explaining rationale for some of the settings in youf configuration.

0 commit comments

Comments
 (0)