Fix JNI GetFieldId() signature check for "".
Test: Add a test to 647-jni-get-field-id
Change-Id: I8581997cb60057798fe4389531bf285baa076920
diff --git a/runtime/jni/jni_internal.cc b/runtime/jni/jni_internal.cc
index f8d0ef1..c032650 100644
--- a/runtime/jni/jni_internal.cc
+++ b/runtime/jni/jni_internal.cc
@@ -311,7 +311,9 @@
ArtField* field = nullptr;
ObjPtr<mirror::Class> field_type;
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
- if (sig[1] != '\0') {
+ if (UNLIKELY(sig[0] == '\0')) {
+ DCHECK(field == nullptr);
+ } else if (sig[1] != '\0') {
Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader()));
field_type = class_linker->FindClass(soa.Self(), sig, class_loader);
} else {
@@ -319,7 +321,7 @@
}
if (field_type == nullptr) {
// Failed to find type from the signature of the field.
- DCHECK(soa.Self()->IsExceptionPending());
+ DCHECK(sig[0] == '\0' || soa.Self()->IsExceptionPending());
StackHandleScope<1> hs2(soa.Self());
Handle<mirror::Throwable> cause(hs2.NewHandle(soa.Self()->GetException()));
soa.Self()->ClearException();
@@ -328,7 +330,9 @@
"no type \"%s\" found and so no field \"%s\" "
"could be found in class \"%s\" or its superclasses", sig, name,
c->GetDescriptor(&temp));
- soa.Self()->GetException()->SetCause(cause.Get());
+ if (cause != nullptr) {
+ soa.Self()->GetException()->SetCause(cause.Get());
+ }
return nullptr;
}
std::string temp;
diff --git a/test/647-jni-get-field-id/expected.txt b/test/647-jni-get-field-id/expected.txt
index 9506dd7..5fdc1cc 100644
--- a/test/647-jni-get-field-id/expected.txt
+++ b/test/647-jni-get-field-id/expected.txt
@@ -1,4 +1,6 @@
JNI_OnLoad called
+getFieldId(class TestClass, "intField", "")
+Caught java.lang.NoSuchFieldError
getFieldId(class TestClass, "intField", "I")
Result: true
getFieldId(class TestClass, "intField", "int")
diff --git a/test/647-jni-get-field-id/src/Main.java b/test/647-jni-get-field-id/src/Main.java
index 590ee8a..1f5cac2 100644
--- a/test/647-jni-get-field-id/src/Main.java
+++ b/test/647-jni-get-field-id/src/Main.java
@@ -21,6 +21,7 @@
public static void main(String[] args) {
System.loadLibrary(args[0]);
+ testGetFieldId(TestClass.class, "intField", "");
testGetFieldId(TestClass.class, "intField", "I");
testGetFieldId(TestClass.class, "intField", "int");
testGetFieldId(TestClass.class, "intField", "Lint;");