Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_METHOD_ITERATOR_H_ |
| 18 | #define ART_RUNTIME_DEX_METHOD_ITERATOR_H_ |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "dex_file.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class DexMethodIterator { |
| 27 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 28 | explicit DexMethodIterator(const std::vector<const DexFile*>& dex_files) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 29 | : dex_files_(dex_files), |
| 30 | found_next_(false), |
| 31 | dex_file_index_(0), |
| 32 | class_def_index_(0), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 33 | class_def_(nullptr), |
| 34 | class_data_(nullptr), |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 35 | 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 54 | if (class_def_ == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 55 | class_def_ = &GetDexFileInternal().GetClassDef(class_def_index_); |
| 56 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | if (class_data_ == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 58 | class_data_ = GetDexFileInternal().GetClassData(*class_def_); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 59 | if (class_data_ == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 60 | // empty class, such as a marker interface |
| 61 | // End of this class, advance and retry. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 62 | class_def_ = nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 63 | class_def_index_++; |
| 64 | continue; |
| 65 | } |
| 66 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 67 | if (it_.get() == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 68 | it_.reset(new ClassDataItemIterator(GetDexFileInternal(), class_data_)); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 69 | GetIterator().SkipAllFields(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 70 | 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 85 | it_.reset(nullptr); |
| 86 | class_data_ = nullptr; |
| 87 | class_def_ = nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 88 | class_def_index_++; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void Next() { |
| 93 | found_next_ = false; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 94 | if (it_.get() != nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 95 | // 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 112 | CHECK(class_def_ != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 113 | return GetIterator().GetMethodInvokeType(*class_def_); |
| 114 | } |
| 115 | |
| 116 | private: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 117 | ClassDataItemIterator& GetIterator() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 118 | CHECK(it_.get() != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 119 | 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 125 | CHECK(dex_file != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 126 | 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 Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 136 | const uint8_t* class_data_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 137 | std::unique_ptr<ClassDataItemIterator> it_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 138 | bool direct_method_; |
| 139 | }; |
| 140 | |
| 141 | } // namespace art |
| 142 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 143 | #endif // ART_RUNTIME_DEX_METHOD_ITERATOR_H_ |