Also print class for NoSuchFieldException
Example message:
Caused by: java.lang.NoSuchFieldException: No field value in class Ljava/lang/String;
Added test.
(cherry picked from commit 3beb245da9392818e3154d47593f82cf0ef69aac)
Bug: 20881251
Bug: 21027454
Change-Id: I4043cbf26c3077952b6c151da0d0edd980da26b1
diff --git a/runtime/common_throws.cc b/runtime/common_throws.cc
index 0808999..b401066 100644
--- a/runtime/common_throws.cc
+++ b/runtime/common_throws.cc
@@ -283,8 +283,7 @@
// NoSuchFieldError
void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
- const StringPiece& type, const StringPiece& name)
- SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+ const StringPiece& type, const StringPiece& name) {
std::ostringstream msg;
std::string temp;
msg << "No " << scope << "field " << name << " of type " << type
@@ -292,6 +291,13 @@
ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
}
+void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
+ std::ostringstream msg;
+ std::string temp;
+ msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
+ ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
+}
+
// NoSuchMethodError
void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
diff --git a/runtime/common_throws.h b/runtime/common_throws.h
index df95cf9..49890e2 100644
--- a/runtime/common_throws.h
+++ b/runtime/common_throws.h
@@ -149,6 +149,9 @@
const StringPiece& type, const StringPiece& name)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name)
+ SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
// NoSuchMethodError
void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index a779e97..795a0ea 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -252,7 +252,7 @@
std::string name_str = name_string->ToModifiedUtf8();
// We may have a pending exception if we failed to resolve.
if (!soa.Self()->IsExceptionPending()) {
- soa.Self()->ThrowNewException("Ljava/lang/NoSuchFieldException;", name_str.c_str());
+ ThrowNoSuchFieldException(DecodeClass(soa, javaThis), name_str.c_str());
}
return nullptr;
}
diff --git a/test/046-reflect/src/Main.java b/test/046-reflect/src/Main.java
index 59f7001..0d8e576 100644
--- a/test/046-reflect/src/Main.java
+++ b/test/046-reflect/src/Main.java
@@ -233,6 +233,20 @@
field.set(instance, null);
/*
+ * Try getDeclaredField on a non-existant field.
+ */
+ try {
+ field = target.getDeclaredField("nonExistant");
+ System.out.println("ERROR: Expected NoSuchFieldException");
+ } catch (NoSuchFieldException nsfe) {
+ String msg = nsfe.getMessage();
+ if (!msg.contains("Target;")) {
+ System.out.println(" NoSuchFieldException '" + msg +
+ "' didn't contain class");
+ }
+ }
+
+ /*
* Do some stuff with long.
*/
long longVal;
@@ -868,4 +882,4 @@
System.out.println(e);
}
}
-}
\ No newline at end of file
+}