Skip to content

fix: drop useless import and clippy happy for java binding #5746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci_bindings_java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ jobs:
run: |
./mvnw clean compile spotless:check

- name: Check Clippy
working-directory: bindings/java
run: cargo clippy -- -D warnings

test:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
4 changes: 1 addition & 3 deletions bindings/java/src/async_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// specific language governing permissions and limitations
// under the License.

use std::future::Future;
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -30,10 +29,9 @@ use jni::sys::jobject;
use jni::sys::jsize;
use jni::JNIEnv;
use opendal::layers::BlockingLayer;
use opendal::operator_futures::FutureWrite;
use opendal::raw::PresignedRequest;
use opendal::Operator;
use opendal::Scheme;
use opendal::{Metadata, Operator};

use crate::convert::jstring_to_string;
use crate::convert::{
Expand Down
8 changes: 4 additions & 4 deletions bindings/java/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub(crate) fn string_to_jstring<'a>(
)
}

pub(crate) fn get_optional_string_from_object<'a>(
env: &mut JNIEnv<'a>,
pub(crate) fn get_optional_string_from_object(
env: &mut JNIEnv<'_>,
obj: &JObject,
method: &str,
) -> crate::Result<Option<String>> {
Expand All @@ -84,8 +84,8 @@ pub(crate) fn get_optional_string_from_object<'a>(
}
}

pub(crate) fn get_optional_map_from_object<'a>(
env: &mut JNIEnv<'a>,
pub(crate) fn get_optional_map_from_object(
env: &mut JNIEnv<'_>,
obj: &JObject,
method: &str,
) -> crate::Result<Option<HashMap<String, String>>> {
Expand Down
26 changes: 15 additions & 11 deletions bindings/java/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::ffi::c_void;
use std::future::Future;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::OnceLock;
use std::thread::available_parallelism;

use jni::objects::JClass;
Expand All @@ -28,12 +29,11 @@ use jni::objects::JValue;
use jni::sys::jlong;
use jni::JNIEnv;
use jni::JavaVM;
use once_cell::sync::OnceCell;
use tokio::task::JoinHandle;

use crate::Result;

static mut RUNTIME: OnceCell<Executor> = OnceCell::new();
static RUNTIME: OnceLock<Executor> = OnceLock::new();
thread_local! {
static ENV: RefCell<Option<*mut jni::sys::JNIEnv>> = const { RefCell::new(None) };
}
Expand All @@ -42,9 +42,7 @@ thread_local! {
///
/// This function could be only called by java vm when unload this lib.
#[no_mangle]
pub unsafe extern "system" fn JNI_OnUnload(_: JavaVM, _: *mut c_void) {
let _ = RUNTIME.take();
}
pub unsafe extern "system" fn JNI_OnUnload(_: JavaVM, _: *mut c_void) {}

/// # Safety
///
Expand Down Expand Up @@ -186,10 +184,16 @@ pub(crate) fn executor_or_default<'a>(
///
/// This function could be only when the lib is loaded.
unsafe fn default_executor<'a>(env: &mut JNIEnv<'a>) -> Result<&'a Executor> {
RUNTIME.get_or_try_init(|| {
make_tokio_executor(
env,
available_parallelism().map(NonZeroUsize::get).unwrap_or(1),
)
})
// Return the executor if it's already initialized
if let Some(runtime) = RUNTIME.get() {
return Ok(runtime);
}

// Try to initialize the executor
let executor = make_tokio_executor(
env,
available_parallelism().map(NonZeroUsize::get).unwrap_or(1),
)?;

Ok(RUNTIME.get_or_init(|| executor))
}