GetFieldID should throw NoSuchFieldFound rather than NoClassDefFound

Change-Id: Id610b537a47898bb2202caf0e6a32138279b887b
diff --git a/src/jni_internal.cc b/src/jni_internal.cc
index 125401e..4cd809e 100644
--- a/src/jni_internal.cc
+++ b/src/jni_internal.cc
@@ -357,8 +357,12 @@
   }
   if (field_type == NULL) {
     // Failed to find type from the signature of the field.
-    // TODO: Linkage or NoSuchFieldError?
-    CHECK(ts.Self()->IsExceptionPending());
+    DCHECK(ts.Self()->IsExceptionPending());
+    ts.Self()->ClearException();
+    std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
+    ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
+        "no type \"%s\" found and so no field \"%s\" could be found in class "
+        "\"%s\" or its superclasses", sig, name, class_descriptor.c_str());
     return NULL;
   }
   if (is_static) {
@@ -367,9 +371,8 @@
     field = c->FindInstanceField(name, field_type);
   }
   if (field == NULL) {
-    Thread* self = Thread::Current();
     std::string class_descriptor(c->GetDescriptor()->ToModifiedUtf8());
-    self->ThrowNewException("Ljava/lang/NoSuchFieldError;",
+    ts.Self()->ThrowNewException("Ljava/lang/NoSuchFieldError;",
         "no \"%s\" field \"%s\" in class \"%s\" or its superclasses", sig,
         name, class_descriptor.c_str());
     return NULL;
diff --git a/src/jni_internal_test.cc b/src/jni_internal_test.cc
index e82b734..325d1ec 100644
--- a/src/jni_internal_test.cc
+++ b/src/jni_internal_test.cc
@@ -121,6 +121,11 @@
   EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
   EXPECT_EXCEPTION(jlnsfe);
 
+  // Wrong type where type doesn't exist.
+  fid = env_->GetFieldID(c, "count", "Lrod/jane/freddy;");
+  EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
+  EXPECT_EXCEPTION(jlnsfe);
+
   // Wrong name.
   fid = env_->GetFieldID(c, "Count", "I");
   EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
@@ -156,6 +161,11 @@
   EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
   EXPECT_EXCEPTION(jlnsfe);
 
+  // Wrong type where type doesn't exist.
+  fid = env_->GetStaticFieldID(c, "CASE_INSENSITIVE_ORDER", "Lrod/jane/freddy;");
+  EXPECT_EQ(static_cast<jfieldID>(NULL), fid);
+  EXPECT_EXCEPTION(jlnsfe);
+
   // Wrong name.
   fid = env_->GetStaticFieldID(c, "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
   EXPECT_EQ(static_cast<jfieldID>(NULL), fid);