Skip to content

Commit 9f44a74

Browse files
Emil Ejbyfeldtviirya
andauthored
refactor: Remove method get_global_jclass (apache#580)
* refactor: Remove method get_global_jclass The method uses unsafe code to cast a value to a static lifetime using std::mem::transmute. But none of users of the method actually seems to depend on this lifetime extention. So to me it seems better to just call the underlying `find_class` method and delete the unsafe code. * Apply suggestions from code review Remove no longer needed comments. Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com> --------- Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
1 parent 28309a4 commit 9f44a74

File tree

5 files changed

+6
-30
lines changed

5 files changed

+6
-30
lines changed

core/src/jvm_bridge/batch_iterator.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use super::get_global_jclass;
1918
use jni::{
2019
errors::Result as JniResult,
2120
objects::{JClass, JMethodID},
@@ -34,8 +33,7 @@ impl<'a> CometBatchIterator<'a> {
3433
pub const JVM_CLASS: &'static str = "org/apache/comet/CometBatchIterator";
3534

3635
pub fn new(env: &mut JNIEnv<'a>) -> JniResult<CometBatchIterator<'a>> {
37-
// Get the global class reference
38-
let class = get_global_jclass(env, Self::JVM_CLASS)?;
36+
let class = env.find_class(Self::JVM_CLASS)?;
3937

4038
Ok(CometBatchIterator {
4139
class,

core/src/jvm_bridge/comet_exec.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use jni::{
2222
JNIEnv,
2323
};
2424

25-
use super::get_global_jclass;
26-
2725
/// A struct that holds all the JNI methods and fields for JVM CometExec object.
2826
pub struct CometExec<'a> {
2927
pub class: JClass<'a>,
@@ -55,8 +53,7 @@ impl<'a> CometExec<'a> {
5553
pub const JVM_CLASS: &'static str = "org/apache/spark/sql/comet/CometScalarSubquery";
5654

5755
pub fn new(env: &mut JNIEnv<'a>) -> JniResult<CometExec<'a>> {
58-
// Get the global class reference
59-
let class = get_global_jclass(env, Self::JVM_CLASS)?;
56+
let class = env.find_class(Self::JVM_CLASS)?;
6057

6158
Ok(CometExec {
6259
method_get_bool: env

core/src/jvm_bridge/comet_metric_node.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use jni::{
2222
JNIEnv,
2323
};
2424

25-
use super::get_global_jclass;
26-
2725
/// A struct that holds all the JNI methods and fields for JVM CometMetricNode class.
2826
pub struct CometMetricNode<'a> {
2927
pub class: JClass<'a>,
@@ -37,8 +35,7 @@ impl<'a> CometMetricNode<'a> {
3735
pub const JVM_CLASS: &'static str = "org/apache/spark/sql/comet/CometMetricNode";
3836

3937
pub fn new(env: &mut JNIEnv<'a>) -> JniResult<CometMetricNode<'a>> {
40-
// Get the global class reference
41-
let class = get_global_jclass(env, Self::JVM_CLASS)?;
38+
let class = env.find_class(Self::JVM_CLASS)?;
4239

4340
Ok(CometMetricNode {
4441
method_get_child_node: env

core/src/jvm_bridge/comet_task_memory_manager.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use jni::{
2222
JNIEnv,
2323
};
2424

25-
use crate::jvm_bridge::get_global_jclass;
26-
2725
/// A wrapper which delegate acquire/release memory calls to the
2826
/// JVM side `CometTaskMemoryManager`.
2927
#[derive(Debug)]
@@ -40,7 +38,7 @@ impl<'a> CometTaskMemoryManager<'a> {
4038
pub const JVM_CLASS: &'static str = "org/apache/spark/CometTaskMemoryManager";
4139

4240
pub fn new(env: &mut JNIEnv<'a>) -> JniResult<CometTaskMemoryManager<'a>> {
43-
let class = get_global_jclass(env, Self::JVM_CLASS)?;
41+
let class = env.find_class(Self::JVM_CLASS)?;
4442

4543
let result = CometTaskMemoryManager {
4644
class,

core/src/jvm_bridge/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
use crate::errors::CometResult;
2121

2222
use jni::{
23-
errors::{Error, Result as JniResult},
24-
objects::{JClass, JMethodID, JObject, JString, JThrowable, JValueGen, JValueOwned},
23+
errors::Error,
24+
objects::{JMethodID, JObject, JString, JThrowable, JValueGen, JValueOwned},
2525
signature::ReturnType,
2626
AttachGuard, JNIEnv,
2727
};
@@ -176,20 +176,6 @@ pub(crate) use jni_new_string;
176176
pub(crate) use jni_static_call;
177177
pub(crate) use jvalues;
178178

179-
/// Gets a global reference to a Java class.
180-
pub fn get_global_jclass(env: &mut JNIEnv, cls: &str) -> JniResult<JClass<'static>> {
181-
let local_jclass = env.find_class(cls)?;
182-
let global = env.new_global_ref::<JObject>(local_jclass.into())?;
183-
184-
// A hack to make the `JObject` static. This is safe because the global reference is never
185-
// gc-ed by the JVM before dropping the global reference.
186-
let global_obj = unsafe { std::mem::transmute::<_, JObject<'static>>(global.as_obj()) };
187-
// Prevent the global reference from being dropped.
188-
let _ = std::mem::ManuallyDrop::new(global);
189-
190-
Ok(JClass::from(global_obj))
191-
}
192-
193179
mod comet_exec;
194180
pub use comet_exec::*;
195181
mod batch_iterator;

0 commit comments

Comments
 (0)