jvmti_rs/wrapper/jvmtifns/
system_properties.rs1use std::{
2 os::raw::c_char,
3 ptr,
4};
5
6use jni::strings::JNIString;
7
8use crate::{errors::*, JVMTIEnv, slice_raw, sys::*};
9
10impl<'a> JVMTIEnv<'a> {
11 pub fn get_system_properties(&self) -> Result<Vec<String>> {
12 let mut count: jint = 0 as jint;
13 let mut properties: *mut *mut c_char = ptr::null_mut();
14
15 let res = jvmti_call_result!(self.jvmti_raw(), GetSystemProperties,
16 &mut count,
17 &mut properties
18 );
19 jvmti_error_code_to_result(res)?;
20
21 if count == 0 {
22 return Ok(vec![]);
23 }
24
25 let items = slice_raw(properties, count);
26 let mut strings: Vec<String> = Vec::with_capacity(count as usize);
27 for &item in items.iter() {
28 strings.push(self.build_string(item)?.into())
29 }
30 self.deallocate_raw(properties as jmemory)?;
31 Ok(strings)
32 }
33
34 pub fn get_system_property<S>(&self, property: S) -> Result<String>
35 where
36 S: Into<JNIString> {
37 let ffi_name = property.into();
38
39 let mut value = ptr::null_mut();
40 let res = jvmti_call_result!(self.jvmti_raw(), GetSystemProperty,
41 ffi_name.as_ptr(),
42 &mut value
43 );
44 jvmti_error_code_to_result(res)?;
45 Ok(self.build_string(value)?.into())
46 }
47
48 pub fn set_system_property<S>(&self, property: S, value: S) -> Result<()>
49 where
50 S: Into<JNIString> {
51 let ffi_name = property.into();
52 let ffi_value = value.into();
53 jvmti_call!(self.jvmti_raw(), SetSystemProperty,
54 ffi_name.as_ptr(),
55 ffi_value.as_ptr()
56 );
57 }
58}