Code Monkey home page Code Monkey logo

Comments (5)

proohit avatar proohit commented on June 21, 2024 1

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.

argv-minus-one avatar argv-minus-one commented on June 21, 2024

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.

argv-minus-one avatar argv-minus-one commented on June 21, 2024

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.

xanahopper avatar xanahopper commented on June 21, 2024

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.

proohit avatar proohit commented on June 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.