Add -Xcheck:jni.
I think this is as complete as possible right now. The remaining
two #if 0 sections require:
1. a way to get the Method* of the current native method.
2. a way to get the Class* of the type of a given Field*.
Change-Id: I331586022095fb36ccc10c9ac1890a59a9224d01
diff --git a/src/jni_compiler_test.cc b/src/jni_compiler_test.cc
index 9129162..4cd32e8 100644
--- a/src/jni_compiler_test.cc
+++ b/src/jni_compiler_test.cc
@@ -353,27 +353,35 @@
int gExceptionHandler_calls;
void ExceptionHandler(Method** frame) {
- EXPECT_TRUE((*frame)->GetName()->Equals("foo"));
+ EXPECT_TRUE((*frame)->GetName()->Equals("throwException"));
gExceptionHandler_calls++;
Thread::Current()->ClearException();
}
-TEST_F(JniCompilerTest, ExceptionHandling) {
- SetupForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClass_foo));
- Thread::Current()->RegisterExceptionEntryPoint(&ExceptionHandler);
+void Java_MyClass_throwException(JNIEnv* env, jobject) {
+ jclass c = env->FindClass("java/lang/RuntimeException");
+ env->ThrowNew(c, "hello");
+}
+TEST_F(JniCompilerTest, ExceptionHandling) {
+ Thread::Current()->RegisterExceptionEntryPoint(&ExceptionHandler);
+ gExceptionHandler_calls = 0;
gJava_MyClass_foo_calls = 0;
+
+ SetupForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClass_foo));
env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
EXPECT_EQ(1, gJava_MyClass_foo_calls);
EXPECT_EQ(0, gExceptionHandler_calls);
- // TODO: create a real exception here
- Thread::Current()->SetException(reinterpret_cast<Throwable*>(jobj_));
+
+ SetupForTest(false, "throwException", "()V", reinterpret_cast<void*>(&Java_MyClass_throwException));
+ env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
+ EXPECT_EQ(1, gJava_MyClass_foo_calls);
+ EXPECT_EQ(1, gExceptionHandler_calls);
+
+ SetupForTest(false, "foo", "()V", reinterpret_cast<void*>(&Java_MyClass_foo));
env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
EXPECT_EQ(2, gJava_MyClass_foo_calls);
EXPECT_EQ(1, gExceptionHandler_calls);
- env_->CallNonvirtualVoidMethod(jobj_, jklass_, jmethod_);
- EXPECT_EQ(3, gJava_MyClass_foo_calls);
- EXPECT_EQ(1, gExceptionHandler_calls);
}
jint Java_MyClass_nativeUpCall(JNIEnv* env, jobject thisObj, jint i) {