Move veridex to ClassAccessor

Bug: 79758018
Test: test-art-host

Change-Id: I38bfc8ca1050698a60807ae77eadeb04a5809579
diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h
index 9f355ba..8f853ea 100644
--- a/dexlayout/dex_ir.h
+++ b/dexlayout/dex_ir.h
@@ -926,8 +926,6 @@
   ClassData* GetClassData() { return class_data_; }
   EncodedArrayItem* StaticValues() { return static_values_; }
 
-  MethodItem* GenerateMethodItem(Header& header, ClassDataItemIterator& cdii);
-
   void Accept(AbstractDispatcher* dispatch) { dispatch->Dispatch(this); }
 
  private:
diff --git a/libdexfile/dex/class_accessor.h b/libdexfile/dex/class_accessor.h
index 9b9ac87..8ffeee4 100644
--- a/libdexfile/dex/class_accessor.h
+++ b/libdexfile/dex/class_accessor.h
@@ -350,6 +350,10 @@
     return class_def_index_;
   }
 
+  const DexFile::ClassDef& GetClassDef() const {
+    return dex_file_.GetClassDef(GetClassDefIndex());
+  }
+
  protected:
   // Template visitor to reduce copy paste for visiting elements.
   // No thread safety analysis since the visitor may require capabilities.
diff --git a/tools/veridex/flow_analysis.cc b/tools/veridex/flow_analysis.cc
index d4f7e5f..f5eb4ea 100644
--- a/tools/veridex/flow_analysis.cc
+++ b/tools/veridex/flow_analysis.cc
@@ -17,6 +17,7 @@
 #include "flow_analysis.h"
 
 #include "dex/bytecode_utils.h"
+#include "dex/class_accessor-inl.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_instruction-inl.h"
 #include "dex/dex_file-inl.h"
