blob: 8a4bed31b1c0375e638cf336dbf0aeb819036bfe [file] [log] [blame]
Brian Carlstrom265091e2013-01-30 14:08:26 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_METHOD_ITERATOR_H_
18#define ART_RUNTIME_DEX_METHOD_ITERATOR_H_
Brian Carlstrom265091e2013-01-30 14:08:26 -080019
20#include <vector>
21
22#include "dex_file.h"
23
24namespace art {
25
26class DexMethodIterator {
27 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070028 explicit DexMethodIterator(const std::vector<const DexFile*>& dex_files)
Brian Carlstrom265091e2013-01-30 14:08:26 -080029 : dex_files_(dex_files),
30 found_next_(false),
31 dex_file_index_(0),
32 class_def_index_(0),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070033 class_def_(nullptr),
34 class_data_(nullptr),
Brian Carlstrom265091e2013-01-30 14:08:26 -080035 direct_method_(false) {
36 CHECK_NE(0U, dex_files_.size());
37 }
38
39 bool HasNext() {
40 if (found_next_) {
41 return true;
42 }
43 while (true) {
44 // End of DexFiles, we are done.
45 if (dex_file_index_ == dex_files_.size()) {
46 return false;
47 }
48 if (class_def_index_ == GetDexFileInternal().NumClassDefs()) {
49 // End of this DexFile, advance and retry.
50 class_def_index_ = 0;
51 dex_file_index_++;
52 continue;
53 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070054 if (class_def_ == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080055 class_def_ = &GetDexFileInternal().GetClassDef(class_def_index_);
56 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070057 if (class_data_ == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080058 class_data_ = GetDexFileInternal().GetClassData(*class_def_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 if (class_data_ == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080060 // empty class, such as a marker interface
61 // End of this class, advance and retry.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070062 class_def_ = nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -080063 class_def_index_++;
64 continue;
65 }
66 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 if (it_.get() == nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080068 it_.reset(new ClassDataItemIterator(GetDexFileInternal(), class_data_));
Mathieu Chartiere17cf242017-06-19 11:05:51 -070069 GetIterator().SkipAllFields();
Brian Carlstrom265091e2013-01-30 14:08:26 -080070 direct_method_ = true;
71 }
72 if (direct_method_ && GetIterator().HasNextDirectMethod()) {
73 // Found method
74 found_next_ = true;
75 return true;
76 }
77 direct_method_ = false;
78 if (GetIterator().HasNextVirtualMethod()) {
79 // Found method
80 found_next_ = true;
81 return true;
82 }
83 // End of this class, advance and retry.
84 DCHECK(!GetIterator().HasNext());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 it_.reset(nullptr);
86 class_data_ = nullptr;
87 class_def_ = nullptr;
Brian Carlstrom265091e2013-01-30 14:08:26 -080088 class_def_index_++;
89 }
90 }
91
92 void Next() {
93 found_next_ = false;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070094 if (it_.get() != nullptr) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080095 // Advance to next method if we currently are looking at a class.
96 GetIterator().Next();
97 }
98 }
99
100 const DexFile& GetDexFile() {
101 CHECK(HasNext());
102 return GetDexFileInternal();
103 }
104
105 uint32_t GetMemberIndex() {
106 CHECK(HasNext());
107 return GetIterator().GetMemberIndex();
108 }
109
110 InvokeType GetInvokeType() {
111 CHECK(HasNext());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 CHECK(class_def_ != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800113 return GetIterator().GetMethodInvokeType(*class_def_);
114 }
115
116 private:
Brian Carlstrom265091e2013-01-30 14:08:26 -0800117 ClassDataItemIterator& GetIterator() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 CHECK(it_.get() != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800119 return *it_.get();
120 }
121
122 const DexFile& GetDexFileInternal() const {
123 CHECK_LT(dex_file_index_, dex_files_.size());
124 const DexFile* dex_file = dex_files_[dex_file_index_];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700125 CHECK(dex_file != nullptr);
Brian Carlstrom265091e2013-01-30 14:08:26 -0800126 return *dex_file;
127 }
128
129 const std::vector<const DexFile*>& dex_files_;
130
131 bool found_next_;
132
133 uint32_t dex_file_index_;
134 uint32_t class_def_index_;
135 const DexFile::ClassDef* class_def_;
Ian Rogers13735952014-10-08 12:43:28 -0700136 const uint8_t* class_data_;
Ian Rogers700a4022014-05-19 16:49:03 -0700137 std::unique_ptr<ClassDataItemIterator> it_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800138 bool direct_method_;
139};
140
141} // namespace art
142
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700143#endif // ART_RUNTIME_DEX_METHOD_ITERATOR_H_