Skip to content

Commit d5edb2b

Browse files
committed
#61: Support null values for callbacks to support Future<Void> completion.
1 parent 0c569ee commit d5edb2b

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

java/src/main/java/org/astonbitecode/j4rs/api/invocation/JsonInvocationImpl.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,10 @@ public void invokeAsyncToChannel(final long channelAddress, final String methodN
9191
try {
9292
NativeCallbackToRustFutureSupport callback = newCallbackForAsyncToChannel(channelAddress);
9393
invokeAsyncMethod(methodName, gen.generateArgObjects(args)).handle((o, t) -> {
94-
if (o != null) {
95-
callback.doCallbackSuccess(o);
96-
} else if (t != null) {
94+
if (t != null) {
9795
callback.doCallbackFailure(t);
9896
} else {
99-
String message = String.format("Error while handling the future returned by the invocation of method %s of class %s",
100-
methodName, getObjectClassName());
101-
throw new InvocationException(message);
97+
callback.doCallbackSuccess(o);
10298
}
10399
return Void.TYPE;
104100
});

java/src/main/java/org/astonbitecode/j4rs/api/invocation/NativeCallbackToRustFutureSupport.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.astonbitecode.j4rs.api.invocation;
1616

1717
import org.astonbitecode.j4rs.api.Instance;
18+
import org.astonbitecode.j4rs.api.value.NullObject;
1819
import org.astonbitecode.j4rs.errors.InvocationException;
1920
import org.astonbitecode.j4rs.rust.RustPointer;
2021

@@ -46,10 +47,14 @@ static void initialize(String libname) {
4647
* @param obj The {@link Object} to pass in the callback.
4748
*/
4849
public void doCallbackSuccess(Object obj) {
49-
if (channelPointerOpt.isPresent() && obj != null) {
50-
docallbacktochannel(channelPointerOpt.get().getAddress(), InstanceGenerator.create(obj, obj.getClass()));
50+
if (channelPointerOpt.isPresent()) {
51+
if (obj != null) {
52+
docallbacktochannel(channelPointerOpt.get().getAddress(), InstanceGenerator.create(obj, obj.getClass()));
53+
} else {
54+
docallbacktochannel(channelPointerOpt.get().getAddress(), InstanceGenerator.create(null, NullObject.class));
55+
}
5156
} else {
52-
throw new InvocationException("Cannot do callback. Please make sure that you don't try to access this method while being in the constructor of your class (that extends NativeCallbackSupport)");
57+
throw new InvocationException("Cannot do callback. Please make sure that you don't try to access this method while being in the constructor of your class (that extends NativeCallbackToRustFutureSupport)");
5358
}
5459
}
5560

java/src/main/java/org/astonbitecode/j4rs/tests/MyTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ public Future<String> getErrorWithFuture(String message) {
153153
});
154154
}
155155

156+
public Future<Void> executeVoidFuture(String message) {
157+
return executor.submit(() -> {
158+
return null;
159+
});
160+
}
161+
156162
public static Future<String> getErrorWithFutureStatic(String string) {
157163
return executor.submit(() -> string);
158164
}

rust/src/async_api/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ mod api_unit_tests {
183183
Ok(())
184184
}
185185

186+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
187+
async fn invoke_void_future() -> errors::Result<()> {
188+
let s_test = "j4rs_rust";
189+
let jvm = JvmBuilder::new().build()?;
190+
let my_test = jvm.create_instance("org.astonbitecode.j4rs.tests.MyTest", &[])?;
191+
let instance_res = jvm.invoke_async(&my_test, "executeVoidFuture", &[InvocationArg::try_from(s_test)?]).await;
192+
assert!(instance_res.is_ok());
193+
Ok(())
194+
}
195+
186196
// #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
187197
async fn _memory_leaks_invoke_async_instances() -> errors::Result<()> {
188198
let jvm = JvmBuilder::new().build()?;

0 commit comments

Comments
 (0)