summaryrefslogtreecommitdiffstats
path: root/runtime/dex_method_iterator.h
blob: 7fae277c14c69e78dc6c4f7421460f58a755879b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_DEX_METHOD_ITERATOR_H_
#define ART_RUNTIME_DEX_METHOD_ITERATOR_H_

#include <vector>

#include "dex_file.h"

namespace art {

class DexMethodIterator {
 public:
  explicit DexMethodIterator(const std::vector<const DexFile*>& dex_files)
      : dex_files_(dex_files),
        found_next_(false),
        dex_file_index_(0),
        class_def_index_(0),
        class_def_(nullptr),
        class_data_(nullptr),
        direct_method_(false) {
    CHECK_NE(0U, dex_files_.size());
  }

  bool HasNext() {
    if (found_next_) {
      return true;
    }
    while (true) {
      // End of DexFiles, we are done.
      if (dex_file_index_ == dex_files_.size()) {
        return false;
      }
      if (class_def_index_ == GetDexFileInternal().NumClassDefs()) {
        // End of this DexFile, advance and retry.
        class_def_index_ = 0;
        dex_file_index_++;
        continue;
      }
      if (class_def_ == nullptr) {
        class_def_ = &GetDexFileInternal().GetClassDef(class_def_index_);
      }
      if (class_data_ == nullptr) {
        class_data_ = GetDexFileInternal().GetClassData(*class_def_);
        if (class_data_ == nullptr) {
          // empty class, such as a marker interface
          // End of this class, advance and retry.
          class_def_ = nullptr;
          class_def_index_++;
          continue;
        }
      }
      if (it_.get() == nullptr) {
        it_.reset(new ClassDataItemIterator(GetDexFileInternal(), class_data_));
        // Skip fields
        while (GetIterator().HasNextStaticField()) {
          GetIterator().Next();
        }
        while (GetIterator().HasNextInstanceField()) {
          GetIterator().Next();
        }
        direct_method_ = true;
      }
      if (direct_method_ && GetIterator().HasNextDirectMethod()) {
        // Found method
        found_next_ = true;
        return true;
      }
      direct_method_ = false;
      if (GetIterator().HasNextVirtualMethod()) {
        // Found method
        found_next_ = true;
        return true;
      }
      // End of this class, advance and retry.
      DCHECK(!GetIterator().HasNext());
      it_.reset(nullptr);
      class_data_ = nullptr;
      class_def_ = nullptr;
      class_def_index_++;
    }
  }

  void Next() {
    found_next_ = false;
    if (it_.get() != nullptr) {
      // Advance to next method if we currently are looking at a class.
      GetIterator().Next();
    }
  }

  const DexFile& GetDexFile() {
    CHECK(HasNext());
    return GetDexFileInternal();
  }

  uint32_t GetMemberIndex() {
    CHECK(HasNext());
    return GetIterator().GetMemberIndex();
  }

  InvokeType GetInvokeType() {
    CHECK(HasNext());
    CHECK(class_def_ != nullptr);
    return GetIterator().GetMethodInvokeType(*class_def_);
  }

 private:
  ClassDataItemIterator& GetIterator() const {
    CHECK(it_.get() != nullptr);
    return *it_.get();
  }

  const DexFile& GetDexFileInternal() const {
    CHECK_LT(dex_file_index_, dex_files_.size());
    const DexFile* dex_file = dex_files_[dex_file_index_];
    CHECK(dex_file != nullptr);
    return *dex_file;
  }

  const std::vector<const DexFile*>& dex_files_;

  bool found_next_;

  uint32_t dex_file_index_;
  uint32_t class_def_index_;
  const DexFile::ClassDef* class_def_;
  const uint8_t* class_data_;
  std::unique_ptr<ClassDataItemIterator> it_;
  bool direct_method_;
};

}  // namespace art

#endif  // ART_RUNTIME_DEX_METHOD_ITERATOR_H_