Increase use of ScopedJniThreadState.

Move the routines for changing Object* to jobject and vice-versa
(AddLocalReference and Decode) to ScopedJniThreadState to enforce use of
Object*s in the Runnable thread state. In the Runnable thread state
suspension is necessary before GC can take place.

Reduce use of const ClassLoader* as the code bottoms out in FindClass
and with a field assignment where the const is cast away (ie if we're
not going to enforce the const-ness we shouldn't pretend it is).

Refactor the Thread::Attach API so that we're not handling raw Objects on
unattached threads.

Remove some unreachable code.

Change-Id: I0fa969f49ee6a8f10752af74a6b0e04d46b4cd97
diff --git a/src/stack.cc b/src/stack.cc
index 336f8ad..07a1cb1 100644
--- a/src/stack.cc
+++ b/src/stack.cc
@@ -26,7 +26,8 @@
 
 class StackGetter {
  public:
-  StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
+  StackGetter(const ScopedJniThreadState& ts, Thread* thread)
+    : ts_(ts), thread_(thread), trace_(NULL) {
   }
 
   static void Callback(void* arg) {
@@ -39,17 +40,17 @@
 
  private:
   void Callback() {
-    trace_ = thread_->CreateInternalStackTrace(env_);
+    trace_ = thread_->CreateInternalStackTrace(ts_);
   }
 
-  JNIEnv* env_;
-  Thread* thread_;
+  const ScopedJniThreadState& ts_;
+  Thread* const thread_;
   jobject trace_;
 };
 
-jobject GetThreadStack(JNIEnv* env, Thread* thread) {
+jobject GetThreadStack(const ScopedJniThreadState& ts, Thread* thread) {
   ThreadList* thread_list = Runtime::Current()->GetThreadList();
-  StackGetter stack_getter(env, thread);
+  StackGetter stack_getter(ts, thread);
   thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
   return stack_getter.GetTrace();
 }