Use WellKnownClasses in ThrowStackOverflowError to reduce changes of further stack overflow

Bug: 17463118

(cherry picked from commit bfc2b98a3cf7a9d580f2477e54aabded5004fd26)

Change-Id: Iea04bb7f6c4daa9cbdb8ee6ce8ddcbeb9f05aa3d
diff --git a/runtime/entrypoints/entrypoint_utils.cc b/runtime/entrypoints/entrypoint_utils.cc
index d834d4d..a78c2c0 100644
--- a/runtime/entrypoints/entrypoint_utils.cc
+++ b/runtime/entrypoints/entrypoint_utils.cc
@@ -33,8 +33,10 @@
 
 namespace art {
 
-static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx, mirror::ArtMethod* referrer,
-                                                      int32_t component_count, Thread* self,
+static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx,
+                                                      mirror::ArtMethod* referrer,
+                                                      int32_t component_count,
+                                                      Thread* self,
                                                       bool access_check)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (UNLIKELY(component_count < 0)) {
@@ -56,9 +58,10 @@
     } else {
       ThrowLocation throw_location = self->GetCurrentLocationForThrow();
       DCHECK(throw_location.GetMethod() == referrer);
-      self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
-                               "Found type %s; filled-new-array not implemented for anything but 'int'",
-                               PrettyDescriptor(klass).c_str());
+      self->ThrowNewExceptionF(
+          throw_location, "Ljava/lang/InternalError;",
+          "Found type %s; filled-new-array not implemented for anything but 'int'",
+          PrettyDescriptor(klass).c_str());
     }
     return nullptr;  // Failure
   }
@@ -92,8 +95,10 @@
 }
 
 // Helper function to allocate array for FILLED_NEW_ARRAY.
-mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, mirror::ArtMethod* referrer,
-                                                      int32_t component_count, Thread* self,
+mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
+                                                      mirror::ArtMethod* referrer,
+                                                      int32_t component_count,
+                                                      Thread* self,
                                                       bool access_check,
                                                       gc::AllocatorType /* allocator_type */) {
   mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self,
@@ -144,24 +149,19 @@
     // TODO: Use String::FromModifiedUTF...?
     ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
     if (s.get() != nullptr) {
-      jfieldID detail_message_id = env->GetFieldID(WellKnownClasses::java_lang_Throwable,
-                                                   "detailMessage", "Ljava/lang/String;");
-      env->SetObjectField(exc.get(), detail_message_id, s.get());
+      env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
 
       // cause.
-      jfieldID cause_id = env->GetFieldID(WellKnownClasses::java_lang_Throwable,
-                                          "cause", "Ljava/lang/Throwable;");
-      env->SetObjectField(exc.get(), cause_id, exc.get());
+      env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
 
       // suppressedExceptions.
-      jfieldID emptylist_id = env->GetStaticFieldID(WellKnownClasses::java_util_Collections,
-                                                    "EMPTY_LIST", "Ljava/util/List;");
       ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
-              WellKnownClasses::java_util_Collections, emptylist_id));
+          WellKnownClasses::java_util_Collections,
+          WellKnownClasses::java_util_Collections_EMPTY_LIST));
       CHECK(emptylist.get() != nullptr);
-      jfieldID suppressed_id = env->GetFieldID(WellKnownClasses::java_lang_Throwable,
-                                               "suppressedExceptions", "Ljava/util/List;");
-      env->SetObjectField(exc.get(), suppressed_id, emptylist.get());
+      env->SetObjectField(exc.get(),
+                          WellKnownClasses::java_lang_Throwable_suppressedExceptions,
+                          emptylist.get());
 
       // stackState is set as result of fillInStackTrace. fillInStackTrace calls
       // nativeFillInStackTrace.
@@ -171,19 +171,17 @@
         stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
       }
       if (stack_state_val.get() != nullptr) {
-        jfieldID stackstateID = env->GetFieldID(WellKnownClasses::java_lang_Throwable,
-            "stackState", "Ljava/lang/Object;");
-        env->SetObjectField(exc.get(), stackstateID, stack_state_val.get());
+        env->SetObjectField(exc.get(),
+                            WellKnownClasses::java_lang_Throwable_stackState,
+                            stack_state_val.get());
 
         // stackTrace.
-        jfieldID stack_trace_elem_id = env->GetStaticFieldID(
-            WellKnownClasses::libcore_util_EmptyArray, "STACK_TRACE_ELEMENT",
-            "[Ljava/lang/StackTraceElement;");
         ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
-                WellKnownClasses::libcore_util_EmptyArray, stack_trace_elem_id));
-        jfieldID stacktrace_id = env->GetFieldID(
-            WellKnownClasses::java_lang_Throwable, "stackTrace", "[Ljava/lang/StackTraceElement;");
-        env->SetObjectField(exc.get(), stacktrace_id, stack_trace_elem.get());
+            WellKnownClasses::libcore_util_EmptyArray,
+            WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
+        env->SetObjectField(exc.get(),
+                            WellKnownClasses::java_lang_Throwable_stackTrace,
+                            stack_trace_elem.get());
 
         // Throw the exception.
         ThrowLocation throw_location = self->GetCurrentLocationForThrow();
@@ -326,7 +324,8 @@
         }
       }
       CHECK_NE(throws_index, -1);
-      mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
+      mirror::ObjectArray<mirror::Class>* declared_exceptions =
+          proxy_class->GetThrows()->Get(throws_index);
       mirror::Class* exception_class = exception->GetClass();
       bool declares_exception = false;
       for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {