jvmti_rs/wrapper/jvmtifns/
method.rs

1use std::os::raw::c_char;
2use std::ptr;
3
4use jni::strings::JNIString;
5
6use crate::{AdapterTransform, builder::*, errors::*, JMethodName, JSignature, JVMTIEnv, objects::*, sys::*, to_bool};
7use crate::sys;
8
9impl<'a> JVMTIEnv<'a> {
10    pub fn set_native_method_prefix<S>(&self, prefix: S) -> Result<()>
11        where
12            S: Into<JNIString> {
13        let value = prefix.into();
14        jvmti_call!(self.jvmti_raw(), SetNativeMethodPrefix,
15            value.as_ptr()
16        )
17    }
18
19    pub fn set_native_method_prefixes<S>(&self, prefixes: &Vec<S>) -> Result<()>
20        where
21            S: Into<JNIString> + AsRef<str> {
22        if prefixes.is_empty() {
23            return Ok(());
24        }
25
26        let mut prefixes: Vec<*mut c_char> = prefixes.iter().map(|e| {
27            let prefix: JNIString = e.into();
28            prefix.as_ptr() as *mut c_char
29        }).collect();
30
31        jvmti_call!(self.jvmti_raw(), SetNativeMethodPrefixes,
32            prefixes.len() as jint,
33            prefixes.as_mut_ptr()
34        )
35    }
36
37    pub fn get_method_name<M>(&self, method: M) -> Result<JMethodName>
38        where
39            M: AdapterTransform<jmethodID> {
40        let mut name = ptr::null_mut();
41        let mut signature = ptr::null_mut();
42        let mut generic = ptr::null_mut();
43
44        let res = jvmti_call_result!(self.jvmti_raw(), GetMethodName,
45            method.transform(),
46            &mut name,
47            &mut signature,
48            &mut generic
49        );
50        jvmti_error_code_to_result(res)?;
51
52        let signature = JSignature::new(self.build_string(signature)?, self.build_string(generic)?)?;
53        Ok(JMethodName::new(self.build_string(name)?, signature))
54    }
55
56    pub fn get_method_declaring_class<M>(&self, method: M) -> Result<JObject>
57        where
58            M: AdapterTransform<jmethodID> {
59        let mut value_ptr: jclass = ptr::null_mut();
60        let res = jvmti_call_result!(self.jvmti_raw(), GetMethodDeclaringClass,
61            method.transform(),
62            &mut value_ptr
63        );
64        jvmti_error_code_to_result(res)?;
65        Ok(value_ptr.into())
66    }
67
68    pub fn get_method_modifiers<M>(&self, method: M) -> Result<jint>
69        where
70            M: AdapterTransform<jmethodID> {
71        Ok(jvmti_call_number_result!(self.jvmti_raw(), jint,
72            GetMethodModifiers,
73            method.transform()
74        ))
75    }
76
77    pub fn get_max_locals<M>(&self, method: M) -> Result<jint>
78        where
79            M: AdapterTransform<jmethodID> {
80        Ok(jvmti_call_number_result!(self.jvmti_raw(), jint,
81            GetMaxLocals,
82            method.transform()
83        ))
84    }
85
86    pub fn get_arguments_size<M>(&self, method: M) -> Result<jint>
87        where
88            M: AdapterTransform<jmethodID> {
89        Ok(jvmti_call_number_result!(self.jvmti_raw(), jint,
90            GetArgumentsSize,
91            method.transform()
92        ))
93    }
94
95    pub fn get_line_number_table<M>(&self, method: M) -> Result<Vec<JLineNumberEntry>>
96        where
97            M: AdapterTransform<jmethodID> {
98        let mut builder: MutAutoDeallocateObjectArrayBuilder<jvmtiLineNumberEntry> = MutAutoDeallocateObjectArrayBuilder::new();
99        let res = jvmti_call_result!( self.jvmti_raw(), GetLineNumberTable,
100            method.transform(),
101            &mut builder.count,
102            &mut builder.items
103        );
104        jvmti_error_code_to_result(res)?;
105        Ok(builder.build(self))
106    }
107
108    pub fn get_method_location<M>(&self, method: M) -> Result<JMethodLocation>
109        where
110            M: AdapterTransform<jmethodID> {
111        let mut start_location: sys::jlocation = 0 as sys::jlocation;
112        let mut end_location: sys::jlocation = 0 as sys::jlocation;
113        let res = jvmti_call_result!(self.jvmti_raw(), GetMethodLocation,
114            method.transform(),
115            &mut start_location,
116            &mut end_location
117        );
118        jvmti_error_code_to_result(res)?;
119        Ok(JMethodLocation::new(start_location, end_location))
120    }
121
122    pub fn get_local_variable_table<M>(&self, method: M) -> Result<Vec<JLocalVariableEntry>>
123        where
124            M: AdapterTransform<jmethodID> {
125        let mut builder: MutAutoDeallocateObjectArrayBuilder<jvmtiLocalVariableEntry> = MutAutoDeallocateObjectArrayBuilder::new();
126        let res = jvmti_call_result!( self.jvmti_raw(), GetLocalVariableTable,
127            method.transform(),
128            &mut builder.count,
129            &mut builder.items
130        );
131        jvmti_error_code_to_result(res)?;
132        Ok(builder.build(self))
133    }
134
135    pub fn get_bytecodes<M>(&self, method: M) -> Result<JMemoryAllocate>
136        where
137            M: AdapterTransform<jmethodID> {
138        let mut bytecode_count: jint = 0 as jint;
139        let mut bytecodes_ptr: jmemory = ptr::null_mut();
140        let res = jvmti_call_result!(self.jvmti_raw(), GetBytecodes,
141            method.transform(),
142            &mut bytecode_count,
143            &mut bytecodes_ptr
144        );
145        jvmti_error_code_to_result(res)?;
146        Ok(JMemoryAllocate::new(bytecodes_ptr, bytecode_count as jlong, self))
147    }
148
149    pub fn is_method_native<M>(&self, method: M) -> Result<bool>
150        where
151            M: AdapterTransform<jmethodID> {
152        let res = jvmti_call_number_result!(self.jvmti_raw(), jboolean,
153            IsMethodNative,
154            method.transform()
155        );
156        Ok(to_bool(res))
157    }
158
159    pub fn is_method_synthetic<M>(&self, method: M) -> Result<bool>
160        where
161            M: AdapterTransform<jmethodID> {
162        let res = jvmti_call_number_result!(self.jvmti_raw(), jboolean,
163            IsMethodSynthetic,
164            method.transform()
165        );
166        Ok(to_bool(res))
167    }
168
169    pub fn is_method_obsolete<M>(&self, method: M) -> Result<bool>
170        where
171            M: AdapterTransform<jmethodID> {
172        let res = jvmti_call_number_result!(self.jvmti_raw(), jboolean,
173            IsMethodObsolete,
174            method.transform()
175        );
176        Ok(to_bool(res))
177    }
178}