Skip to content

Commit 24dc4ef

Browse files
committed
docs: more examples
1 parent 62c4081 commit 24dc4ef

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,53 @@ fn your_function_name(arg: String) -> Result<Vec<String>, String> {
1313
}
1414
```
1515

16+
### conversions
1617
every type that must go into/from Java must implement `IntoJava` or `FromJava` (methods will receive a `&mut JNIEnv` and can return errors).
1718
most primitives already have them implemented. conversions are automatic and the wrapper function will invoke IntoJava/FromJava for every type,
1819
passing an environment reference.
1920

21+
```rust
22+
impl<'j> IntoJava for MyClass {
23+
type T = jni::sys::jobject;
24+
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
25+
let hello = env.new_string("world")?;
26+
// TODO!!
27+
}
28+
}
29+
```
2030

31+
### pointers
32+
to return pointer type values, add the `ptr` attribute
33+
34+
note that, while possible to pass raw pointers to the JVM, it is not safe by default and must be done with extreme care.
35+
36+
### exceptions
2137
Errors are thrown automatically when a `Result` is an error. For your errors to work, you must implement the `JniToolboxError` trait for your errors,
2238
(which just returns the path to your Java error class) and then make a Java error wrapper which can be constructed with a single string argument.
2339
functions returning `Result`s will automatically have their return value unwrapped and, if is an err, throw an exception and return early.
2440

41+
```rust
42+
impl JniToolboxError for MyError {
43+
fn jclass(&self) -> String {
44+
"my/package/some/MyError".to_string()
45+
}
46+
}
47+
```
48+
49+
```java
50+
package my.package.some;
51+
public class MyError {
52+
public MyError(String x) {
53+
// TODO
54+
}
55+
}
56+
```
57+
2558
to throw simple exceptions, it's possible to use the `exception` attribute. just pass your exception's path (must be constructable with a single string argument!)
2659

27-
to return pointer type values, add the `ptr` attribute
2860

2961

30-
## examples
62+
### examples
3163
the following function:
3264
```rust
3365
#[jni(package = "mp.code", class = "Client", ptr)]

0 commit comments

Comments
 (0)