ObjPtr<>-ify String allocations, fix stale refs.

ObjPtr<>-ify String allocation functions and related code
and remove some unnecessary calls to ObjPtr<>::Ptr(). Fix
stale reference uses in reference_table_test and stub_test.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Bug: 31113334
Change-Id: I42927fb8b7240e5132188f73318b2ccb218748fd
diff --git a/runtime/reference_table_test.cc b/runtime/reference_table_test.cc
index 2acb2c7..e5c1e6a 100644
--- a/runtime/reference_table_test.cc
+++ b/runtime/reference_table_test.cc
@@ -42,7 +42,7 @@
 
 class ReferenceTableTest : public CommonRuntimeTest {};
 
-static mirror::Object* CreateWeakReference(mirror::Object* referent)
+static ObjPtr<mirror::Object> CreateWeakReference(ObjPtr<mirror::Object> referent)
     REQUIRES_SHARED(Locks::mutator_lock_) {
   Thread* self = Thread::Current();
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
@@ -77,7 +77,9 @@
 
 TEST_F(ReferenceTableTest, Basics) {
   ScopedObjectAccess soa(Thread::Current());
-  mirror::Object* o1 = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello");
+  StackHandleScope<5u> hs(soa.Self());
+  Handle<mirror::String> o1 =
+      hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"));
 
   ReferenceTable rt("test", 0, 11);
 
@@ -89,17 +91,17 @@
     EXPECT_EQ(0U, rt.Size());
   }
 
-  // Check removal of all nullss in a empty table is a no-op.
+  // Check removal of all nulls in a empty table is a no-op.
   rt.Remove(nullptr);
   EXPECT_EQ(0U, rt.Size());
 
   // Check removal of all o1 in a empty table is a no-op.
-  rt.Remove(o1);
+  rt.Remove(o1.Get());
   EXPECT_EQ(0U, rt.Size());
 
   // Add o1 and check we have 1 element and can dump.
   {
-    rt.Add(o1);
+    rt.Add(o1.Get());
     EXPECT_EQ(1U, rt.Size());
     std::ostringstream oss;
     rt.Dump(oss);
@@ -108,9 +110,9 @@
   }
 
   // Add a second object 10 times and check dumping is sane.
-  ObjPtr<mirror::Object> o2 = mirror::ShortArray::Alloc(soa.Self(), 0);
+  Handle<mirror::ShortArray> o2 = hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 0));
   for (size_t i = 0; i < 10; ++i) {
-    rt.Add(o2);
+    rt.Add(o2.Get());
     EXPECT_EQ(i + 2, rt.Size());
     std::ostringstream oss;
     rt.Dump(oss);
@@ -129,7 +131,7 @@
 
   // Remove o1 (first element).
   {
-    rt.Remove(o1);
+    rt.Remove(o1.Get());
     EXPECT_EQ(10U, rt.Size());
     std::ostringstream oss;
     rt.Dump(oss);
@@ -138,7 +140,7 @@
 
   // Remove o2 ten times.
   for (size_t i = 0; i < 10; ++i) {
-    rt.Remove(o2);
+    rt.Remove(o2.Get());
     EXPECT_EQ(9 - i, rt.Size());
     std::ostringstream oss;
     rt.Dump(oss);
@@ -154,7 +156,7 @@
 
   // Add a reference and check that the type of the referent is dumped.
   {
-    mirror::Object* empty_reference = CreateWeakReference(nullptr);
+    ObjPtr<mirror::Object> empty_reference = CreateWeakReference(nullptr);
     ASSERT_TRUE(empty_reference->IsReferenceInstance());
     rt.Add(empty_reference);
     std::ostringstream oss;
@@ -165,8 +167,9 @@
   }
 
   {
-    mirror::Object* string_referent = mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A");
-    mirror::Object* non_empty_reference = CreateWeakReference(string_referent);
+    ObjPtr<mirror::Object> string_referent =
+        mirror::String::AllocFromModifiedUtf8(Thread::Current(), "A");
+    ObjPtr<mirror::Object> non_empty_reference = CreateWeakReference(string_referent);
     ASSERT_TRUE(non_empty_reference->IsReferenceInstance());
     rt.Add(non_empty_reference);
     std::ostringstream oss;
@@ -179,7 +182,6 @@
 
   // Add two objects. Enable allocation tracking for the latter.
   {
-    StackHandleScope<3> hs(soa.Self());
     Handle<mirror::String> h_without_trace(hs.NewHandle(
         mirror::String::AllocFromModifiedUtf8(soa.Self(), "Without")));