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: |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 71 | explicit FieldHelper(Handle<mirror::ArtField> f) : field_(f) {} |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 72 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 73 | void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 74 | DCHECK(new_f != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 75 | field_.Assign(new_f); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 76 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 77 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 78 | mirror::ArtField* GetField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 79 | return field_.Get(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 80 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 81 | |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 82 | mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 83 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 84 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 85 | return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(), |
| 86 | field_->GetTypeDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 87 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 88 | const DexFile* dex_file = field_->GetDexFile(); |
| 89 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
| 90 | mirror::Class* type = field_->GetDexCache()->GetResolvedType(field_id.type_idx_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | if (resolve && (type == nullptr)) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 92 | type = Runtime::Current()->GetClassLinker()->ResolveType(field_id.type_idx_, field_.Get()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 94 | } |
| 95 | return type; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 96 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 97 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 98 | // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper. |
| 99 | // If you need it longer, copy it into a std::string. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 100 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 102 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 103 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 104 | DCHECK(field_->IsStatic()); |
| 105 | DCHECK_LT(field_index, 2U); |
| 106 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 107 | declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 108 | return declaring_class_descriptor_.c_str(); |
| 109 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 110 | const DexFile* dex_file = field_->GetDexFile(); |
| 111 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
| 112 | return dex_file->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private: |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 116 | Handle<mirror::ArtField> field_; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 117 | std::string declaring_class_descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(FieldHelper); |
| 120 | }; |
| 121 | |
| 122 | class MethodHelper { |
| 123 | public: |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 124 | explicit MethodHelper(Handle<mirror::ArtMethod> m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 125 | : method_(m), shorty_(nullptr), shorty_len_(0) { |
| 126 | SetMethod(m.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 129 | void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 130 | DCHECK(new_m != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 131 | SetMethod(new_m); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 132 | shorty_ = nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 133 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 134 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 135 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 136 | return method_->GetInterfaceMethodIfProxy(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 139 | mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 140 | const DexFile* dex_file = method_->GetDexFile(); |
| 141 | mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy(); |
| 142 | uint32_t dex_method_idx = method->GetDexMethodIndex(); |
| 143 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
| 144 | StackHandleScope<1> hs(self); |
| 145 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 146 | return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_, |
| 147 | dex_cache); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 148 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 149 | |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 150 | const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 151 | const char* result = shorty_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 152 | if (result == nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 153 | result = method_->GetShorty(&shorty_len_); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 154 | shorty_ = result; |
| 155 | } |
| 156 | return result; |
| 157 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 158 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 159 | uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 160 | if (shorty_ == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 161 | GetShorty(); |
| 162 | } |
| 163 | return shorty_len_; |
| 164 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 165 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 166 | // Counts the number of references in the parameter list of the corresponding method. |
| 167 | // Note: Thus does _not_ include "this" for non-static methods. |
| 168 | uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 169 | const char* shorty = GetShorty(); |
| 170 | uint32_t refs = 0; |
| 171 | for (uint32_t i = 1; i < shorty_len_ ; ++i) { |
| 172 | if (shorty[i] == 'L') { |
| 173 | refs++; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return refs; |
| 178 | } |
| 179 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 180 | // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large |
| 181 | // number of bugs at call sites. |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 182 | mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 183 | mirror::ArtMethod* method = GetMethod(); |
| 184 | const DexFile* dex_file = method->GetDexFile(); |
| 185 | const DexFile::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex()); |
| 186 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 187 | uint16_t return_type_idx = proto_id.return_type_idx_; |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 188 | return GetClassFromTypeIdx(return_type_idx, resolve); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 189 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 190 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 191 | size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 192 | // "1 +" because the first in Args is the receiver. |
| 193 | // "- 1" because we don't count the return type. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 194 | return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 195 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 196 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 197 | // Get the primitive type associated with the given parameter. |
| 198 | Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 199 | CHECK_LT(param, NumArgs()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 200 | if (GetMethod()->IsStatic()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 201 | param++; // 0th argument must skip return value at start of the shorty |
| 202 | } else if (param == 0) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 203 | return Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 204 | } |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 205 | return Primitive::GetType(GetShorty()[param]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 206 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 207 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 208 | // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods. |
| 209 | bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 210 | Primitive::Type type = GetParamPrimitiveType(param); |
| 211 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 212 | } |
| 213 | |
| 214 | // 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] | 215 | bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 216 | return GetParamPrimitiveType(param) == Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 217 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 218 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 219 | bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 220 | const DexFile* dex_file = method_->GetDexFile(); |
| 221 | const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex()); |
| 222 | if (method_->GetDexCache() == other->method_->GetDexCache()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 223 | const DexFile::MethodId& other_mid = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 224 | dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex()); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 225 | 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] | 226 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 227 | const DexFile* other_dex_file = other->method_->GetDexFile(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 228 | const DexFile::MethodId& other_mid = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 229 | other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex()); |
| 230 | if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 231 | return false; // Name mismatch. |
| 232 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 233 | return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 234 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 235 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 236 | bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other) |
| 237 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 238 | if (UNLIKELY(GetReturnType() != other->GetReturnType())) { |
| 239 | return false; |
| 240 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 241 | const DexFile::TypeList* types = method_->GetParameterTypeList(); |
| 242 | const DexFile::TypeList* other_types = other->method_->GetParameterTypeList(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 243 | if (types == nullptr) { |
| 244 | return (other_types == nullptr) || (other_types->Size() == 0); |
| 245 | } else if (UNLIKELY(other_types == nullptr)) { |
| 246 | return types->Size() == 0; |
| 247 | } |
| 248 | uint32_t num_types = types->Size(); |
| 249 | if (UNLIKELY(num_types != other_types->Size())) { |
| 250 | return false; |
| 251 | } |
| 252 | for (uint32_t i = 0; i < num_types; ++i) { |
| 253 | mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_); |
| 254 | mirror::Class* other_param_type = |
| 255 | other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_); |
| 256 | if (UNLIKELY(param_type != other_param_type)) { |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 263 | mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 264 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 265 | mirror::ArtMethod* method = GetMethod(); |
| 266 | mirror::Class* type = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 267 | if (type == nullptr && resolve) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 268 | type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 269 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 270 | } |
| 271 | return type; |
| 272 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 273 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 274 | mirror::Class* GetDexCacheResolvedType(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 275 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 276 | return GetMethod()->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 277 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 278 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 279 | mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 280 | mirror::ArtMethod* method = GetMethod(); |
| 281 | mirror::String* s = method->GetDexCacheStrings()->Get(string_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 282 | if (UNLIKELY(s == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 283 | StackHandleScope<1> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 284 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 285 | s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx, |
| 286 | dex_cache); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 287 | } |
| 288 | return s; |
| 289 | } |
| 290 | |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 291 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile) |
| 292 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 293 | mirror::ArtMethod* method = GetMethod(); |
| 294 | const DexFile* dexfile = method->GetDexFile(); |
| 295 | if (dexfile == &other_dexfile) { |
| 296 | return method->GetDexMethodIndex(); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 297 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 298 | const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex()); |
| 299 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 300 | const DexFile::StringId* other_descriptor = |
| 301 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 302 | if (other_descriptor != nullptr) { |
| 303 | const DexFile::TypeId* other_type_id = |
| 304 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 305 | if (other_type_id != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 306 | const char* mid_name = dexfile->GetMethodName(mid); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 307 | const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name); |
| 308 | if (other_name != nullptr) { |
| 309 | uint16_t other_return_type_idx; |
| 310 | std::vector<uint16_t> other_param_type_idxs; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 311 | bool success = other_dexfile.CreateTypeList( |
| 312 | dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx, |
| 313 | &other_param_type_idxs); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 314 | if (success) { |
| 315 | const DexFile::ProtoId* other_sig = |
| 316 | other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs); |
| 317 | if (other_sig != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 318 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 319 | *other_type_id, *other_name, *other_sig); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 320 | if (other_mid != nullptr) { |
| 321 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | return DexFile::kDexNoIndex; |
| 329 | } |
| 330 | |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 331 | // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the |
| 332 | // other_dexfile, such as the method index used to resolve this method in the other_dexfile. |
| 333 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 334 | uint32_t name_and_signature_idx) |
| 335 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 336 | mirror::ArtMethod* method = GetMethod(); |
| 337 | const DexFile* dexfile = method->GetDexFile(); |
| 338 | const uint32_t dex_method_idx = method->GetDexMethodIndex(); |
| 339 | const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 340 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 341 | DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 342 | DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 343 | if (dexfile == &other_dexfile) { |
| 344 | return dex_method_idx; |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 345 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 346 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 347 | const DexFile::StringId* other_descriptor = |
| 348 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 349 | if (other_descriptor != nullptr) { |
| 350 | const DexFile::TypeId* other_type_id = |
| 351 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 352 | if (other_type_id != nullptr) { |
| 353 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 354 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 355 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 356 | if (other_mid != nullptr) { |
| 357 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | return DexFile::kDexNoIndex; |
| 362 | } |
| 363 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 364 | private: |
| 365 | // Set the method_ field, for proxy methods looking up the interface method via the resolved |
| 366 | // methods table. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 367 | void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 368 | method_.Assign(method); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 369 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 370 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 371 | Handle<mirror::ArtMethod> method_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 372 | const char* shorty_; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 373 | uint32_t shorty_len_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 374 | |
| 375 | DISALLOW_COPY_AND_ASSIGN(MethodHelper); |
| 376 | }; |
| 377 | |
| 378 | } // namespace art |
| 379 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 380 | #endif // ART_RUNTIME_OBJECT_UTILS_H_ |