Add convenience helper `ArtMethod::NewObject()`.

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Change-Id: I4961d4f121f782d47490f7e5e718ee28b3b87efa
diff --git a/adbconnection/adbconnection.cc b/adbconnection/adbconnection.cc
index 6a60754..239fe31 100644
--- a/adbconnection/adbconnection.cc
+++ b/adbconnection/adbconnection.cc
@@ -24,6 +24,7 @@
 #include "android-base/endian.h"
 #include "android-base/stringprintf.h"
 #include "art_field-inl.h"
+#include "art_method-alloc-inl.h"
 #include "base/file_utils.h"
 #include "base/globals.h"
 #include "base/logging.h"
@@ -184,31 +185,23 @@
 
 static art::ObjPtr<art::mirror::Object> CreateAdbConnectionThread(art::Thread* self)
     REQUIRES_SHARED(art::Locks::mutator_lock_) {
-  art::StackHandleScope<2u> hs(self);
+  art::StackHandleScope<3u> hs(self);
   art::Handle<art::mirror::String> thr_name =
       hs.NewHandle(art::mirror::String::AllocFromModifiedUtf8(self, kAdbConnectionThreadName));
   if (thr_name == nullptr) {
     DCHECK(self->IsExceptionPending());
     return nullptr;
   }
-  art::ObjPtr<art::mirror::Class> thread_class =
-      art::WellKnownClasses::java_lang_Thread_init->GetDeclaringClass();
-  art::Handle<art::mirror::Object> thread = hs.NewHandle(thread_class->AllocObject(self));
-  if (thread == nullptr) {
-    DCHECK(self->IsExceptionPending());
-    return nullptr;
-  }
   art::ArtField* system_thread_group_field =
       art::WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
   DCHECK(system_thread_group_field->GetDeclaringClass()->IsInitialized());
   // Avoid using `ArtField::GetObject` as it requires linking against `libdexfile` for
   // `operator<<(std::ostream&, Primitive::Type)`.
-  art::ObjPtr<art::mirror::Object> system_thread_group =
+  art::Handle<art::mirror::Object> system_thread_group = hs.NewHandle(
       system_thread_group_field->GetDeclaringClass()->GetFieldObject<art::mirror::Object>(
-          system_thread_group_field->GetOffset());
-  art::WellKnownClasses::java_lang_Thread_init->InvokeInstance<'V', 'L', 'L', 'I', 'Z'>(
-      self, thread.Get(), system_thread_group, thr_name.Get(), /*priority=*/ 0, /*daemon=*/ true);
-  return self->IsExceptionPending() ? nullptr : thread.Get();
+          system_thread_group_field->GetOffset()));
+  return art::WellKnownClasses::java_lang_Thread_init->NewObject<'L', 'L', 'I', 'Z'>(
+      hs, self, system_thread_group, thr_name, /*priority=*/ 0, /*daemon=*/ true).Get();
 }
 
 struct CallbackData {
diff --git a/runtime/art_method-alloc-inl.h b/runtime/art_method-alloc-inl.h
new file mode 100644
index 0000000..13051b1
--- /dev/null
+++ b/runtime/art_method-alloc-inl.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_RUNTIME_ART_METHOD_ALLOC_INL_H_
+#define ART_RUNTIME_ART_METHOD_ALLOC_INL_H_
+
+#include "art_method-inl.h"
+
+#include "handle.h"
+#include "handle_scope.h"
+#include "mirror/class-alloc-inl.h"
+
+namespace art {
+
+namespace detail {
+
+template <char Shorty>
+struct HandleShortyTraits {
+  using Type = typename ShortyTraits<Shorty>::Type;
+  static Type Extract(Type value) ALWAYS_INLINE { return value; }
+};
+
+template <> struct HandleShortyTraits<'L'> {
+  using Type = Handle<mirror::Object>;
+  static typename ShortyTraits<'L'>::Type Extract(Type value)
+      REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
+    return value.Get();
+  }
+};
+
+}  // namespace detail
+
+template <char... ArgType, typename HandleScopeType>
+inline Handle<mirror::Object> ArtMethod::NewObject(
+    HandleScopeType& hs,
+    Thread* self,
+    typename detail::HandleShortyTraits<ArgType>::Type... args) {
+  DCHECK(!GetDeclaringClass()->IsInterface());
+  DCHECK(GetDeclaringClass()->IsInitialized());
+  DCHECK(IsConstructor());
+  DCHECK(!IsStatic());
+  MutableHandle<mirror::Object> new_object = hs.NewHandle(GetDeclaringClass()->AllocObject(self));
+  DCHECK_EQ(new_object == nullptr, self->IsExceptionPending());
+  if (LIKELY(new_object != nullptr)) {
+    InvokeInstance<'V', ArgType...>(
+        self, new_object.Get(), detail::HandleShortyTraits<ArgType>::Extract(args)...);
+    if (UNLIKELY(self->IsExceptionPending())) {
+      new_object.Assign(nullptr);
+    }
+  }
+  return new_object;
+}
+
+template <char... ArgType>
+inline ObjPtr<mirror::Object> ArtMethod::NewObject(
+    Thread* self, typename detail::HandleShortyTraits<ArgType>::Type... args) {
+  StackHandleScope<1u> hs(self);
+  return NewObject<ArgType...>(hs, self, args...).Get();
+}
+
+}  // namespace art
+
+#endif  // ART_RUNTIME_ART_METHOD_ALLOC_INL_H_
diff --git a/runtime/art_method-inl.h b/runtime/art_method-inl.h
index a1ed242..a568534 100644
--- a/runtime/art_method-inl.h
+++ b/runtime/art_method-inl.h
@@ -180,6 +180,7 @@
 inline typename detail::ShortyTraits<ReturnType>::Type
 ArtMethod::InvokeStatic(Thread* self, typename detail::ShortyTraits<ArgType>::Type... args) {
   DCHECK(IsStatic());
+  DCHECK(GetDeclaringClass()->IsInitialized());  // Used only for initialized well-known classes.
   JValue result;
   constexpr auto shorty = detail::MaterializeShorty<ReturnType, ArgType...>();
   auto vregs = detail::MaterializeVRegs<ArgType...>(args...);
diff --git a/runtime/art_method.h b/runtime/art_method.h
index 1065429..5186a09 100644
--- a/runtime/art_method.h
+++ b/runtime/art_method.h
@@ -69,7 +69,7 @@
 }  // namespace mirror
 
 namespace detail {
-template <char Type> struct ShortyTraits;
+template <char Shorty> struct ShortyTraits;
 template <> struct ShortyTraits<'V'>;
 template <> struct ShortyTraits<'Z'>;
 template <> struct ShortyTraits<'B'>;
@@ -80,6 +80,8 @@
 template <> struct ShortyTraits<'F'>;
 template <> struct ShortyTraits<'D'>;
 template <> struct ShortyTraits<'L'>;
+template <char Shorty> struct HandleShortyTraits;
+template <> struct HandleShortyTraits<'L'>;
 }  // namespace detail
 
 class ArtMethod final {
@@ -698,6 +700,17 @@
                   typename detail::ShortyTraits<ArgType>::Type... args)
       REQUIRES_SHARED(Locks::mutator_lock_);
 
