Refactor ClassLinker & ImageWriter to use a common resolution routine

ImageWriter prepopulates DexCaches of an app image, but does resolution
directly on mirror::Class rather than going through the class linker
where hidden API checks happen. This patch removes the duplicate code.

Similarly, ClassLinker contains multiple methods for resolving methods
and fields. Consolidate these into three common routines:
- FindResolvedMethod
- FindResolvedField
- FindResolvedFieldJLS

The CL also passes the correct class loader to
ImageWriter::PruneAndPreloadDexCache because it would trip a DCHECK
in class linker.

Bug: 78548674
Test: art/test.py
Change-Id: I12e383290945d2f44b209c32e8a7617533d86063
diff --git a/runtime/class_linker.h b/runtime/class_linker.h
index e935d1d..60cff9e 100644
--- a/runtime/class_linker.h
+++ b/runtime/class_linker.h
@@ -392,6 +392,27 @@
       REQUIRES_SHARED(Locks::mutator_lock_)
       REQUIRES(!Locks::dex_lock_, !Roles::uninterruptible_);
 
+  // Find a field with a given ID from the DexFile associated with the given DexCache
+  // and ClassLoader, storing the result in DexCache. The declaring class is assumed
+  // to have been already resolved into `klass`. The `is_static` argument is used to
+  // determine if we are resolving a static or non-static field.
+  ArtField* FindResolvedField(ObjPtr<mirror::Class> klass,
+                              ObjPtr<mirror::DexCache> dex_cache,
+                              ObjPtr<mirror::ClassLoader> class_loader,
+                              uint32_t field_idx,
+                              bool is_static)
+      REQUIRES_SHARED(Locks::mutator_lock_);
+
+  // Find a field with a given ID from the DexFile associated with the given DexCache
+  // and ClassLoader, storing the result in DexCache. The declaring class is assumed
+  // to have been already resolved into `klass`. No is_static argument is provided
+  // so that Java field resolution semantics are followed.
+  ArtField* FindResolvedFieldJLS(ObjPtr<mirror::Class> klass,
+                                 ObjPtr<mirror::DexCache> dex_cache,
+                                 ObjPtr<mirror::ClassLoader> class_loader,
+                                 uint32_t field_idx)
+      REQUIRES_SHARED(Locks::mutator_lock_);
+
   // Resolve a method type with a given ID from the DexFile associated with a given DexCache
   // and ClassLoader, storing the result in the DexCache.
   ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self,