You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Creates a compile-time type-checked `NativeMethod` descriptor for a native method.
659
+
/// Bind a single native method to a Rust function with type safety and optionally export it.
660
+
///
661
+
/// This macro can do the following:
662
+
/// - Generate a `NativeMethod` struct with type-checked function pointer
663
+
/// - Optionally wrap the implementation with `catch_unwind` (via `EnvUnowned::with_env`) and unwrap
664
+
/// any returned `Result` with an `ErrorPolicy` (such as `ThrowRuntimeExAndDefault`)
665
+
/// - Optionally generate a JNI export symbol for the method
666
+
///
667
+
/// Firstly, this macro always generates a `NativeMethod` struct with a compile-time guarantee that
668
+
/// the provided function pointer matches the JNI signature.
669
+
///
670
+
/// By default the native method implementation is automatically wrapped with a call to
671
+
/// `EnvUnowned::with_env` and any returned `Result` is unwrapped with an `ErrorPolicy` (default
672
+
/// `ThrowRuntimeExAndDefault`).
597
673
///
598
-
/// This macro generates a `NativeMethod` struct with a compile-time guarantee that the
599
-
/// provided function pointer matches the JNI signature.
674
+
/// If a `java_type` name is specified, it can also generate a JNI export symbol for the method.
600
675
///
601
-
/// By default the native method implementation is automatically wrapped with a
602
-
/// call to `EnvUnowned::with_env` and any returned `Result` is unwrapped with
603
-
/// an `ErrorPolicy` (default `ThrowRuntimeExAndDefault`).
676
+
/// This can be used as an alternative to the `bind_java_type!` macro if you only have a few native
677
+
/// methods to bind and offers stronger type safety than the `#[jni_mangle]` attribute macro.
604
678
///
605
-
/// It also optionally generates a JNI export symbol for the method.
679
+
/// The signature and type mappings syntax is compatible with the `jni_sig!` and `bind_java_type!`
680
+
/// macros which makes it easy to share type mapping definitions or migrate between them.
606
681
///
607
682
/// # Syntax
608
683
///
@@ -611,13 +686,13 @@ pub fn jni_mangle(
611
686
/// ```ignore
612
687
/// native_method! {
613
688
/// [jni = <path>,] // Override jni crate path (default: auto-detected, must come first)
614
-
/// [rust_type = <Type>,] // Type for 'this' parameter (default: JObject)
689
+
/// [rust_type = <Type>,] // Type for 'this' parameter, for instance methods (default: JObject)
615
690
/// [java_type = <Type>,] // Fully-qualified Java class name (required if export = true)
616
-
/// [name = "<methodName>",] // Java method name
691
+
/// [name = "<methodName>",] // Java method name (default: snake_case to lowerCamelCase of Rust fn name)
617
692
/// [type_map = { ... },] // Type mappings for custom types
618
693
/// [sig = (args) -> ret,] // JNI signature (see `jni_sig!` macro for syntax)
619
694
/// [static = true,] // Indicates static method with a `class` parameter instead of `this`
620
-
/// [export = true | "Java_name",] // Generate mangled JNI export symbol like `Java_package_Class_method` that JVM can resolve (requires `java_type`)
695
+
/// [export = true | "Java_name",] // Generate mangled JNI export symbol like `Java_package_Class_method` that JVM can resolve (requires `java_type` if true)
621
696
/// [fn = <function_path>,] // Path to Rust function
622
697
/// [error_policy = <Policy>,] // ErrorPolicy for unwrapping Result (default: ThrowRuntimeExAndDefault)
623
698
///
@@ -629,16 +704,24 @@ pub fn jni_mangle(
629
704
/// # Properties
630
705
///
631
706
/// - `jni` - Optional override for the jni crate path (must come first if provided)
632
-
/// - `rust_type` - Optional type for the `this` parameter (e.g., `MyType`). If omitted, uses `JObject`
633
-
/// - `java_type` - Fully-qualified Java class name, required in combination with `export = true` / `extern` native methods
634
-
/// - `name` - The Java method name as a string literal
707
+
/// - `rust_type` - Optional type for the `this` parameter (e.g., `MyType`). If omitted, uses
708
+
/// `JObject`
709
+
/// - `java_type` - Fully-qualified Java class name, required in combination with `export = true` /
710
+
/// `extern` native methods
711
+
/// - `name` - The Java method name as a string literal ( defaults to snake_case to lowerCamelCase
712
+
/// conversion of the Rust function name)
635
713
/// - `type_map` - Optional type mappings from Rust types to Java class names
636
714
/// - `sig` - The method signature (see [`jni_sig!`] macro for syntax)
637
-
/// - `fn` - Path to the Rust function that implements this native method (defaults to `RustType::method_name` if shorthand syntax is used)
638
-
/// - `static` - Indicates that this is a static method (emits a `class` parameter instead of `this`)
639
-
/// - `export` - If `true` or a string literal like `"Java_package_Class_method"`, generates a JNI export symbol for the method (requires `java_type`)
640
-
/// - `raw` - If specified, the function receives a raw `EnvUnowned` instead of `&mut Env`, with no `catch_unwind` wrapper and does not return a `Result`
641
-
/// - `error_policy` - The `ErrorPolicy` to use when unwrapping the `Result` returned by a non-raw implementation (default: `ThrowRuntimeExAndDefault`)
715
+
/// - `fn` - Path to the Rust function that implements this native method (defaults to
716
+
/// `RustType::method_name` if shorthand syntax is used)
717
+
/// - `static` - Indicates that this is a static method (emits a `class` parameter instead of
718
+
/// `this`)
719
+
/// - `export` - If `true` or a string literal like `"Java_package_Class_method"`, generates a JNI
720
+
/// export symbol for the method (requires `java_type`)
721
+
/// - `raw` - If specified, the function receives a raw `EnvUnowned` instead of `&mut Env`, with no
722
+
/// `catch_unwind` wrapper and does not return a `Result`
723
+
/// - `error_policy` - The `ErrorPolicy` to use when unwrapping the `Result` returned by a non-raw
0 commit comments