Xuanwo commented on code in PR #2288:
URL: 
https://github.com/apache/incubator-opendal/pull/2288#discussion_r1200763421


##########
bindings/java/src/convert.rs:
##########
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use jni::errors::Result;
+use jni::objects::{JMap, JObject, JString, JThrowable, JValue};
+use jni::JNIEnv;
+use opendal::ErrorKind;
+use std::collections::HashMap;
+
+pub(crate) fn error_to_error<E>(error: E) -> opendal::Error
+where
+    E: Into<anyhow::Error> + ToString,
+{
+    opendal::Error::new(ErrorKind::Unexpected, 
&error.to_string()).set_source(error)
+}
+
+pub(crate) fn error_to_exception<'local>(
+    env: &mut JNIEnv<'local>,
+    error: opendal::Error,
+) -> Result<JThrowable<'local>> {
+    let class = env.find_class("org/apache/opendal/exception/ODException")?;
+
+    let code = env.new_string(match error.kind() {

Review Comment:
   Maybe we can use `error.kind().to_string()`?



##########
bindings/java/src/convert.rs:
##########
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use jni::errors::Result;
+use jni::objects::{JMap, JObject, JString, JThrowable, JValue};
+use jni::JNIEnv;
+use opendal::ErrorKind;
+use std::collections::HashMap;
+
+pub(crate) fn error_to_error<E>(error: E) -> opendal::Error
+where
+    E: Into<anyhow::Error> + ToString,
+{
+    opendal::Error::new(ErrorKind::Unexpected, 
&error.to_string()).set_source(error)
+}
+
+pub(crate) fn error_to_exception<'local>(

Review Comment:
   How about naming this API to `format_jni_exception`?



##########
bindings/java/src/convert.rs:
##########
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use jni::errors::Result;
+use jni::objects::{JMap, JObject, JString, JThrowable, JValue};
+use jni::JNIEnv;
+use opendal::ErrorKind;
+use std::collections::HashMap;
+
+pub(crate) fn error_to_error<E>(error: E) -> opendal::Error

Review Comment:
   The naming doesn't LGTM. And I don't like to implement functions upon 
`Into<anyhow::Error> + ToString` which can be used in wrong easily.
   
   How about implement `parse_jni_error(error: jni::errors::Error) -> 
opendal::Error`?



##########
bindings/java/src/lib.rs:
##########
@@ -75,263 +68,16 @@ pub unsafe extern "system" fn JNI_OnUnload(_: JavaVM, _: 
*mut c_void) {
     }
 }
 
-#[no_mangle]
-pub extern "system" fn Java_org_apache_opendal_Operator_newOperator(
-    mut env: JNIEnv,
-    _class: JClass,
-    input: JString,
-    params: JObject,
-) -> jlong {
-    let input: String = env
-        .get_string(&input)
-        .expect("cannot get java string")
-        .into();
-
-    let scheme = Scheme::from_str(&input).unwrap();
-
-    let map = convert_jmap_to_hashmap(&mut env, &params);
-    if let Ok(operator) = Operator::via_map(scheme, map) {
-        Box::into_raw(Box::new(operator)) as jlong
-    } else {
-        env.exception_clear().expect("cannot clear exception");
-        env.throw_new(
-            "java/lang/IllegalArgumentException",
-            "Unsupported operator.",
-        )
-        .expect("cannot throw exception");
-        0 as jlong
-    }
-}
-
-/// # Safety
-///
-/// This function should not be called before the Operator are ready.
-#[no_mangle]
-pub unsafe extern "system" fn Java_org_apache_opendal_Operator_writeAsync(
-    mut env: JNIEnv,
-    _class: JClass,
-    ptr: *mut Operator,
-    file: JString,
-    content: JString,
-) -> jobject {
-    let op = &mut *ptr;
-
-    let file: String = env.get_string(&file).unwrap().into();
-    let content: String = env.get_string(&content).unwrap().into();
-
-    let class = "java/util/concurrent/CompletableFuture";
-    let f = env.new_object(class, "()V", &[]).unwrap();
-
-    // keep the future alive, so that we can complete it later
-    // but this approach will be limited by global ref table size (65535)
-    let future = env.new_global_ref(&f).unwrap();
-
-    RUNTIME.get_unchecked().spawn(async move {
-        let result = op.write(&file, content).await;
-
-        let env = ENV.with(|cell| *cell.borrow_mut()).unwrap();
-        let mut env = JNIEnv::from_raw(env).unwrap();
-
-        match result {
-            Ok(()) => env
-                .call_method(
-                    future,
-                    "complete",
-                    "(Ljava/lang/Object;)Z",
-                    &[JValue::Object(&JObject::null())],
-                )
-                .unwrap(),
-            Err(err) => {
-                let exception = convert_error_to_exception(&mut env, 
err).unwrap();
-                env.call_method(
-                    future,
-                    "completeExceptionally",
-                    "(Ljava/lang/Throwable;)Z",
-                    &[JValue::Object(&exception)],
-                )
-                .unwrap()
-            }
+fn or_throw<T>(env: &mut JNIEnv, res: opendal::Result<T>) -> T

Review Comment:
   I prefer to define a java binding's own `Result` type and implement function 
upon it.
   
   And we don't need to add `T: Default` here, the code after 
`env.throw(exception).unwrap();` is unreachable:
   
   ```diff
   - Default::default()
   + unreachable!("error has been throwed")
   ```



##########
bindings/java/src/test/java/org/apache/opendal/ExceptionTest.java:
##########
@@ -30,23 +30,23 @@
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class ExceptionTest {
-    private Operator operator;
+    private BlockingOperator blockingOperator;

Review Comment:
   We can call this `op` for short.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to