musikr: fix jni env borrows

This commit is contained in:
Alexander Capehart 2025-02-19 10:46:44 -07:00
parent 11ab27df97
commit ca169a9354
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
2 changed files with 32 additions and 45 deletions

View file

@ -240,13 +240,17 @@ impl<'local, 'file_ref> JMetadataBuilder<'local, 'file_ref> {
.borrow_mut() .borrow_mut()
.find_class("org/oxycblt/musikr/metadata/Metadata") .find_class("org/oxycblt/musikr/metadata/Metadata")
.unwrap(); .unwrap();
// Get the objects first since they need the env borrow first
let id3v2 = self.id3v2.get_object();
let xiph = self.xiph.get_object();
let mp4 = self.mp4.get_object();
self.env.borrow_mut().new_object( self.env.borrow_mut().new_object(
metadata_class, metadata_class,
"(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;[BLorg/oxycblt/musikr/metadata/Properties;)V", "(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;[BLorg/oxycblt/musikr/metadata/Properties;)V",
&[ &[
JValueGen::from(&self.id3v2.get_object()), JValueGen::from(&id3v2),
JValueGen::from(&self.xiph.get_object()), JValueGen::from(&xiph),
JValueGen::from(&self.mp4.get_object()), JValueGen::from(&mp4),
JValueGen::from(&cover_array), JValueGen::from(&cover_array),
JValueGen::from(&properties), JValueGen::from(&properties),
], ],

View file

@ -44,57 +44,40 @@ impl<'local> JTagMap<'local> {
} }
pub fn get_object(&self) -> JObject { pub fn get_object(&self) -> JObject {
let map_class = self let mut env = self.env.borrow_mut();
.env
.borrow_mut() let map_class = env.find_class("java/util/HashMap").unwrap();
.find_class("java/util/HashMap") let map = env.new_object(&map_class, "()V", &[]).unwrap();
.unwrap(); let array_list_class = env.find_class("java/util/ArrayList").unwrap();
let map = self
.env
.borrow_mut()
.new_object(&map_class, "()V", &[])
.unwrap();
for (key, values) in &self.map { for (key, values) in &self.map {
let j_key = self.env.borrow().new_string(key).unwrap(); let j_key = env.new_string(key).unwrap();
let j_values: Vec<JObject> = values
.iter()
.map(|v| self.env.borrow().new_string(v).unwrap().into())
.collect();
// Create ArrayList for values // Create ArrayList for values
let array_list_class = self let array_list = env.new_object(&array_list_class, "()V", &[]).unwrap();
.env
.borrow_mut()
.find_class("java/util/ArrayList")
.unwrap();
let array_list = self
.env
.borrow_mut()
.new_object(array_list_class, "()V", &[])
.unwrap();
// Convert all values to Java strings first
let j_values: Vec<JObject> = values
.iter()
.map(|v| env.new_string(v).unwrap().into())
.collect();
// Add all values to ArrayList
for value in j_values { for value in j_values {
self.env env.call_method(
.borrow_mut()
.call_method(
&array_list, &array_list,
"add", "add",
"(Ljava/lang/Object;)Z", "(Ljava/lang/Object;)Z",
&[JValueGen::from(&value)], &[JValueGen::from(&value)],
) ).unwrap();
.unwrap();
} }
self.env env.call_method(
.borrow_mut()
.call_method(
&map, &map,
"put", "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
&[JValueGen::from(&j_key), JValueGen::from(&array_list)], &[JValueGen::from(&j_key), JValueGen::from(&array_list)],
) ).unwrap();
.unwrap();
} }
map map