Use ArtMethod::GetDex{File,Cache}() more.

Do not go through the declaring class when it can cause
a DexFile or DexCache mismatch for obsolete methods.

Also fix similar potential mismatch in hiddenapi.

This is a follow-up to
    https://android-review.googlesource.com/834082 ,
    https://android-review.googlesource.com/836008 .

Test: m test-art-host-gtest
Test: testrunner.py --host --optimizing
Bug: 119830111
Change-Id: I3fdf1aa1bc7bab816d5d8034b107506a32438b77
diff --git a/runtime/art_method.cc b/runtime/art_method.cc
index abfdd55..882291f 100644
--- a/runtime/art_method.cc
+++ b/runtime/art_method.cc
@@ -70,7 +70,7 @@
   } else {
     ObjPtr<mirror::Class> declaring_class = GetDeclaringClass();
     DCHECK(declaring_class->IsInterface());
-    ArtMethod* ret = declaring_class->FindInterfaceMethod(declaring_class->GetDexCache(),
+    ArtMethod* ret = declaring_class->FindInterfaceMethod(GetDexCache(),
                                                           GetDexMethodIndex(),
                                                           pointer_size);
     DCHECK(ret != nullptr);
@@ -79,10 +79,11 @@
 }
 
 ArtMethod* ArtMethod::GetNonObsoleteMethod() {
-  DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
   if (LIKELY(!IsObsolete())) {
     return this;
-  } else if (IsDirect()) {
+  }
+  DCHECK_EQ(kRuntimePointerSize, Runtime::Current()->GetClassLinker()->GetImagePointerSize());
+  if (IsDirect()) {
     return &GetDeclaringClass()->GetDirectMethodsSlice(kRuntimePointerSize)[GetMethodIndex()];
   } else {
     return GetDeclaringClass()->GetVTableEntry(GetMethodIndex(), kRuntimePointerSize);
@@ -504,10 +505,10 @@
                          << method->PrettyMethod();
   }
   DCHECK_EQ(oat_method_index,
-            GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(),
+            GetOatMethodIndexFromMethodIndex(declaring_class->GetDexFile(),
                                              method->GetDeclaringClass()->GetDexClassDefIndex(),
                                              method->GetDexMethodIndex()));
-  OatFile::OatClass oat_class = OatFile::FindOatClass(*declaring_class->GetDexCache()->GetDexFile(),
+  OatFile::OatClass oat_class = OatFile::FindOatClass(declaring_class->GetDexFile(),
                                                       declaring_class->GetDexClassDefIndex(),
                                                       found);
   if (!(*found)) {
@@ -543,7 +544,7 @@
 }
 
 ArrayRef<const uint8_t> ArtMethod::GetQuickenedInfo() {
-  const DexFile& dex_file = GetDeclaringClass()->GetDexFile();
+  const DexFile& dex_file = *GetDexFile();
   const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
   if (oat_dex_file == nullptr || (oat_dex_file->GetOatFile() == nullptr)) {
     return ArrayRef<const uint8_t>();
diff --git a/runtime/hidden_api.cc b/runtime/hidden_api.cc
index d3df7fd..6cdba73 100644
--- a/runtime/hidden_api.cc
+++ b/runtime/hidden_api.cc
@@ -280,11 +280,15 @@
   uint32_t flags = kInvalidDexFlags;
   DCHECK(!AreValidDexFlags(flags));
 
+  // Use the non-obsolete method to avoid DexFile mismatch between
+  // the method index and the declaring class.
+  uint32_t method_index = method->GetNonObsoleteMethod()->GetDexMethodIndex();
+
   ClassAccessor accessor(declaring_class->GetDexFile(),
                          *class_def,
                          /* parse_hiddenapi_class_data= */ true);
   auto fn_visit = [&](const ClassAccessor::Method& dex_method) {
-    if (dex_method.GetIndex() == method->GetDexMethodIndex()) {
+    if (dex_method.GetIndex() == method_index) {
       flags = dex_method.GetHiddenapiFlags();
     }
   };
diff --git a/runtime/quick_exception_handler.cc b/runtime/quick_exception_handler.cc
index afdfefa..d23f3e8 100644
--- a/runtime/quick_exception_handler.cc
+++ b/runtime/quick_exception_handler.cc
@@ -238,7 +238,7 @@
         LOG(INFO) << "Handler is upcall";
       }
       if (handler_method_ != nullptr) {
-        const DexFile* dex_file = handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
+        const DexFile* dex_file = handler_method_->GetDexFile();
         int line_number = annotations::GetLineNumFromPC(dex_file, handler_method_, handler_dex_pc_);
         LOG(INFO) << "Handler: " << handler_method_->PrettyMethod() << " (line: "
                   << line_number << ")";
diff --git a/runtime/thread.cc b/runtime/thread.cc
index e9fed76..1de1be4 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -1952,8 +1952,7 @@
       override
       REQUIRES_SHARED(Locks::mutator_lock_) {
     m = m->GetInterfaceMethodIfProxy(kRuntimePointerSize);
-    ObjPtr<mirror::Class> c = m->GetDeclaringClass();
-    ObjPtr<mirror::DexCache> dex_cache = c->GetDexCache();
+    ObjPtr<mirror::DexCache> dex_cache = m->GetDexCache();
     int line_number = -1;
     if (dex_cache != nullptr) {  // be tolerant of bad input
       const DexFile* dex_file = dex_cache->GetDexFile();