DO NOT MERGE Fix all unsafe caching to be like libcore.

This way, if a runtime is restarted within a process, we re-initialize all
the cached data.

Conflicts:

	src/native/java_lang_Runtime.cc -- nativeExit lost an argument in dalvik-dev

(cherry picked from commit 7756d5473fa27ce7e6ac7c31770eef7030431da4)

Change-Id: I6184fc20c2a9ec16c4b053584a4d1c3b64452d0f
diff --git a/src/debugger.cc b/src/debugger.cc
index 2156482..342d548 100644
--- a/src/debugger.cc
+++ b/src/debugger.cc
@@ -34,6 +34,7 @@
 #include "space.h"
 #include "stack_indirect_reference_table.h"
 #include "thread_list.h"
+#include "well_known_classes.h"
 
 extern "C" void dlmalloc_walk_heap(void(*)(const void*, size_t, const void*, size_t, void*), void*);
 #ifndef HAVE_ANDROID_OS
@@ -2322,14 +2323,6 @@
   Thread* self = Thread::Current();
   JNIEnv* env = self->GetJniEnv();
 
-  static jclass Chunk_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
-  static jclass DdmServer_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
-  static jmethodID dispatch_mid = env->GetStaticMethodID(DdmServer_class, "dispatch", "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
-  static jfieldID data_fid = env->GetFieldID(Chunk_class, "data", "[B");
-  static jfieldID length_fid = env->GetFieldID(Chunk_class, "length", "I");
-  static jfieldID offset_fid = env->GetFieldID(Chunk_class, "offset", "I");
-  static jfieldID type_fid = env->GetFieldID(Chunk_class, "type", "I");
-
   // Create a byte[] corresponding to 'buf'.
   ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(dataLen));
   if (dataArray.get() == NULL) {
@@ -2352,7 +2345,9 @@
   }
 
   // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
-  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(DdmServer_class, dispatch_mid, type, dataArray.get(), offset, length));
+  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
+                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
+                                                                 type, dataArray.get(), offset, length));
   if (env->ExceptionCheck()) {
     LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
     env->ExceptionDescribe();
@@ -2376,10 +2371,10 @@
    *
    * So we're pretty much stuck with copying data around multiple times.
    */
-  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), data_fid)));
-  length = env->GetIntField(chunk.get(), length_fid);
-  offset = env->GetIntField(chunk.get(), offset_fid);
-  type = env->GetIntField(chunk.get(), type_fid);
+  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
+  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
+  offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
+  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
 
   VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
   if (length == 0 || replyData.get() == NULL) {
@@ -2418,10 +2413,10 @@
   }
 
   JNIEnv* env = self->GetJniEnv();
-  static jclass DdmServer_class = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
-  static jmethodID broadcast_mid = env->GetStaticMethodID(DdmServer_class, "broadcast", "(I)V");
   jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
-  env->CallStaticVoidMethod(DdmServer_class, broadcast_mid, event);
+  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
+                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
+                            event);
   if (env->ExceptionCheck()) {
     LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
     env->ExceptionDescribe();