Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -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_OBJECT_UTILS_H_ |
| 18 | #define ART_RUNTIME_OBJECT_UTILS_H_ |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 19 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 20 | #include "class_linker.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "dex_file.h" |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 22 | #include "monitor.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field.h" |
| 24 | #include "mirror/art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class.h" |
| 26 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/iftable.h" |
Sebastien Hertz | 80989a6 | 2014-04-01 14:39:44 +0200 | [diff] [blame] | 28 | #include "mirror/proxy.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/string.h" |
| 30 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 31 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 32 | #include "handle_scope-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 33 | |
| 34 | #include <string> |
| 35 | |
| 36 | namespace art { |
| 37 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 38 | template <typename T> |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 39 | class ObjectLock { |
| 40 | public: |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 41 | ObjectLock(Thread* self, Handle<T> object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 42 | : self_(self), obj_(object) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 43 | CHECK(object.Get() != nullptr); |
| 44 | obj_->MonitorEnter(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 47 | ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 48 | obj_->MonitorExit(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 51 | void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 52 | Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 55 | void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 56 | obj_->Notify(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 59 | void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 60 | obj_->NotifyAll(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | Thread* const self_; |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 65 | Handle<T> const obj_; |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 66 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
| 67 | }; |
| 68 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 69 | class FieldHelper { |
| 70 | public: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 71 | FieldHelper() : field_(nullptr) {} |
| 72 | explicit FieldHelper(mirror::ArtField* f) : field_(f) {} |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 73 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 74 | void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 75 | DCHECK(new_f != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 76 | field_ = new_f; |
| 77 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 78 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 79 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 80 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 81 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 82 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 83 | DCHECK_LT(field_index, 2U); |
| 84 | return field_index == 0 ? "interfaces" : "throws"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 85 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 86 | const DexFile& dex_file = GetDexFile(); |
| 87 | return dex_file.GetFieldName(dex_file.GetFieldId(field_index)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 88 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 89 | |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 90 | mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 91 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 92 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 93 | return GetClassLinker()->FindSystemClass(Thread::Current(), GetTypeDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 94 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 95 | const DexFile& dex_file = GetDexFile(); |
| 96 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 97 | mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 98 | if (resolve && (type == nullptr)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 99 | type = GetClassLinker()->ResolveType(field_id.type_idx_, field_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 100 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 101 | } |
| 102 | return type; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 103 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 104 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 105 | const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 106 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 107 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 108 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 109 | DCHECK_LT(field_index, 2U); |
| 110 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
| 111 | return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 112 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 113 | const DexFile& dex_file = GetDexFile(); |
| 114 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 115 | return dex_file.GetFieldTypeDescriptor(field_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 116 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 117 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | Primitive::Type GetTypeAsPrimitiveType() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 119 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 120 | return Primitive::GetType(GetTypeDescriptor()[0]); |
| 121 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 122 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 123 | bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 124 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 125 | return type != Primitive::kPrimNot; |
| 126 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 127 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 128 | size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 129 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 130 | return Primitive::FieldSize(type); |
| 131 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 132 | |
| 133 | // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper. |
| 134 | // If you need it longer, copy it into a std::string. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 136 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 137 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 138 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 139 | DCHECK(field_->IsStatic()); |
| 140 | DCHECK_LT(field_index, 2U); |
| 141 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 142 | declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 143 | return declaring_class_descriptor_.c_str(); |
| 144 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 145 | const DexFile& dex_file = GetDexFile(); |
| 146 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 147 | return dex_file.GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 151 | mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 152 | return field_->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 153 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 154 | ClassLinker* GetClassLinker() ALWAYS_INLINE { |
| 155 | return Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 156 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 157 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 158 | return *GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 159 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 160 | mirror::ArtField* field_; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 161 | std::string declaring_class_descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(FieldHelper); |
| 164 | }; |
| 165 | |
| 166 | class MethodHelper { |
| 167 | public: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 168 | MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 169 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 170 | explicit MethodHelper(mirror::ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 171 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 172 | : method_(nullptr), shorty_(nullptr), shorty_len_(0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 173 | SetMethod(m); |
| 174 | } |
| 175 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 176 | void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 177 | DCHECK(new_m != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 178 | SetMethod(new_m); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 179 | shorty_ = nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 180 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 181 | |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 182 | mirror::ArtMethod* GetMethod() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 183 | return method_; |
| 184 | } |
| 185 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 186 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 187 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 188 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 189 | if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 190 | return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); |
| 191 | } else { |
| 192 | Runtime* runtime = Runtime::Current(); |
| 193 | if (method_ == runtime->GetResolutionMethod()) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 194 | return "<runtime internal resolution method>"; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 195 | } else if (method_ == runtime->GetImtConflictMethod()) { |
| 196 | return "<runtime internal imt conflict method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 197 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 198 | return "<runtime internal callee-save all registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 199 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 200 | return "<runtime internal callee-save reference registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 201 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 202 | return "<runtime internal callee-save reference and argument registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 203 | } else { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 204 | return "<unknown runtime internal method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 207 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 208 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 210 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 211 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 212 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 213 | StackHandleScope<1> hs(Thread::Current()); |
| 214 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 215 | return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 216 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 217 | |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 218 | const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 219 | const char* result = shorty_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 220 | if (result == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 221 | const DexFile& dex_file = GetDexFile(); |
| 222 | result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()), |
| 223 | &shorty_len_); |
| 224 | shorty_ = result; |
| 225 | } |
| 226 | return result; |
| 227 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 228 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 229 | uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 230 | if (shorty_ == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 231 | GetShorty(); |
| 232 | } |
| 233 | return shorty_len_; |
| 234 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 235 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 236 | // Counts the number of references in the parameter list of the corresponding method. |
| 237 | // Note: Thus does _not_ include "this" for non-static methods. |
| 238 | uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 239 | const char* shorty = GetShorty(); |
| 240 | uint32_t refs = 0; |
| 241 | for (uint32_t i = 1; i < shorty_len_ ; ++i) { |
| 242 | if (shorty[i] == 'L') { |
| 243 | refs++; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return refs; |
| 248 | } |
| 249 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 250 | const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 251 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 252 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 253 | if (dex_method_idx != DexFile::kDexNoIndex) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 254 | return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx)); |
| 255 | } else { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 256 | return Signature::NoSignature(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 257 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 258 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 259 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 260 | const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 261 | const DexFile& dex_file = GetDexFile(); |
| 262 | return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex())); |
| 263 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 264 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 265 | const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 266 | const DexFile::ProtoId& proto = GetPrototype(); |
| 267 | return GetDexFile().GetProtoParameters(proto); |
| 268 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 269 | |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 270 | mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 271 | const DexFile& dex_file = GetDexFile(); |
| 272 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 273 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 274 | uint16_t return_type_idx = proto_id.return_type_idx_; |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 275 | return GetClassFromTypeIdx(return_type_idx, resolve); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 276 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 277 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 278 | const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 279 | const DexFile& dex_file = GetDexFile(); |
| 280 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 281 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 282 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 283 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx)); |
| 284 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 285 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 286 | int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 287 | if (dex_pc == DexFile::kDexNoIndex) { |
| 288 | return method_->IsNative() ? -2 : -1; |
| 289 | } else { |
| 290 | const DexFile& dex_file = GetDexFile(); |
| 291 | return dex_file.GetLineNumFromPC(method_, dex_pc); |
| 292 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 293 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 294 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 295 | const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 296 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 297 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 298 | if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 299 | return "<runtime method>"; |
| 300 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 301 | return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx)); |
| 302 | } |
| 303 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 304 | const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 305 | return method_->GetDeclaringClass()->GetSourceFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 306 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 307 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 308 | uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 309 | return method_->GetDeclaringClass()->GetDexClassDefIndex(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 312 | const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 313 | return GetDexFile().GetClassDef(GetClassDefIndex()); |
| 314 | } |
| 315 | |
| 316 | mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 317 | return method_->GetDeclaringClass()->GetClassLoader(); |
| 318 | } |
| 319 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 320 | bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 321 | return method_->IsStatic(); |
| 322 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 323 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 324 | bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 325 | return method_->IsConstructor() && IsStatic(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 326 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 327 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 328 | size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 329 | // "1 +" because the first in Args is the receiver. |
| 330 | // "- 1" because we don't count the return type. |
| 331 | return (IsStatic() ? 0 : 1) + GetShortyLength() - 1; |
| 332 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 333 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 334 | // Get the primitive type associated with the given parameter. |
| 335 | Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 336 | CHECK_LT(param, NumArgs()); |
| 337 | if (IsStatic()) { |
| 338 | param++; // 0th argument must skip return value at start of the shorty |
| 339 | } else if (param == 0) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 340 | return Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 341 | } |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 342 | return Primitive::GetType(GetShorty()[param]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 343 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 344 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 345 | // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods. |
| 346 | bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 347 | Primitive::Type type = GetParamPrimitiveType(param); |
| 348 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 349 | } |
| 350 | |
| 351 | // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 352 | bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 353 | return GetParamPrimitiveType(param) == Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 354 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 355 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 356 | bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 357 | const DexFile& dex_file = GetDexFile(); |
| 358 | const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 359 | if (GetDexCache() == other->GetDexCache()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 360 | const DexFile::MethodId& other_mid = |
| 361 | dex_file.GetMethodId(other->method_->GetDexMethodIndex()); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 362 | return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 363 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 364 | const DexFile& other_dex_file = other->GetDexFile(); |
| 365 | const DexFile::MethodId& other_mid = |
| 366 | other_dex_file.GetMethodId(other->method_->GetDexMethodIndex()); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 367 | if (!DexFileStringEquals(&dex_file, mid.name_idx_, |
| 368 | &other_dex_file, other_mid.name_idx_)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 369 | return false; // Name mismatch. |
| 370 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 371 | return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 372 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 373 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 374 | bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other) |
| 375 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 376 | if (UNLIKELY(GetReturnType() != other->GetReturnType())) { |
| 377 | return false; |
| 378 | } |
| 379 | const DexFile::TypeList* types = GetParameterTypeList(); |
| 380 | const DexFile::TypeList* other_types = other->GetParameterTypeList(); |
| 381 | if (types == nullptr) { |
| 382 | return (other_types == nullptr) || (other_types->Size() == 0); |
| 383 | } else if (UNLIKELY(other_types == nullptr)) { |
| 384 | return types->Size() == 0; |
| 385 | } |
| 386 | uint32_t num_types = types->Size(); |
| 387 | if (UNLIKELY(num_types != other_types->Size())) { |
| 388 | return false; |
| 389 | } |
| 390 | for (uint32_t i = 0; i < num_types; ++i) { |
| 391 | mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_); |
| 392 | mirror::Class* other_param_type = |
| 393 | other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_); |
| 394 | if (UNLIKELY(param_type != other_param_type)) { |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 401 | const DexFile::CodeItem* GetCodeItem() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 402 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 403 | return GetDexFile().GetCodeItem(method_->GetCodeItemOffset()); |
| 404 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 405 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 406 | bool IsResolvedTypeIdx(uint16_t type_idx) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 407 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 408 | return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr; |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 409 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 410 | |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 411 | mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 412 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 413 | mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 414 | if (type == nullptr && resolve) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 415 | type = GetClassLinker()->ResolveType(type_idx, method_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 416 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 417 | } |
| 418 | return type; |
| 419 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 420 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 421 | const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 422 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 423 | const DexFile& dex_file = GetDexFile(); |
| 424 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)); |
| 425 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 426 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 427 | mirror::Class* GetDexCacheResolvedType(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 428 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 429 | return method_->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 430 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 431 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 432 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 433 | return *GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 434 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 435 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 436 | mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 437 | return method_->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 438 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 439 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 440 | mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 441 | mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 442 | if (UNLIKELY(s == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 443 | StackHandleScope<1> hs(Thread::Current()); |
| 444 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 445 | s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 446 | } |
| 447 | return s; |
| 448 | } |
| 449 | |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 450 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile) |
| 451 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 452 | const DexFile& dexfile = GetDexFile(); |
| 453 | if (&dexfile == &other_dexfile) { |
| 454 | return method_->GetDexMethodIndex(); |
| 455 | } |
| 456 | const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex()); |
| 457 | const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_); |
| 458 | const DexFile::StringId* other_descriptor = |
| 459 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 460 | if (other_descriptor != nullptr) { |
| 461 | const DexFile::TypeId* other_type_id = |
| 462 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 463 | if (other_type_id != nullptr) { |
| 464 | const char* mid_name = dexfile.GetMethodName(mid); |
| 465 | const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name); |
| 466 | if (other_name != nullptr) { |
| 467 | uint16_t other_return_type_idx; |
| 468 | std::vector<uint16_t> other_param_type_idxs; |
| 469 | bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(), |
| 470 | &other_return_type_idx, |
| 471 | &other_param_type_idxs); |
| 472 | if (success) { |
| 473 | const DexFile::ProtoId* other_sig = |
| 474 | other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs); |
| 475 | if (other_sig != nullptr) { |
| 476 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id, |
| 477 | *other_name, |
| 478 | *other_sig); |
| 479 | if (other_mid != nullptr) { |
| 480 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | return DexFile::kDexNoIndex; |
| 488 | } |
| 489 | |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 490 | // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the |
| 491 | // other_dexfile, such as the method index used to resolve this method in the other_dexfile. |
| 492 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 493 | uint32_t name_and_signature_idx) |
| 494 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 495 | const DexFile& dexfile = GetDexFile(); |
| 496 | const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex()); |
| 497 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
| 498 | DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 499 | DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 500 | if (&dexfile == &other_dexfile) { |
| 501 | return method_->GetDexMethodIndex(); |
| 502 | } |
| 503 | const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_); |
| 504 | const DexFile::StringId* other_descriptor = |
| 505 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 506 | if (other_descriptor != nullptr) { |
| 507 | const DexFile::TypeId* other_type_id = |
| 508 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 509 | if (other_type_id != nullptr) { |
| 510 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 511 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 512 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 513 | if (other_mid != nullptr) { |
| 514 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return DexFile::kDexNoIndex; |
| 519 | } |
| 520 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 521 | private: |
| 522 | // Set the method_ field, for proxy methods looking up the interface method via the resolved |
| 523 | // methods table. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 524 | void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 525 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 526 | mirror::Class* klass = method->GetDeclaringClass(); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 527 | if (UNLIKELY(klass->IsProxyClass())) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 528 | mirror::ArtMethod* interface_method = |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 529 | method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 530 | DCHECK(interface_method != nullptr); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 531 | DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 532 | method = interface_method; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | method_ = method; |
| 536 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 537 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 538 | ClassLinker* GetClassLinker() ALWAYS_INLINE { |
| 539 | return Runtime::Current()->GetClassLinker(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 542 | mirror::ArtMethod* method_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 543 | const char* shorty_; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 544 | uint32_t shorty_len_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 545 | |
| 546 | DISALLOW_COPY_AND_ASSIGN(MethodHelper); |
| 547 | }; |
| 548 | |
| 549 | } // namespace art |
| 550 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 551 | #endif // ART_RUNTIME_OBJECT_UTILS_H_ |