Comments (5)
Thanks for your reply. I have the following class, considering your response:
package shared.server;
import java.util.function.Function;
public class RouteHandlerNative implements Function<String, String> {
public String path;
public RouteHandlerNative(String path) {
this.path = path;
}
public native String apply(String arg0);
}
and was able to register a native method via rust:
fn native_apply(mut env: JNIEnv, _class: JClass, input: JString) -> jstring {
// Convert the Java input string to a Rust string
let input_str: String = env.get_string(&input).expect("Invalid string").into();
// Perform your processing on the input here.
// For example, you can create a result string.
let result_str = format!("Hello, {}", input_str);
// Convert the result string back to a Java string
let result_jstring = env
.new_string(result_str)
.expect("Couldn't create Java string");
**result_jstring
}
fn main() {
...
let method = NativeMethod {
name: "apply".into(),
sig: "(Ljava/lang/String;)Ljava/lang/String;".into(),
fn_ptr: native_apply as *mut std::ffi::c_void,
};
let router_handler_native_class = env.find_class("shared/server/RouteHandlerNative").unwrap();
let _ = env.register_native_methods(&router_handler_native_class, &[method]);
let router_handler_ctor = env
.get_method_id(
&router_handler_native_class,
"<init>",
"(Ljava/lang/String;)V",
)
.unwrap();
let router_handler = unsafe {
env.new_object_unchecked(
&router_handler_native_class,
router_handler_ctor,
&[JValue::Object(&j_string_path).as_jni()],
)
}
.unwrap();
let _ = unsafe {
env.call_static_method_unchecked(
&server_class,
add_route_method_id,
ReturnType::Primitive(Primitive::Void),
&[JValue::Object(&router_handler).as_jni()],
)
}
.unwrap()
.v()
.unwrap();
...
}
That way, there is one and only one implementation of apply
for all created objects of RouteHandlerNative
. However, this isn't quite what I am looking for. I want to be able to provide different implementations for apply
for different instances of RouteHandlerNative
. Is there a way to accomplish that?
from jni-rs.
Not an easy way, no. You'll need to make a Java static native
method somewhere, then supply a Java lambda that calls said native
method to whichever Java API requires it.
Or just write a Java class that implements Function
, with public native R apply(T t);
.
Either way, you'll have to write some Java code. There's no way to do this with only Rust and JNI.
from jni-rs.
No. Just like regular Java methods, native
methods have the same implementation for all instances. Some languages (like JavaScript) allow you to change method implementations on a per-instance basis, but Java is not one of them.
from jni-rs.
this feature should not be part of a fundamental library like jni-rs
. Maybe you can try some JNI-Rust FFI library like Robusta
from jni-rs.
I've found a way to implement what I was looking for. Will document it here as soon as I have some time.
Basically registerNative
was what I needed, since the dynamically registered function still lies in the same memory space as the rest of the rust program. And this shared memory was the missing part in my puzzle. Using static native method results in the need of a static library, so a whole separate rust program.
from jni-rs.
Related Issues (20)
- JavaVM should be Clone HOT 1
- `get_static_field_unchecked` was not updated to use ReturnType in #344
- `{get,set}[_static]_field_unchecked` should be `unsafe`
- How to add elements into an `JObjectArray`? HOT 2
- `JNIStr` should implement `to_str()` and not `Deref` directly to a `CStr`
- How to pass JavaVM from c to rust HOT 2
- `JClass::from`, `JString::from`, `JThrowable::from`, etc. safety HOT 2
- Byte type confusion
- Feature request: JString from JChar slice via NewString
- How to return jobject as kotlin data class
- New release on crates.io
- Could not compile jni (build script) HOT 1
- JNIEnv::register_native_methods should be marked as unsafe
- JNIEnv::get_string should delete the local ref of string_class HOT 1
- Performance enhancement in get_string
- `new_object_unchecked` should accept`Desc<'local, JMethodID>`
- [Question]: How does one send a native rust function to java as a callback? HOT 2
- `JNIEnv::take_rust_field` seems meant for use with `java.lang.Object.finalize`, but that is deprecated
- Unsound usages of unsafe implementation from small size to large size HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jni-rs.