Consolidate updating of reflective Field/Method references

Previously we used several different visitors to update Field &
Method references for structural redefinition. We also did not visit
or update JVMTI based references.

This consolidates all the visitors to a single
Runtime::VisitReflectiveTargets function with a single
ReflectiveTargetVisitor type. This simplifies the code around
structural redefinition and ensures that the reflective value holders
are in charge of the actual replacement.

Support was also added for walking internal openjdkjvmti references
for things like field-read/modification events.

Test: ./test.py --host
Bug: 134162467

Change-Id: Ic5fc1db7db0a30f947a1a67259dc024e149ebd57
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 8f1508a..b3a46ca 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -41,6 +41,7 @@
 #include "base/systrace.h"
 #include "base/time_utils.h"
 #include "base/utils.h"
+#include "class_root.h"
 #include "common_throws.h"
 #include "debugger.h"
 #include "dex/dex_file-inl.h"
@@ -80,10 +81,14 @@
 #include "jit/jit_code_cache.h"
 #include "jni/java_vm_ext.h"
 #include "mirror/class-inl.h"
+#include "mirror/executable-inl.h"
+#include "mirror/field.h"
+#include "mirror/method_handle_impl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object-refvisitor-inl.h"
 #include "mirror/object_array-inl.h"
 #include "mirror/reference-inl.h"
+#include "mirror/var_handle.h"
 #include "nativehelper/scoped_local_ref.h"
 #include "obj_ptr-inl.h"
 #include "reflection.h"
@@ -4193,5 +4198,27 @@
   }
 }
 
+void Heap::VisitReflectiveTargets(ReflectiveValueVisitor *visit) {
+  VisitObjectsPaused([&visit](mirror::Object* ref) NO_THREAD_SAFETY_ANALYSIS {
+    art::ObjPtr<mirror::Class> klass(ref->GetClass());
+    // All these classes are in the BootstrapClassLoader.
+    if (!klass->IsBootStrapClassLoaded()) {
+      return;
+    }
+    if (GetClassRoot<mirror::Method>()->IsAssignableFrom(klass) ||
+        GetClassRoot<mirror::Constructor>()->IsAssignableFrom(klass)) {
+      down_cast<mirror::Executable*>(ref)->VisitTarget(visit);
+    } else if (art::GetClassRoot<art::mirror::Field>() == klass) {
+      down_cast<mirror::Field*>(ref)->VisitTarget(visit);
+    } else if (art::GetClassRoot<art::mirror::MethodHandle>()->IsAssignableFrom(klass)) {
+      down_cast<mirror::MethodHandle*>(ref)->VisitTarget(visit);
+    } else if (art::GetClassRoot<art::mirror::FieldVarHandle>()->IsAssignableFrom(klass)) {
+      down_cast<mirror::FieldVarHandle*>(ref)->VisitTarget(visit);
+    } else if (art::GetClassRoot<art::mirror::DexCache>()->IsAssignableFrom(klass)) {
+      down_cast<mirror::DexCache*>(ref)->VisitReflectiveTargets(visit);
+    }
+  });
+}
+
 }  // namespace gc
 }  // namespace art