+  template <char... ArgType, typename HandleScopeType>
+  Handle<mirror::Object> NewObject(HandleScopeType& hs,
+                                   Thread* self,
+                                   typename detail::HandleShortyTraits<ArgType>::Type... args)
+      REQUIRES_SHARED(Locks::mutator_lock_);
+
+  template <char... ArgType>
+  ObjPtr<mirror::Object> NewObject(Thread* self,
+                                   typename detail::HandleShortyTraits<ArgType>::Type... args)
+      REQUIRES_SHARED(Locks::mutator_lock_);
+
   const void* GetEntryPointFromQuickCompiledCode() const {
     return GetEntryPointFromQuickCompiledCodePtrSize(kRuntimePointerSize);
   }
diff --git a/runtime/art_method_test.cc b/runtime/art_method_test.cc
index fac1d12..b71f8a3 100644
--- a/runtime/art_method_test.cc
+++ b/runtime/art_method_test.cc
@@ -16,7 +16,7 @@
 
 #include <type_traits>
 
-#include "art_method-inl.h"
+#include "art_method-alloc-inl.h"
 
 #include "base/casts.h"
 #include "common_runtime_test.h"
@@ -135,10 +135,9 @@
       list_class->FindInterfaceMethod("size", "()I", kRuntimePointerSize);
   DCHECK(list_size_method != nullptr);
 
