Refactor java.lang.reflect implementation

Cherry-picked from commit ed41d5c44299ec5d44b8514f6e17f802f48094d1.

Move to ArtMethod/Field instead of AbstractMethod/Field and have
java.lang.reflect APIs delegate to ArtMethod/ArtField.

Bug: 10014286.

Change-Id: Iafc1d8c5b62562c9af8fb9fd8c5e1d61270536e7
diff --git a/runtime/native/dalvik_system_VMStack.cc b/runtime/native/dalvik_system_VMStack.cc
index 1a80d62..eaf67b8 100644
--- a/runtime/native/dalvik_system_VMStack.cc
+++ b/runtime/native/dalvik_system_VMStack.cc
@@ -16,7 +16,7 @@
 
 #include "jni_internal.h"
 #include "nth_caller_visitor.h"
-#include "mirror/abstract_method-inl.h"
+#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
 #include "mirror/class_loader.h"
 #include "mirror/object-inl.h"
diff --git a/runtime/native/java_lang_reflect_Constructor.cc b/runtime/native/java_lang_reflect_Constructor.cc
index 9180217..85556ac 100644
--- a/runtime/native/java_lang_reflect_Constructor.cc
+++ b/runtime/native/java_lang_reflect_Constructor.cc
@@ -16,13 +16,14 @@
 
 #include "class_linker.h"
 #include "jni_internal.h"
+#include "mirror/art_method.h"
+#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
-#include "mirror/abstract_method.h"
-#include "mirror/abstract_method-inl.h"
 #include "mirror/object-inl.h"
 #include "object_utils.h"
 #include "reflection.h"
 #include "scoped_thread_state_change.h"
