diff --git a/README.md b/README.md index e3b629a..16877df 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ let state = jvm.static_class("java.lang.Thread$State")?; ```rust let rust_vec = vec!["arg1", "arg2", "arg3", "arg33"]; -// Generate a Java List. The Java List implementation is the one that is returned by java.util.Arrays#asList +// Generate a Java List. The Java List implementation is the one that is returned by java.util.Arrays#asList let java_list_instance = jvm.java_list( JavaClass::String, rust_vec)?; @@ -124,7 +124,13 @@ let my_vec: Vec = vec![ "def".to_owned(), "ghi".to_owned()]; +// Creates List let i10 = InvocationArg::try_from(my_vec.as_slice())?; + +let another_vec: Vec = vec![0.0, 1.0, 3.6]; + +// Creates List +let i11 = InvocationArg::try_from(another_vec.as_slice())?; ``` The `j4rs` apis accept `InvocationArg`s either as references, or values: @@ -281,7 +287,7 @@ jvm.cast(&instance, "java.lang.Object")?; ### Java arrays and variadics ```rust -// Create a Java array of Strings +// Create a Java array of Strings `String []` let s1 = InvocationArg::try_from("string1")?; let s2 = InvocationArg::try_from("string2")?; let s3 = InvocationArg::try_from("string3")?; @@ -291,6 +297,24 @@ let arr_instance = jvm.create_java_array("java.lang.String", &[s1, s2, s3])?; let list_instance = jvm.invoke_static("java.util.Arrays", "asList", &[InvocationArg::from(arr_instance)])?; ``` +When creating an array of primitives, each item needs to be converted first. + +```rust +let doubles = vec![0.1, 466.5, 21.37]; + +// Creates Java `double` primitives within a Rust `Vec` +let double_args: Vec = doubles.iter() + .map(|value| Ok::<_, J4RsError>( + InvocationArg::try_from(value)?.into_primitive()? + )) + .collect::>()? + +// Creates an instance of `double []` +let doubles_array = InvocationArg::try_from( + jvm.create_java_array("double", &double_args)? +)?; +``` + ### Java Generics ```rust