-  Handle<mirror::Object> array_list = hs.NewHandle(array_list_class->AllocObject(self));
-  ASSERT_TRUE(array_list != nullptr);
-  init->InvokeInstance<'V'>(self, array_list.Get());
+  Handle<mirror::Object> array_list = init->NewObject<>(hs, self);
   ASSERT_FALSE(self->IsExceptionPending());
+  ASSERT_TRUE(array_list != nullptr);
 
   // Invoke `ArrayList.size()` directly, with virtual dispatch from
   // `AbstractList.size()` and with interface dispatch from `List.size()`.
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index 3dda8f9..a7b818e 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -199,7 +199,6 @@
   memcpy(data_array->GetData(), data.data(), data.size());
   // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
   ArtMethod* dispatch = WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
-  DCHECK(dispatch->GetDeclaringClass()->IsInitialized());
   ObjPtr<mirror::Object> chunk = dispatch->InvokeStatic<'L', 'I', 'L', 'I', 'I'>(
       soa.Self(), type, data_array.Get(), 0, static_cast<jint>(data.size()));
   if (soa.Self()->IsExceptionPending()) {
@@ -269,7 +268,6 @@
   JNIEnv* env = self->GetJniEnv();
   jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
   ArtMethod* broadcast = WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
-  DCHECK(broadcast->GetDeclaringClass()->IsInitialized());
   broadcast->InvokeStatic<'V', 'I'>(self, event);
   if (self->IsExceptionPending()) {
     LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
diff --git a/runtime/dex/dex_file_annotations.cc b/runtime/dex/dex_file_annotations.cc
index f2ee9a6..7c461fd 100644
--- a/runtime/dex/dex_file_annotations.cc
+++ b/runtime/dex/dex_file_annotations.cc
@@ -21,7 +21,7 @@
 #include "android-base/stringprintf.h"
 
 #include "art_field-inl.h"
-#include "art_method-inl.h"
+#include "art_method-alloc-inl.h"
 #include "base/sdk_version.h"
 #include "class_linker-inl.h"
 #include "class_root-inl.h"
@@ -396,7 +396,6 @@
 
   ArtMethod* create_annotation_method =
       WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
-  DCHECK(create_annotation_method->GetDeclaringClass()->IsInitialized());
   ObjPtr<mirror::Object> result = create_annotation_method->InvokeStatic<'L', 'L', 'L'>(
       self, annotation_class.Get(), h_element_array.Get());
   if (self->IsExceptionPending()) {
@@ -741,23 +740,12 @@
     return nullptr;
   }
 
-  ArtMethod* annotation_member_init = WellKnownClasses::libcore_reflect_AnnotationMember_init;
-  DCHECK(annotation_member_init->GetDeclaringClass()->IsInitialized());
   Handle<mirror::Object> new_member =
-      hs.NewHandle(annotation_member_init->GetDeclaringClass()->AllocObject(self));
+      WellKnownClasses::libcore_reflect_AnnotationMember_init->NewObject<'L', 'L', 'L', 'L'>(
+          hs, self, string_name, value_object, method_return, method_object);
   if (new_member == nullptr) {
-    LOG(ERROR) << "Failed to allocate annotation member";
-    return nullptr;
-  }
-
-  annotation_member_init->InvokeInstance<'V', 'L', 'L', 'L', 'L'>(self,
-                                                                  new_member.Get(),
-                                                                  string_name.Get(),
-                                                                  value_object.Get(),
-                                                                  method_return.Get(),
-                                                                  method_object.Get());
-  if (self->IsExceptionPending()) {
-    LOG(INFO) << "Exception in AnnotationMember.<init>";
+    DCHECK(self->IsExceptionPending());
+    LOG(ERROR) << "Failed to create annotation member";
     return nullptr;
   }
 
diff --git a/runtime/jni/jni_internal.cc b/runtime/jni/jni_internal.cc
index ea34fd3..f517dd7 100644
--- a/runtime/jni/jni_internal.cc
+++ b/runtime/jni/jni_internal.cc
@@ -22,7 +22,7 @@
 #include <utility>
 
 #include "art_field-inl.h"
-#include "art_method-inl.h"
+#include "art_method-alloc-inl.h"
 #include "base/allocator.h"
 #include "base/atomic.h"
 #include "base/casts.h"
@@ -2777,17 +2777,9 @@
     jint capacity_arg = static_cast<jint>(capacity);
 
     ScopedObjectAccess soa(env);
-    DCHECK(WellKnownClasses::java_nio_DirectByteBuffer_init->GetDeclaringClass()->IsInitialized());
-    Thread* self = soa.Self();
-    StackHandleScope<1u> hs(self);
-    Handle<mirror::Object> result = hs.NewHandle(
-        WellKnownClasses::java_nio_DirectByteBuffer_init->GetDeclaringClass()->AllocObject(self));
-    DCHECK_EQ(result == nullptr, self->IsExceptionPending());
-    if (result != nullptr) {
-      WellKnownClasses::java_nio_DirectByteBuffer_init->InvokeInstance<'V', 'J', 'I'>(
-          self, result.Get(), address_arg, capacity_arg);
-    }
-    return self->IsExceptionPending() ? nullptr : soa.AddLocalReference<jobject>(result.Get());
+    return soa.AddLocalReference<jobject>(
+        WellKnownClasses::java_nio_DirectByteBuffer_init->NewObject<'J', 'I'>(
+            soa.Self(), address_arg, capacity_arg));
   }
 
   static void* GetDirectBufferAddress(JNIEnv* env, jobject java_buffer) {
diff --git a/runtime/mirror/method_handles_lookup.cc b/runtime/mirror/method_handles_lookup.cc
index c4c2b58..62c35df 100644
--- a/runtime/mirror/method_handles_lookup.cc
+++ b/runtime/mirror/method_handles_lookup.cc
@@ -42,7 +42,6 @@
 
 ObjPtr<MethodHandlesLookup> MethodHandlesLookup::GetDefault(Thread* const self) {
   ArtMethod* lookup = WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
-  DCHECK(lookup->GetDeclaringClass()->IsInitialized());
   return ObjPtr<MethodHandlesLookup>::DownCast(lookup->InvokeStatic<'L'>(self));
 }
 
diff --git a/runtime/runtime_callbacks_test.cc b/runtime/runtime_callbacks_test.cc
index ee11800..b0782a5 100644
--- a/runtime/runtime_callbacks_test.cc
+++ b/runtime/runtime_callbacks_test.cc
@@ -27,7 +27,7 @@
 
 #include "jni.h"
 
-#include "art_method-inl.h"
+#include "art_method-alloc-inl.h"
 #include "base/mem_map.h"
 #include "base/mutex.h"
 #include "class_linker.h"
@@ -167,24 +167,18 @@
   ScopedObjectAccess soa(self);
   MakeExecutable(WellKnownClasses::java_lang_Thread_init->GetDeclaringClass());
 
-  StackHandleScope<2u> hs(self);
+  StackHandleScope<3u> hs(self);
   Handle<mirror::String> thread_name = hs.NewHandle(
       mirror::String::AllocFromModifiedUtf8(self, "ThreadLifecycleCallback test thread"));
   ASSERT_TRUE(thread_name != nullptr);
 
-  DCHECK(WellKnownClasses::java_lang_Thread_init->GetDeclaringClass()->IsInitialized());
-  Handle<mirror::Object> thread = hs.NewHandle(
-      WellKnownClasses::java_lang_Thread_init->GetDeclaringClass()->AllocObject(self));
-  ASSERT_TRUE(thread != nullptr);
-
-  WellKnownClasses::java_lang_Thread_init->InvokeInstance<'V', 'L', 'L', 'I', 'Z'>(
-      self,
-      thread.Get(),
-      soa.Decode<mirror::Object>(runtime_->GetMainThreadGroup()),
-      thread_name.Get(),
-      kMinThreadPriority,
-      /*daemon=*/ false);
+  Handle<mirror::Object> thread_group =
+      hs.NewHandle(soa.Decode<mirror::Object>(runtime_->GetMainThreadGroup()));
+  Handle<mirror::Object> thread =
+      WellKnownClasses::java_lang_Thread_init->NewObject<'L', 'L', 'I', 'Z'>(
+          hs, self, thread_group, thread_name, kMinThreadPriority, /*daemon=*/ false);
   ASSERT_FALSE(self->IsExceptionPending());
+  ASSERT_TRUE(thread != nullptr);
 
   ArtMethod* start_method =
       thread->GetClass()->FindClassMethod("start", "()V", kRuntimePointerSize);