+#include "well_known_classes.h"
 
 namespace art {
 
@@ -35,7 +36,10 @@
  */
 static jobject Constructor_newInstance(JNIEnv* env, jobject javaMethod, jobjectArray javaArgs) {
   ScopedObjectAccess soa(env);
-  mirror::AbstractMethod* m = soa.Decode<mirror::Object*>(javaMethod)->AsMethod();
+  jobject art_method = soa.Env()->GetObjectField(
+      javaMethod, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
+
+  mirror::ArtMethod* m = soa.Decode<mirror::Object*>(art_method)->AsArtMethod();
   mirror::Class* c = m->GetDeclaringClass();
   if (UNLIKELY(c->IsAbstract())) {
     ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
diff --git a/runtime/native/java_lang_reflect_Field.cc b/runtime/native/java_lang_reflect_Field.cc
index b0daa91..00f89b6 100644
--- a/runtime/native/java_lang_reflect_Field.cc
+++ b/runtime/native/java_lang_reflect_Field.cc
@@ -19,16 +19,15 @@
 #include "common_throws.h"
 #include "dex_file-inl.h"
 #include "jni_internal.h"
+#include "mirror/art_field-inl.h"
 #include "mirror/class-inl.h"
-#include "mirror/field.h"
-#include "mirror/field-inl.h"
 #include "object_utils.h"
 #include "reflection.h"
 #include "scoped_thread_state_change.h"
 
 namespace art {
 
-static bool GetFieldValue(const ScopedObjectAccess& soa, mirror::Object* o, mirror::Field* f,
+static bool GetFieldValue(const ScopedObjectAccess& soa, mirror::Object* o, mirror::ArtField* f,
                           JValue& value, bool allow_references)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   DCHECK_EQ(value.GetJ(), 0LL);
@@ -78,7 +77,7 @@
   return false;
 }
 
-static bool CheckReceiver(const ScopedObjectAccess& soa, jobject j_rcvr, mirror::Field* f,
+static bool CheckReceiver(const ScopedObjectAccess& soa, jobject j_rcvr, mirror::ArtField* f,
                           mirror::Object*& class_or_rcvr)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (f->IsStatic()) {
@@ -96,7 +95,7 @@
 
 static jobject Field_get(JNIEnv* env, jobject javaField, jobject javaObj) {
   ScopedObjectAccess soa(env);
-  mirror::Field* f = soa.DecodeField(env->FromReflectedField(javaField));
+  mirror::ArtField* f = soa.DecodeField(env->FromReflectedField(javaField));
   mirror::Object* o = NULL;
   if (!CheckReceiver(soa, javaObj, f, o)) {
     return NULL;
@@ -111,9 +110,10 @@
       soa.AddLocalReference<jobject>(BoxPrimitive(FieldHelper(f).GetTypeAsPrimitiveType(), value));
 }
 
-static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char dst_descriptor) {
+static JValue GetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj,
+                                char dst_descriptor) {
   ScopedObjectAccess soa(env);
-  mirror::Field* f = soa.DecodeField(env->FromReflectedField(javaField));
+  mirror::ArtField* f = soa.DecodeField(env->FromReflectedField(javaField));
   mirror::Object* o = NULL;
   if (!CheckReceiver(soa, javaObj, f, o)) {
     return JValue();
@@ -127,7 +127,8 @@
 
   // Widen it if necessary (and possible).
   JValue wide_value;
-  mirror::Class* dst_type = Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
+  mirror::Class* dst_type =
+      Runtime::Current()->GetClassLinker()->FindPrimitiveClass(dst_descriptor);
   if (!ConvertPrimitiveValue(NULL, false, FieldHelper(f).GetTypeAsPrimitiveType(),
                              dst_type->GetPrimitiveType(), field_value, wide_value)) {
     return JValue();
@@ -167,7 +168,7 @@
   return GetPrimitiveField(env, javaField, javaObj, 'S').GetS();
 }
 
-static void SetFieldValue(mirror::Object* o, mirror::Field* f, const JValue& new_value,
+static void SetFieldValue(mirror::Object* o, mirror::ArtField* f, const JValue& new_value,
                           bool allow_references)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(f->GetDeclaringClass(),
@@ -221,7 +222,7 @@
 
 static void Field_set(JNIEnv* env, jobject javaField, jobject javaObj, jobject javaValue) {
   ScopedObjectAccess soa(env);
-  mirror::Field* f = soa.DecodeField(env->FromReflectedField(javaField));
+  mirror::ArtField* f = soa.DecodeField(env->FromReflectedField(javaField));
 
   // Unbox the value, if necessary.
   mirror::Object* boxed_value = soa.Decode<mirror::Object*>(javaValue);
@@ -242,7 +243,7 @@
 static void SetPrimitiveField(JNIEnv* env, jobject javaField, jobject javaObj, char src_descriptor,
                               const JValue& new_value) {
   ScopedObjectAccess soa(env);
-  mirror::Field* f = soa.DecodeField(env->FromReflectedField(javaField));
+  mirror::ArtField* f = soa.DecodeField(env->FromReflectedField(javaField));
   mirror::Object* o = NULL;
   if (!CheckReceiver(soa, javaObj, f, o)) {
     return;
diff --git a/runtime/native/java_lang_reflect_Method.cc b/runtime/native/java_lang_reflect_Method.cc
index 14dc6a4..d29de3d 100644
--- a/runtime/native/java_lang_reflect_Method.cc
+++ b/runtime/native/java_lang_reflect_Method.cc
@@ -16,26 +16,31 @@
 
 #include "class_linker.h"
 #include "jni_internal.h"
+#include "mirror/art_method.h"
+#include "mirror/art_method-inl.h"
 #include "mirror/class-inl.h"
-#include "mirror/abstract_method.h"
-#include "mirror/abstract_method-inl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
 #include "mirror/proxy.h"
 #include "object_utils.h"
 #include "reflection.h"
 #include "scoped_thread_state_change.h"
+#include "well_known_classes.h"
 
 namespace art {
 
-static jobject Method_invoke(JNIEnv* env, jobject javaMethod, jobject javaReceiver, jobject javaArgs) {
+static jobject Method_invoke(JNIEnv* env,
+                             jobject javaMethod, jobject javaReceiver, jobject javaArgs) {
   ScopedObjectAccess soa(env);
   return InvokeMethod(soa, javaMethod, javaReceiver, javaArgs);
 }
 
 static jobject Method_getExceptionTypesNative(JNIEnv* env, jobject javaMethod) {
   ScopedObjectAccess soa(env);
-  mirror::AbstractMethod* proxy_method = soa.Decode<mirror::Object*>(javaMethod)->AsMethod();
+  jobject art_method = soa.Env()->GetObjectField(
+      javaMethod, WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
+
+  mirror::ArtMethod* proxy_method = soa.Decode<mirror::Object*>(art_method)->AsArtMethod();
   CHECK(proxy_method->GetDeclaringClass()->IsProxyClass());
   mirror::SynthesizedProxyClass* proxy_class =
       down_cast<mirror::SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
@@ -48,20 +53,14 @@
     }
   }
   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);
   return soa.AddLocalReference<jobject>(declared_exceptions->Clone(soa.Self()));
 }
 
-static jobject Method_findOverriddenMethodNative(JNIEnv* env, jobject javaMethod) {
-  ScopedObjectAccess soa(env);
-  mirror::AbstractMethod* method = soa.Decode<mirror::Object*>(javaMethod)->AsMethod();
-  return soa.AddLocalReference<jobject>(method->FindOverriddenMethod());
-}
-
 static JNINativeMethod gMethods[] = {
   NATIVE_METHOD(Method, invoke, "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"),
   NATIVE_METHOD(Method, getExceptionTypesNative, "()[Ljava/lang/Class;"),
-  NATIVE_METHOD(Method, findOverriddenMethodNative, "()Ljava/lang/reflect/Method;"),
 };
 
 void register_java_lang_reflect_Method(JNIEnv* env) {
diff --git a/runtime/native/java_lang_reflect_Proxy.cc b/runtime/native/java_lang_reflect_Proxy.cc
index 547ce7b..a92823a 100644
--- a/runtime/native/java_lang_reflect_Proxy.cc
+++ b/runtime/native/java_lang_reflect_Proxy.cc
@@ -31,8 +31,8 @@
   mirror::ObjectArray<mirror::Class>* interfaces =
       soa.Decode<mirror::ObjectArray<mirror::Class>*>(javaInterfaces);
   mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
-  mirror::ObjectArray<mirror::AbstractMethod>* methods =
-      soa.Decode<mirror::ObjectArray<mirror::AbstractMethod>*>(javaMethods);
+  mirror::ObjectArray<mirror::ArtMethod>* methods =
+      soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(javaMethods);
   mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >* throws =
       soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(javaThrows);
   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
@@ -41,7 +41,7 @@
 }
 
 static JNINativeMethod gMethods[] = {
-  NATIVE_METHOD(Proxy, generateProxy, "(Ljava/lang/String;[Ljava/lang/Class;Ljava/lang/ClassLoader;[Ljava/lang/reflect/Method;[[Ljava/lang/Class;)Ljava/lang/Class;"),
+  NATIVE_METHOD(Proxy, generateProxy, "(Ljava/lang/String;[Ljava/lang/Class;Ljava/lang/ClassLoader;[Ljava/lang/reflect/ArtMethod;[[Ljava/lang/Class;)Ljava/lang/Class;"),
 };
 
 void register_java_lang_reflect_Proxy(JNIEnv* env) {