diff --git a/pkgs/jni/src/dartjni.c b/pkgs/jni/src/dartjni.c index 5b2ac0c30..0c2bff26e 100644 --- a/pkgs/jni/src/dartjni.c +++ b/pkgs/jni/src/dartjni.c @@ -521,6 +521,8 @@ JniExceptionDetails getExceptionDetails(jthrowable exception) { JniExceptionDetails details; details.message = (*jniEnv)->CallObjectMethod( jniEnv, exception, exceptionMethods.toStringMethod); + // No exception is thrown from toString. + (*jniEnv)->ExceptionClear(jniEnv); jobject buffer = (*jniEnv)->NewObject(jniEnv, exceptionMethods.byteArrayOutputStreamClass, exceptionMethods.byteArrayOutputStreamCtor); @@ -529,8 +531,12 @@ JniExceptionDetails getExceptionDetails(jthrowable exception) { exceptionMethods.printStreamCtor, buffer); (*jniEnv)->CallVoidMethod( jniEnv, exception, exceptionMethods.printStackTraceMethod, printStream); + // No exception is thrown from printStackTrace. + (*jniEnv)->ExceptionClear(jniEnv); details.stacktrace = (*jniEnv)->CallObjectMethod( jniEnv, buffer, exceptionMethods.toStringMethod); + // No exception is thrown from toString. + (*jniEnv)->ExceptionClear(jniEnv); details.message = to_global_ref(details.message); details.stacktrace = to_global_ref(details.stacktrace); return details; diff --git a/pkgs/jni/test/boxed_test.dart b/pkgs/jni/test/boxed_test.dart index 261b1accc..d61938df6 100644 --- a/pkgs/jni/test/boxed_test.dart +++ b/pkgs/jni/test/boxed_test.dart @@ -13,7 +13,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/exception_test.dart b/pkgs/jni/test/exception_test.dart index 0c45ac8a6..bc2b89183 100644 --- a/pkgs/jni/test/exception_test.dart +++ b/pkgs/jni/test/exception_test.dart @@ -20,8 +20,7 @@ void main() { Jni.spawn(dylibDir: "wrong_dir"); } on HelperNotFoundError catch (_) { // stderr.write("\n$_\n"); - Jni.spawnIfNotExists( - dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); caught = true; } on JniVmExistsError { stderr.writeln('cannot verify: HelperNotFoundError thrown'); diff --git a/pkgs/jni/test/global_env_test.dart b/pkgs/jni/test/global_env_test.dart index 315225002..4f40eaec0 100644 --- a/pkgs/jni/test/global_env_test.dart +++ b/pkgs/jni/test/global_env_test.dart @@ -26,7 +26,7 @@ void main() { if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/isolate_test.dart b/pkgs/jni/test/isolate_test.dart index 9b54f02ee..a88d98c05 100644 --- a/pkgs/jni/test/isolate_test.dart +++ b/pkgs/jni/test/isolate_test.dart @@ -14,7 +14,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index c445e86cc..fd4d11906 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -13,7 +13,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jbyte_buffer_test.dart b/pkgs/jni/test/jbyte_buffer_test.dart index 2d4de825a..fae8a6d5b 100644 --- a/pkgs/jni/test/jbyte_buffer_test.dart +++ b/pkgs/jni/test/jbyte_buffer_test.dart @@ -14,7 +14,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jlist_test.dart b/pkgs/jni/test/jlist_test.dart index b410525be..329d9c249 100644 --- a/pkgs/jni/test/jlist_test.dart +++ b/pkgs/jni/test/jlist_test.dart @@ -13,7 +13,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jmap_test.dart b/pkgs/jni/test/jmap_test.dart index 4ece7517f..0c157567b 100644 --- a/pkgs/jni/test/jmap_test.dart +++ b/pkgs/jni/test/jmap_test.dart @@ -12,7 +12,7 @@ import 'test_util/test_util.dart'; void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jobject_test.dart b/pkgs/jni/test/jobject_test.dart index 1f1c41964..362e18243 100644 --- a/pkgs/jni/test/jobject_test.dart +++ b/pkgs/jni/test/jobject_test.dart @@ -17,7 +17,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jset_test.dart b/pkgs/jni/test/jset_test.dart index 2d93e6334..cfe8a7158 100644 --- a/pkgs/jni/test/jset_test.dart +++ b/pkgs/jni/test/jset_test.dart @@ -13,7 +13,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/jstring_test.dart b/pkgs/jni/test/jstring_test.dart index c1a968c46..a699e3090 100644 --- a/pkgs/jni/test/jstring_test.dart +++ b/pkgs/jni/test/jstring_test.dart @@ -13,7 +13,7 @@ void main() { // Don't forget to initialize JNI. if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/load_test.dart b/pkgs/jni/test/load_test.dart index 78e151177..27cc6ad71 100644 --- a/pkgs/jni/test/load_test.dart +++ b/pkgs/jni/test/load_test.dart @@ -32,7 +32,7 @@ void doGC() { void main() { if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jni/test/test_util/test_util.dart b/pkgs/jni/test/test_util/test_util.dart index fcdafad2c..841705f00 100644 --- a/pkgs/jni/test/test_util/test_util.dart +++ b/pkgs/jni/test/test_util/test_util.dart @@ -4,6 +4,7 @@ import 'dart:io'; +import 'package:jni/jni.dart'; import 'package:jni/src/build_util/build_util.dart'; typedef TestCaseCallback = void Function(); @@ -34,3 +35,8 @@ void checkDylibIsUpToDate() { exit(1); } } + +void spawnJvm() { + Jni.spawnIfNotExists( + dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m", "-Xcheck:jni"]); +} diff --git a/pkgs/jni/test/type_test.dart b/pkgs/jni/test/type_test.dart index 2962b09e9..f00c47103 100644 --- a/pkgs/jni/test/type_test.dart +++ b/pkgs/jni/test/type_test.dart @@ -205,7 +205,7 @@ final class $FType extends JObjType { void main() { if (!Platform.isAndroid) { checkDylibIsUpToDate(); - Jni.spawnIfNotExists(dylibDir: "build/jni_libs", jvmOptions: ["-Xmx128m"]); + spawnJvm(); } run(testRunner: test); } diff --git a/pkgs/jnigen/test/test_util/bindings_test_setup.dart b/pkgs/jnigen/test/test_util/bindings_test_setup.dart index 0c9d723d2..2800aa1e7 100644 --- a/pkgs/jnigen/test/test_util/bindings_test_setup.dart +++ b/pkgs/jnigen/test/test_util/bindings_test_setup.dart @@ -67,6 +67,8 @@ Future bindingsTestSetup() async { tempClassDir.path, ...jacksonJars, kotlinTestJar, + ], jvmOptions: [ + "-Xcheck:jni", ]); } }