Comments (3)
For a similar reason I ended up writing https://github.com/viperproject/jni-gen (undocumented...)
Our approach inspects the JVM by reflection at Rust's compile time and generates wrappers for methods listed in a whitelist in build.rs
(see here for an example). We introduced the whitelist because we choose to generate a wrapper for every inherited method, thus increasing a lot the size of the generated code. Whenever possible (that is, unambiguous) the user can leave a field empty to let the generator choose the only possible name/signature/...
An example of the generated bindings: https://viperproject.github.io/prusti-dev/viper_sys/wrappers/index.html
Some problems that we found:
- Java methods can be overloaded. How to choose the corresponding name in Rust?
- Java method names allow more characters than function names in Rust. How to translate them?
- How to handle inherited Java methods? Should the user know what is the base class that implements it, or should a wrapper be generated in all inheriting classes?
The end result works fine, but there is a lot that can be improved. For example, the whitelist in build.rs
may be intimidating at a first glance, the name of the method parameters is not preserved, and so on.
from jni-rs.
Federico, thank you for sharing your experience!
Just to avoid any confusion: are you talking about the opposite use-case -- creating native proxies of Java objects? I think that deserves a separate issue, it would be great if you submit one! There might be something the library can provide to ease that task.
We actually have both use-cases, yours on smaller scale, just a couple of hand-written proxies. They look like this:
// Pseudo-code
struct JavaFooProxy {
// A global reference to the Java object.
foo: GlobalRef;
// An abstraction that allows you to execute some code with JNIEnv in a thread-safe manner.
//
// How it provides a context (JNIEnv) -- is an implementation detail.
// For example, there might be implementations that:
// - Always attach/detach threads, which is VERY costly.
// - Keep a couple of attached threads and pass the lambda to them,
// - Rely on the fact that your app never creates more than N threads
// and never `detach` them (a benign leak).
// - For local references: use JNIEnv directly.
executor: Executor;
// Calls a Java `Foo#bar` method.
def bar() {
executor.with_context((env: JNIEnv) -> {
env.call_method(foo, "bar", BAR_SIGNATURE, …);
}
}
}
Regarding method name translation, you might find useful the JNI rules (that's the opposite use-case, but there might be some solutions that you may re-use): https://docs.oracle.com/javase/9/docs/specs/jni/design.html#resolving-native-method-names
from jni-rs.
Oh, I got it the other way around. Yes, I meant creating native proxies of Java objects. I just opened an issue to track it (#82).
Thanks for the suggestions!
from jni-rs.
Related Issues (20)
- JNIEnv::get_object_array_element requires &mut self HOT 2
- Example request: `call_method` with non-primitive type as parameters. HOT 4
- TryFrom<char> for `char` primitive HOT 7
- 0.21 mutability requirements on `JNIEnv` methods break use of `catch_unwind` HOT 15
- Nested "critical sections" disallowed by borrow checker HOT 1
- Provide ways to use `CallNonvirtual*Method` in `JNIEnv` HOT 1
- Converting between types in JNI code HOT 2
- `AttachGuard` detaches too soon when dropped out of order HOT 2
- how to use call_static_method_unchecked with a GlobalRef to a class? HOT 2
- how to get java list on rust HOT 1
- CallNonvirtual<type>Method support request HOT 2
- Can i load native function on exec self ? HOT 3
- Question: How I use `GetObjectRefType` HOT 4
- Make methods `const fn` when it makes sense HOT 1
- `get_rust_field()` exclusive access to `JNIEnv` HOT 8
- How I use `MonitorEnter` and `MonitorExit`
- use catch_unwind to ensure balanced push/pop_local_frame in with_local_frame
- `JNIEnv::get_version()` should be infallible
- JNIVersion should implement `Ord` and be able to hold unknown, newer versions as u32 values
- JNIEnv::exception_check should just return a bool and REQUIRES JNI 1.2 as a minimum supported version! HOT 1
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.