Skip to content

Commit

Permalink
Merge pull request #1456 from JakuJ/cargo-fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fpoli authored Sep 27, 2023
2 parents 24bd4c5 + dd13d6c commit 0631c11
Show file tree
Hide file tree
Showing 40 changed files with 2,822 additions and 2,334 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
shared-key: "shared"
- name: Check and report Clippy errors
run: |
python ./x.py clippy -- -D warnings
python ./x.py clippy-all
# Detect missing dependencies in workspace packages
# See: https://stackoverflow.com/a/74293494/2491528
Expand Down
40 changes: 24 additions & 16 deletions jni-gen/systest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@ fn main() {
.use_jar(&asm_jar)
.wrap(java_class!("java.lang.Object"))
.wrap_all(vec![
java_class!("java.lang.Integer", vec![
constructor!("(I)V"),
field!("value"),
method!("compareTo", "(Ljava/lang/Integer;)I"),
]),
java_class!("java.util.Arrays", vec![
method!("binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I"),
]),
java_class!("java.lang.Error", vec![
constructor!("(Ljava/lang/String;)V"),
method!("getMessage"),
field!("detailMessage"),
]),
java_class!("java.math.BigInteger", vec![
method!("probablePrime"),
]),
java_class!(
"java.lang.Integer",
vec![
constructor!("(I)V"),
field!("value"),
method!("compareTo", "(Ljava/lang/Integer;)I"),
]
),
java_class!(
"java.util.Arrays",
vec![method!(
"binarySearch",
"([Ljava/lang/Object;Ljava/lang/Object;)I"
),]
),
java_class!(
"java.lang.Error",
vec![
constructor!("(Ljava/lang/String;)V"),
method!("getMessage"),
field!("detailMessage"),
]
),
java_class!("java.math.BigInteger", vec![method!("probablePrime"),]),
])
.generate(&generated_dir)
.unwrap_or_else(|e| {
Expand Down
30 changes: 13 additions & 17 deletions jni-gen/systest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#[rustfmt::skip]
#[path = "../gen/mod.rs"]
pub mod wrappers;

use jni::JNIEnv;
use jni::JavaVM;
use jni::InitArgsBuilder;
use jni::JNIVersion;
use jni::errors::Result;
use jni::{errors::Result, InitArgsBuilder, JNIEnv, JNIVersion, JavaVM};
use std::sync::Once;

pub fn print_exception(env: &JNIEnv) {
let exception_occurred = env.exception_check().unwrap_or_else(|e| panic!("{e:?}"));
if exception_occurred {
env.exception_describe()
.unwrap_or_else(|e| panic!("{e:?}"));
env.exception_describe().unwrap_or_else(|e| panic!("{e:?}"));
}
}

Expand All @@ -25,19 +21,19 @@ static mut JVM: Option<JavaVM> = None;
fn init_jvm() -> Result<()> {
INIT_JVM.call_once(|| {
let jvm_args = InitArgsBuilder::new()
.version(JNIVersion::V8)
.option("-Xcheck:jni")
.option("-Xdebug")
.option("-XX:+CheckJNICalls")
.build()
.unwrap_or_else(|e| {
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
.version(JNIVersion::V8)
.option("-Xcheck:jni")
.option("-Xdebug")
.option("-XX:+CheckJNICalls")
.build()
.unwrap_or_else(|e| {
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});

let jvm = JavaVM::new(jvm_args).unwrap_or_else(|e| {
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});

unsafe {
JVM = Some(jvm);
}
Expand All @@ -54,4 +50,4 @@ pub fn get_jvm() -> Result<&'static JavaVM> {
panic!("Failed to get JVM reference")
}
}
}
}
11 changes: 6 additions & 5 deletions jni-gen/systest/tests/constructor_check_fails.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use jni::objects::JObject;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;
use systest::{get_jvm, print_exception, wrappers::*};

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/String, but got java/lang/Integer instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/String, but got java/lang/Integer instead"
)]
fn constructor_should_fail_on_wrong_argument() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -18,7 +18,8 @@ fn constructor_should_fail_on_wrong_argument() {
let error_wrapper = java::lang::Error::with(&env);
let _result = error_wrapper.new(integer_object);
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
Expand Down
11 changes: 6 additions & 5 deletions jni-gen/systest/tests/field_getter_check_fails.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use jni::objects::JObject;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;
use systest::{get_jvm, print_exception, wrappers::*};

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead"
)]
fn field_getter_should_fail_on_wrong_receiver() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -18,7 +18,8 @@ fn field_getter_should_fail_on_wrong_receiver() {
let integer_object = java::lang::Integer::with(&env).new(1337)?;
let _result = error_wrapper.get_detailMessage(integer_object);
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
Expand Down
28 changes: 14 additions & 14 deletions jni-gen/systest/tests/field_setter_check_fails.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use jni::objects::JObject;
use jni::JNIEnv;
use jni::errors::Result as JNIResult;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;

use jni::{errors::Result as JNIResult, objects::JObject, JNIEnv};
use systest::{get_jvm, print_exception, wrappers::*};

fn string_to_jobject<'a>(env: &JNIEnv<'a>, string: &str) -> JNIResult<JObject<'a>> {
Ok(JObject::from(env.new_string(string.to_owned())?))
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead"
)]
fn field_setter_should_fail_on_wrong_receiver() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -23,19 +20,21 @@ fn field_setter_should_fail_on_wrong_receiver() {
env.with_local_frame(16, || {
let error_wrapper = java::lang::Error::with(&env);
let integer_object = java::lang::Integer::with(&env).new(1337)?;
error_wrapper.set_detailMessage(integer_object, string_to_jobject(&env, "error message")?)?;
error_wrapper
.set_detailMessage(integer_object, string_to_jobject(&env, "error message")?)?;
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
}



#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/String, but got java/lang/Integer instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/String, but got java/lang/Integer instead"
)]
fn field_setter_should_fail_on_wrong_argument() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -49,7 +48,8 @@ fn field_setter_should_fail_on_wrong_argument() {
let integer_object = java::lang::Integer::with(&env).new(1337)?;
error_wrapper.set_detailMessage(error_object, integer_object)?;
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
Expand Down
42 changes: 22 additions & 20 deletions jni-gen/systest/tests/jvm_builtin_classes.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use jni::objects::JObject;
use jni::JNIEnv;
use jni::objects::JString;
use jni::errors::Result as JNIResult;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;
use jni::{
errors::Result as JNIResult,
objects::{JObject, JString},
JNIEnv,
};
use systest::{get_jvm, print_exception, wrappers::*};

fn string_to_jobject<'a>(env: &JNIEnv<'a>, string: &str) -> JNIResult<JObject<'a>> {
Ok(JObject::from(env.new_string(string.to_owned())?))
Expand All @@ -27,15 +26,12 @@ fn test_jvm_builtin_classes() {
env.with_local_frame(16, || {
let integer_value = java::lang::Integer::with(&env).new(int_value)?;

let int_array = env.new_object_array(
array_length,
"java/lang/Integer",
integer_value,
)?;
let int_array =
env.new_object_array(array_length, "java/lang/Integer", integer_value)?;
let int_array = unsafe { JObject::from_raw(int_array) };

let result = java::util::Arrays::with(&env)
.call_binarySearch(int_array, integer_value)?;
let result =
java::util::Arrays::with(&env).call_binarySearch(int_array, integer_value)?;

assert!(0 <= result && result < array_length);

Expand All @@ -57,31 +53,37 @@ fn test_jvm_builtin_classes() {
assert!(java::lang::Integer::with(&env).get_value(integer_value)? == new_wal);

Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
}

env.with_local_frame(16, || {
let error_wrapper = java::lang::Error::with(&env);

// Initalize error and check its content
let innitial_message = "First error message".to_string();
let error = error_wrapper.new(string_to_jobject(&env, &innitial_message)?)?;
assert!(jobject_to_string(&env, error_wrapper.get_detailMessage(error)?)? == innitial_message);
assert!(
jobject_to_string(&env, error_wrapper.get_detailMessage(error)?)? == innitial_message
);

// Update the error object and check that the content has changed accordingly
let another_message = "Second message".to_string();
error_wrapper.set_detailMessage(error, string_to_jobject(&env, &another_message)?)?;
assert!(jobject_to_string(&env, error_wrapper.get_detailMessage(error)?)? == another_message);
assert!(
jobject_to_string(&env, error_wrapper.get_detailMessage(error)?)? == another_message
);

// Try calling the getMessage method to achieve the same result
assert!(jobject_to_string(&env, error_wrapper.call_getMessage(error)?)? == another_message);

Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
});
}
22 changes: 12 additions & 10 deletions jni-gen/systest/tests/method_runtime_check_fails.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use jni::objects::JObject;
use jni::JNIEnv;
use jni::errors::Result as JNIResult;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;
use jni::{errors::Result as JNIResult, objects::JObject, JNIEnv};
use systest::{get_jvm, print_exception, wrappers::*};

fn string_to_jobject<'a>(env: &JNIEnv<'a>, string: &str) -> JNIResult<JObject<'a>> {
Ok(JObject::from(env.new_string(string.to_owned())?))
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/Error, but got java/lang/Integer instead"
)]
fn method_should_fail_on_wrong_receiver() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -24,15 +22,18 @@ fn method_should_fail_on_wrong_receiver() {
let integer_object = java::lang::Integer::with(&env).new(1337)?;
error_wrapper.call_getMessage(integer_object)?;
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/lang/Integer, but got java/lang/Error instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/lang/Integer, but got java/lang/Error instead"
)]
fn method_should_fail_on_wrong_argument() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -47,7 +48,8 @@ fn method_should_fail_on_wrong_argument() {
let error_object = error_wrapper.new(string_to_jobject(&env, "error message")?)?;
let _result = integer_wrapper.call_compareTo(integer_object, error_object);
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
Expand Down
15 changes: 7 additions & 8 deletions jni-gen/systest/tests/static_method_runtime_check_fails.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use jni::objects::JObject;
use jni::JNIEnv;
use jni::errors::Result as JNIResult;
use systest::get_jvm;
use systest::print_exception;
use systest::wrappers::*;
use jni::{errors::Result as JNIResult, objects::JObject, JNIEnv};
use systest::{get_jvm, print_exception, wrappers::*};

fn string_to_jobject<'a>(env: &JNIEnv<'a>, string: &str) -> JNIResult<JObject<'a>> {
Ok(JObject::from(env.new_string(string.to_owned())?))
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Java binding type failure. Expected object of class java/util/Random, but got java/lang/Error instead")]
#[should_panic(
expected = "Java binding type failure. Expected object of class java/util/Random, but got java/lang/Error instead"
)]
fn static_method_should_fail_on_wrong_receiver() {
let jvm = get_jvm().expect("failed go get jvm reference");

Expand All @@ -25,7 +23,8 @@ fn static_method_should_fail_on_wrong_receiver() {
let error_object = error_wrapper.new(string_to_jobject(&env, "error message")?)?;
let _result = big_integer_wrapper.call_probablePrime(1337, error_object);
Ok(JObject::null())
}).unwrap_or_else(|e| {
})
.unwrap_or_else(|e| {
print_exception(&env);
panic!("{} source: {:?}", e, std::error::Error::source(&e));
});
Expand Down
Loading

0 comments on commit 0631c11

Please sign in to comment.