@@ -26,6 +27,13 @@
 
 namespace art {
 
+VeriFlowAnalysis::VeriFlowAnalysis(VeridexResolver* resolver,
+                                   const ClassAccessor::Method& method)
+    : resolver_(resolver),
+      method_id_(method.GetIndex()),
+      code_item_accessor_(method.GetInstructionsAndData()),
+      dex_registers_(code_item_accessor_.InsnsSizeInCodeUnits()),
+      instruction_infos_(code_item_accessor_.InsnsSizeInCodeUnits()) {}
 
 void VeriFlowAnalysis::SetAsBranchTarget(uint32_t dex_pc) {
   if (dex_registers_[dex_pc] == nullptr) {
diff --git a/tools/veridex/flow_analysis.h b/tools/veridex/flow_analysis.h
index fc09360..9c86024 100644
--- a/tools/veridex/flow_analysis.h
+++ b/tools/veridex/flow_analysis.h
@@ -17,6 +17,7 @@
 #ifndef ART_TOOLS_VERIDEX_FLOW_ANALYSIS_H_
 #define ART_TOOLS_VERIDEX_FLOW_ANALYSIS_H_
 
+#include "dex/class_accessor.h"
 #include "dex/code_item_accessors.h"
 #include "dex/dex_file_reference.h"
 #include "dex/method_reference.h"
@@ -108,12 +109,7 @@
 
 class VeriFlowAnalysis {
  public:
-  VeriFlowAnalysis(VeridexResolver* resolver, const ClassDataItemIterator& it)
-      : resolver_(resolver),
-        method_id_(it.GetMemberIndex()),
-        code_item_accessor_(resolver->GetDexFile(), it.GetMethodCodeItem()),
-        dex_registers_(code_item_accessor_.InsnsSizeInCodeUnits()),
-        instruction_infos_(code_item_accessor_.InsnsSizeInCodeUnits()) {}
+  VeriFlowAnalysis(VeridexResolver* resolver, const ClassAccessor::Method& method);
 
   void Run();
 
@@ -189,8 +185,8 @@
 // Collects all reflection uses.
 class FlowAnalysisCollector : public VeriFlowAnalysis {
  public:
-  FlowAnalysisCollector(VeridexResolver* resolver, const ClassDataItemIterator& it)
-      : VeriFlowAnalysis(resolver, it) {}
+  FlowAnalysisCollector(VeridexResolver* resolver, const ClassAccessor::Method& method)
+      : VeriFlowAnalysis(resolver, method) {}
 
   const std::vector<ReflectAccessInfo>& GetUses() const {
     return uses_;
@@ -208,9 +204,9 @@
 class FlowAnalysisSubstitutor : public VeriFlowAnalysis {
  public:
   FlowAnalysisSubstitutor(VeridexResolver* resolver,
-                          const ClassDataItemIterator& it,
+                          const ClassAccessor::Method& method,
                           const std::map<MethodReference, std::vector<ReflectAccessInfo>>& accesses)
-      : VeriFlowAnalysis(resolver, it), accesses_(accesses) {}
+      : VeriFlowAnalysis(resolver, method), accesses_(accesses) {}
 
   const std::vector<ReflectAccessInfo>& GetUses() const {
     return uses_;
diff --git a/tools/veridex/hidden_api_finder.cc b/tools/veridex/hidden_api_finder.cc
index 8c6139f..4eba10e 100644
--- a/tools/veridex/hidden_api_finder.cc
+++ b/tools/veridex/hidden_api_finder.cc
@@ -16,6 +16,7 @@
 
 #include "hidden_api_finder.h"
 
+#include "dex/class_accessor-inl.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_instruction-inl.h"
 #include "dex/dex_file.h"
@@ -62,23 +63,9 @@
   }
   // Note: we collect strings constants only referenced in code items as the string table
   // contains other kind of strings (eg types).
-  size_t class_def_count = dex_file.NumClassDefs();
-  for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
-    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
-    const uint8_t* class_data = dex_file.GetClassData(class_def);
-    if (class_data == nullptr) {
-      // Empty class.
-      continue;
-    }
-    ClassDataItemIterator it(dex_file, class_data);
-    it.SkipAllFields();
-    for (; it.HasNextMethod(); it.Next()) {
-      const DexFile::CodeItem* code_item = it.GetMethodCodeItem();
-      if (code_item == nullptr) {
-        continue;
-      }
-      CodeItemDataAccessor code_item_accessor(dex_file, code_item);
-      for (const DexInstructionPcPair& inst : code_item_accessor) {
+  for (ClassAccessor accessor : dex_file.GetClasses()) {
+    for (const ClassAccessor::Method& method : accessor.GetMethods()) {
+      for (const DexInstructionPcPair& inst : method.GetInstructions()) {
         switch (inst->Opcode()) {
           case Instruction::CONST_STRING: {
             dex::StringIndex string_index(inst->VRegB_21c());
@@ -103,8 +90,7 @@
                 // We only keep track of the location for strings, as these will be the
                 // field/method names the user is interested in.
                 strings_.insert(name);
-                reflection_locations_[name].push_back(
-                    MethodReference(&dex_file, it.GetMemberIndex()));
+                reflection_locations_[name].push_back(method.GetReference());
               }
             }
             break;
@@ -114,8 +100,7 @@
           case Instruction::INVOKE_STATIC:
           case Instruction::INVOKE_SUPER:
           case Instruction::INVOKE_VIRTUAL: {
-            CheckMethod(
-                inst->VRegB_35c(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckMethod(inst->VRegB_35c(), resolver, method.GetReference());
             break;
           }
 
@@ -124,8 +109,7 @@
           case Instruction::INVOKE_STATIC_RANGE:
           case Instruction::INVOKE_SUPER_RANGE:
           case Instruction::INVOKE_VIRTUAL_RANGE: {
-            CheckMethod(
-                inst->VRegB_3rc(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckMethod(inst->VRegB_3rc(), resolver, method.GetReference());
             break;
           }
 
@@ -136,8 +120,7 @@
           case Instruction::IGET_BYTE:
           case Instruction::IGET_CHAR:
           case Instruction::IGET_SHORT: {
-            CheckField(
-                inst->VRegC_22c(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckField(inst->VRegC_22c(), resolver, method.GetReference());
             break;
           }
 
@@ -148,8 +131,7 @@
           case Instruction::IPUT_BYTE:
           case Instruction::IPUT_CHAR:
           case Instruction::IPUT_SHORT: {
-            CheckField(
-                inst->VRegC_22c(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckField(inst->VRegC_22c(), resolver, method.GetReference());
             break;
           }
 
@@ -160,8 +142,7 @@
           case Instruction::SGET_BYTE:
           case Instruction::SGET_CHAR:
           case Instruction::SGET_SHORT: {
-            CheckField(
-                inst->VRegB_21c(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckField(inst->VRegB_21c(), resolver, method.GetReference());
             break;
           }
 
@@ -172,8 +153,7 @@
           case Instruction::SPUT_BYTE:
           case Instruction::SPUT_CHAR:
           case Instruction::SPUT_SHORT: {
-            CheckField(
-                inst->VRegB_21c(), resolver, MethodReference(&dex_file, it.GetMemberIndex()));
+            CheckField(inst->VRegB_21c(), resolver, method.GetReference());
             break;
           }
 
diff --git a/tools/veridex/precise_hidden_api_finder.cc b/tools/veridex/precise_hidden_api_finder.cc
index 89754c2..445221e 100644
--- a/tools/veridex/precise_hidden_api_finder.cc
+++ b/tools/veridex/precise_hidden_api_finder.cc
@@ -16,6 +16,7 @@
 
 #include "precise_hidden_api_finder.h"
 
+#include "dex/class_accessor-inl.h"
 #include "dex/code_item_accessors-inl.h"
 #include "dex/dex_instruction-inl.h"
 #include "dex/dex_file.h"
@@ -31,25 +32,13 @@
 
 void PreciseHiddenApiFinder::RunInternal(
     const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
-    const std::function<void(VeridexResolver*, const ClassDataItemIterator&)>& action) {
+    const std::function<void(VeridexResolver*, const ClassAccessor::Method&)>& action) {
   for (const std::unique_ptr<VeridexResolver>& resolver : resolvers) {
-    const DexFile& dex_file = resolver->GetDexFile();
-    size_t class_def_count = dex_file.NumClassDefs();
-    for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
-      const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
-      const uint8_t* class_data = dex_file.GetClassData(class_def);
-      if (class_data == nullptr) {
-        // Empty class.
-        continue;
-      }
-      ClassDataItemIterator it(dex_file, class_data);
-      it.SkipAllFields();
-      for (; it.HasNextMethod(); it.Next()) {
-        const DexFile::CodeItem* code_item = it.GetMethodCodeItem();
-        if (code_item == nullptr) {
-          continue;
+    for (ClassAccessor accessor : resolver->GetDexFile().GetClasses()) {
+      for (const ClassAccessor::Method& method : accessor.GetMethods()) {
+        if (method.GetCodeItem() != nullptr) {
+          action(resolver.get(), method);
         }
-        action(resolver.get(), it);
       }
     }
   }
@@ -68,10 +57,10 @@
 
 void PreciseHiddenApiFinder::Run(const std::vector<std::unique_ptr<VeridexResolver>>& resolvers) {
   // Collect reflection uses.
-  RunInternal(resolvers, [this] (VeridexResolver* resolver, const ClassDataItemIterator& it) {
-    FlowAnalysisCollector collector(resolver, it);
+  RunInternal(resolvers, [this] (VeridexResolver* resolver, const ClassAccessor::Method& method) {
+    FlowAnalysisCollector collector(resolver, method);
     collector.Run();
-    AddUsesAt(collector.GetUses(), MethodReference(&resolver->GetDexFile(), it.GetMemberIndex()));
+    AddUsesAt(collector.GetUses(), method.GetReference());
   });
 
   // For non-final reflection uses, do a limited fixed point calculation over the code to try
@@ -84,11 +73,11 @@
     std::map<MethodReference, std::vector<ReflectAccessInfo>> current_uses
         = std::move(abstract_uses_);
     RunInternal(resolvers,
-                [this, current_uses] (VeridexResolver* resolver, const ClassDataItemIterator& it) {
-      FlowAnalysisSubstitutor substitutor(resolver, it, current_uses);
+                [this, current_uses] (VeridexResolver* resolver,
+                                      const ClassAccessor::Method& method) {
+      FlowAnalysisSubstitutor substitutor(resolver, method, current_uses);
       substitutor.Run();
-      AddUsesAt(substitutor.GetUses(),
-                MethodReference(&resolver->GetDexFile(), it.GetMemberIndex()));
+      AddUsesAt(substitutor.GetUses(), method.GetReference());
     });
   }
 }
diff --git a/tools/veridex/precise_hidden_api_finder.h b/tools/veridex/precise_hidden_api_finder.h
index 1c4d0ae..8c5126c 100644
--- a/tools/veridex/precise_hidden_api_finder.h
+++ b/tools/veridex/precise_hidden_api_finder.h
@@ -48,7 +48,7 @@
   // Run over all methods of all dex files, and call `action` on each.
   void RunInternal(
       const std::vector<std::unique_ptr<VeridexResolver>>& resolvers,
-      const std::function<void(VeridexResolver*, const ClassDataItemIterator&)>& action);
+      const std::function<void(VeridexResolver*, const ClassAccessor::Method&)>& action);
 
   // Add uses found in method `ref`.
   void AddUsesAt(const std::vector<ReflectAccessInfo>& accesses, MethodReference ref);
diff --git a/tools/veridex/resolver.cc b/tools/veridex/resolver.cc
index 9113039..56729ff 100644
--- a/tools/veridex/resolver.cc
+++ b/tools/veridex/resolver.cc
@@ -16,6 +16,7 @@
 
 #include "resolver.h"
 
+#include "dex/class_accessor-inl.h"
 #include "dex/dex_file-inl.h"
 #include "dex/primitive.h"
 #include "hidden_api.h"
@@ -24,34 +25,22 @@
 namespace art {
 
 void VeridexResolver::Run() {
-  size_t class_def_count = dex_file_.NumClassDefs();
-  for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
-    const DexFile::ClassDef& class_def = dex_file_.GetClassDef(class_def_index);
-    std::string name(dex_file_.StringByTypeIdx(class_def.class_idx_));
+  for (ClassAccessor accessor : dex_file_.GetClasses()) {
+    std::string name(accessor.GetDescriptor());
     auto existing = type_map_.find(name);
+    const uint32_t type_idx = accessor.GetClassIdx().index_;
     if (existing != type_map_.end()) {
       // Class already exists, cache it and move on.
-      type_infos_[class_def.class_idx_.index_] = *existing->second;
+      type_infos_[type_idx] = *existing->second;
       continue;
     }
-    type_infos_[class_def.class_idx_.index_] = VeriClass(Primitive::Type::kPrimNot, 0, &class_def);
-    type_map_[name] = &(type_infos_[class_def.class_idx_.index_]);
-
-    const uint8_t* class_data = dex_file_.GetClassData(class_def);
-    if (class_data == nullptr) {
-      // Empty class.
-      continue;
+    type_infos_[type_idx] = VeriClass(Primitive::Type::kPrimNot, 0, &accessor.GetClassDef());
+    type_map_[name] = &type_infos_[type_idx];
+    for (const ClassAccessor::Field& field : accessor.GetFields()) {
+      field_infos_[field.GetIndex()] = field.GetDataPointer();
     }
-
-    ClassDataItemIterator it(dex_file_, class_data);
-    for (; it.HasNextStaticField(); it.Next()) {
-      field_infos_[it.GetMemberIndex()] = it.DataPointer();
-    }
-    for (; it.HasNextInstanceField(); it.Next()) {
-      field_infos_[it.GetMemberIndex()] = it.DataPointer();
-    }
-    for (; it.HasNextMethod(); it.Next()) {
-      method_infos_[it.GetMemberIndex()] = it.DataPointer();
+    for (const ClassAccessor::Method& method : accessor.GetMethods()) {
+      method_infos_[method.GetIndex()] = method.GetDataPointer();
     }
   }
 }
@@ -148,18 +137,14 @@
 
   // Look at methods declared in `kls`.
   const DexFile& other_dex_file = resolver->dex_file_;
-  const uint8_t* class_data = other_dex_file.GetClassData(*kls.GetClassDef());
-  if (class_data != nullptr) {
-    ClassDataItemIterator it(other_dex_file, class_data);
-    it.SkipAllFields();
-    for (; it.HasNextMethod(); it.Next()) {
-      const DexFile::MethodId& other_method_id = other_dex_file.GetMethodId(it.GetMemberIndex());
-      if (HasSameNameAndSignature(other_dex_file,
-                                  other_method_id,
-                                  method_name,
-                                  method_signature)) {
-        return it.DataPointer();
-      }
+  ClassAccessor other_dex_accessor(other_dex_file, *kls.GetClassDef());
+  for (const ClassAccessor::Method& method : other_dex_accessor.GetMethods()) {
+    const DexFile::MethodId& other_method_id = other_dex_file.GetMethodId(method.GetIndex());
+    if (HasSameNameAndSignature(other_dex_file,
+                                other_method_id,
+                                method_name,
+                                method_signature)) {
+      return method.GetDataPointer();
     }
   }
 
@@ -207,17 +192,14 @@
 
   // Look at fields declared in `kls`.
   const DexFile& other_dex_file = resolver->dex_file_;
-  const uint8_t* class_data = other_dex_file.GetClassData(*kls.GetClassDef());
-  if (class_data != nullptr) {
-    ClassDataItemIterator it(other_dex_file, class_data);
-    for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) {
-      const DexFile::FieldId& other_field_id = other_dex_file.GetFieldId(it.GetMemberIndex());
-      if (HasSameNameAndType(other_dex_file,
-                             other_field_id,
-                             field_name,
-                             field_type)) {
-        return it.DataPointer();
-      }
+  ClassAccessor other_dex_accessor(other_dex_file, *kls.GetClassDef());
+  for (const ClassAccessor::Field& field : other_dex_accessor.GetFields()) {
+    const DexFile::FieldId& other_field_id = other_dex_file.GetFieldId(field.GetIndex());
+    if (HasSameNameAndType(other_dex_file,
+                           other_field_id,
+                           field_name,
+                           field_type)) {
+      return field.GetDataPointer();
     }
   }
 
@@ -260,18 +242,13 @@
   }
   VeridexResolver* resolver = GetResolverOf(kls);
   const DexFile& other_dex_file = resolver->dex_file_;
-  const uint8_t* class_data = other_dex_file.GetClassData(*kls.GetClassDef());
-  if (class_data != nullptr) {
-    ClassDataItemIterator it(other_dex_file, class_data);
-    it.SkipAllFields();
-    for (; it.HasNextMethod(); it.Next()) {
-      const DexFile::MethodId& other_method_id = other_dex_file.GetMethodId(it.GetMemberIndex());
-      if (HasSameNameAndSignature(other_dex_file,
-                                  other_method_id,
-                                  method_name,
-                                  type)) {
-        return it.DataPointer();
-      }
+  ClassAccessor other_dex_accessor(other_dex_file, *kls.GetClassDef());
+  for (const ClassAccessor::Method& method : other_dex_accessor.GetMethods()) {
+    if (HasSameNameAndSignature(other_dex_file,
+                                other_dex_file.GetMethodId(method.GetIndex()),
+                                method_name,
+                                type)) {
+      return method.GetDataPointer();
     }
   }
   return nullptr;