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 | d8274bc | 2013-05-15 15:54:45 -0700 | [diff] [blame] | 20 | #include "class_linker-inl.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" |
| 28 | #include "mirror/string.h" |
| 29 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 31 | #include "sirt_ref.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 32 | |
| 33 | #include <string> |
| 34 | |
| 35 | namespace art { |
| 36 | |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 37 | class ObjectLock { |
| 38 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | explicit ObjectLock(Thread* self, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 40 | : self_(self), obj_(object) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 41 | CHECK(object != NULL); |
| 42 | obj_->MonitorEnter(self_); |
| 43 | } |
| 44 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 45 | ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 46 | obj_->MonitorExit(self_); |
| 47 | } |
| 48 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 49 | void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 50 | Monitor::Wait(self_, obj_, 0, 0, false, kWaiting); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 53 | void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 54 | obj_->Notify(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 57 | void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 58 | obj_->NotifyAll(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | Thread* const self_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | mirror::Object* obj_; |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 64 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
| 65 | }; |
| 66 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 67 | class ClassHelper { |
| 68 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | ClassHelper(const mirror::Class* c = NULL, ClassLinker* l = NULL) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 70 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 71 | : class_linker_(l), |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 72 | dex_cache_(NULL), |
| 73 | dex_file_(NULL), |
| 74 | interface_type_list_(NULL), |
Brian Carlstrom | e77be40 | 2012-03-30 01:08:38 -0700 | [diff] [blame] | 75 | klass_(NULL) { |
| 76 | if (c != NULL) { |
| 77 | ChangeClass(c); |
| 78 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 79 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 80 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | void ChangeClass(const mirror::Class* new_c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 82 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 01e076e | 2012-03-30 11:54:16 -0700 | [diff] [blame] | 83 | CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any |
| 84 | CHECK(new_c->IsClass()) << "new_c=" << new_c; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 85 | if (dex_cache_ != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | mirror::DexCache* new_c_dex_cache = new_c->GetDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 87 | if (new_c_dex_cache != dex_cache_) { |
| 88 | dex_cache_ = new_c_dex_cache; |
| 89 | dex_file_ = NULL; |
| 90 | } |
| 91 | } |
| 92 | klass_ = new_c; |
| 93 | interface_type_list_ = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 96 | // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper. |
| 97 | // If you need it longer, copy it into a std::string. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 98 | const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 99 | CHECK(klass_ != NULL); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 100 | if (UNLIKELY(klass_->IsArrayClass())) { |
| 101 | return GetArrayDescriptor(); |
| 102 | } else if (UNLIKELY(klass_->IsPrimitive())) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 103 | return Primitive::Descriptor(klass_->GetPrimitiveType()); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 104 | } else if (UNLIKELY(klass_->IsProxyClass())) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 105 | descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_); |
| 106 | return descriptor_.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 107 | } else { |
| 108 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 109 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 110 | return dex_file.GetTypeDescriptor(type_id); |
| 111 | } |
| 112 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 113 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 114 | const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 115 | std::string result("["); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | const mirror::Class* saved_klass = klass_; |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 117 | CHECK(saved_klass != NULL); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 118 | ChangeClass(klass_->GetComponentType()); |
| 119 | result += GetDescriptor(); |
| 120 | ChangeClass(saved_klass); |
| 121 | descriptor_ = result; |
| 122 | return descriptor_.c_str(); |
| 123 | } |
| 124 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 125 | const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 126 | DCHECK(klass_ != nullptr); |
| 127 | uint16_t class_def_idx = klass_->GetDexClassDefIndex(); |
| 128 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 129 | return nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 130 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 131 | return &GetDexFile().GetClassDef(class_def_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 132 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 133 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 134 | uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 135 | DCHECK(klass_ != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 136 | if (klass_->IsPrimitive()) { |
| 137 | return 0; |
| 138 | } else if (klass_->IsArrayClass()) { |
| 139 | return 2; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 140 | } else if (klass_->IsProxyClass()) { |
| 141 | return klass_->GetIfTable()->GetLength(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 142 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 143 | const DexFile::TypeList* interfaces = GetInterfaceTypeList(); |
| 144 | if (interfaces == NULL) { |
| 145 | return 0; |
| 146 | } else { |
| 147 | return interfaces->Size(); |
| 148 | } |
| 149 | } |
| 150 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 151 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 152 | uint16_t GetDirectInterfaceTypeIdx(uint32_t idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 153 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 154 | DCHECK(klass_ != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 155 | DCHECK(!klass_->IsPrimitive()); |
| 156 | DCHECK(!klass_->IsArrayClass()); |
| 157 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 158 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 159 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 160 | mirror::Class* GetDirectInterface(uint32_t idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 161 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 162 | DCHECK(klass_ != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 163 | DCHECK(!klass_->IsPrimitive()); |
| 164 | if (klass_->IsArrayClass()) { |
| 165 | if (idx == 0) { |
| 166 | return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;"); |
| 167 | } else { |
| 168 | DCHECK_EQ(1U, idx); |
| 169 | return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;"); |
| 170 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 171 | } else if (klass_->IsProxyClass()) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 172 | return klass_->GetIfTable()->GetInterface(idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 173 | } else { |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 174 | uint16_t type_idx = GetDirectInterfaceTypeIdx(idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 175 | mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 176 | if (interface == NULL) { |
| 177 | interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_); |
| 178 | CHECK(interface != NULL || Thread::Current()->IsExceptionPending()); |
| 179 | } |
| 180 | return interface; |
| 181 | } |
| 182 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 183 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 184 | const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 185 | std::string descriptor(GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 186 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 187 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 188 | CHECK(dex_class_def != NULL); |
| 189 | return dex_file.GetSourceFile(*dex_class_def); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 190 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 191 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 192 | std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 193 | mirror::DexCache* dex_cache = GetDexCache(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 194 | if (dex_cache != NULL && !klass_->IsProxyClass()) { |
| 195 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 196 | } else { |
| 197 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 198 | return "generated class"; |
| 199 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 202 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 203 | if (dex_file_ == NULL) { |
| 204 | dex_file_ = GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 205 | } |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 206 | return *dex_file_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 210 | mirror::DexCache* result = dex_cache_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 211 | if (result == NULL) { |
| 212 | DCHECK(klass_ != NULL); |
| 213 | result = klass_->GetDexCache(); |
| 214 | dex_cache_ = result; |
| 215 | } |
| 216 | return result; |
| 217 | } |
| 218 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 219 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 220 | const DexFile::TypeList* GetInterfaceTypeList() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 221 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 222 | const DexFile::TypeList* result = interface_type_list_; |
| 223 | if (result == NULL) { |
| 224 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 225 | if (class_def != NULL) { |
| 226 | result = GetDexFile().GetInterfacesList(*class_def); |
| 227 | interface_type_list_ = result; |
| 228 | } |
| 229 | } |
| 230 | return result; |
| 231 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 232 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 233 | ClassLinker* GetClassLinker() { |
| 234 | ClassLinker* result = class_linker_; |
| 235 | if (result == NULL) { |
| 236 | result = Runtime::Current()->GetClassLinker(); |
| 237 | class_linker_ = result; |
| 238 | } |
| 239 | return result; |
| 240 | } |
| 241 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 242 | ClassLinker* class_linker_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 243 | mirror::DexCache* dex_cache_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 244 | const DexFile* dex_file_; |
| 245 | const DexFile::TypeList* interface_type_list_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | const mirror::Class* klass_; |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 247 | std::string descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 248 | |
| 249 | DISALLOW_COPY_AND_ASSIGN(ClassHelper); |
| 250 | }; |
| 251 | |
| 252 | class FieldHelper { |
| 253 | public: |
| 254 | FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {} |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 255 | explicit FieldHelper(const mirror::ArtField* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {} |
| 256 | FieldHelper(const mirror::ArtField* f, ClassLinker* l) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 257 | : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {} |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 258 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 259 | void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 260 | DCHECK(new_f != NULL); |
| 261 | if (dex_cache_ != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 262 | mirror::DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 263 | if (new_f_dex_cache != dex_cache_) { |
| 264 | dex_cache_ = new_f_dex_cache; |
| 265 | dex_file_ = NULL; |
| 266 | } |
| 267 | } |
| 268 | field_ = new_f; |
| 269 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 270 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 271 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 272 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 273 | const DexFile& dex_file = GetDexFile(); |
| 274 | return dex_file.GetFieldName(dex_file.GetFieldId(field_index)); |
| 275 | } else { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 276 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 277 | DCHECK_LT(field_index, 2U); |
| 278 | return field_index == 0 ? "interfaces" : "throws"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 279 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 280 | } |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 281 | mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 282 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 283 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 284 | const DexFile& dex_file = GetDexFile(); |
| 285 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 286 | mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_); |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 287 | if (resolve && (type == NULL)) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 288 | type = GetClassLinker()->ResolveType(field_id.type_idx_, field_); |
| 289 | CHECK(type != NULL || Thread::Current()->IsExceptionPending()); |
| 290 | } |
| 291 | return type; |
| 292 | } else { |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 293 | return GetClassLinker()->FindSystemClass(GetTypeDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 294 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 295 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 296 | const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 297 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 298 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 299 | const DexFile& dex_file = GetDexFile(); |
| 300 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 301 | return dex_file.GetFieldTypeDescriptor(field_id); |
| 302 | } else { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 303 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 304 | DCHECK_LT(field_index, 2U); |
| 305 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
| 306 | return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 307 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 308 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 309 | Primitive::Type GetTypeAsPrimitiveType() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 310 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 311 | return Primitive::GetType(GetTypeDescriptor()[0]); |
| 312 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 313 | bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 314 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 315 | return type != Primitive::kPrimNot; |
| 316 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 317 | size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 318 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 319 | return Primitive::FieldSize(type); |
| 320 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 321 | |
| 322 | // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper. |
| 323 | // If you need it longer, copy it into a std::string. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 324 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 325 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 326 | uint32_t field_index = field_->GetDexFieldIndex(); |
| 327 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 328 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 329 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 330 | return dex_file.GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 331 | } else { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 332 | DCHECK(field_->IsStatic()); |
| 333 | DCHECK_LT(field_index, 2U); |
| 334 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 335 | ClassHelper kh(field_->GetDeclaringClass()); |
| 336 | declaring_class_descriptor_ = kh.GetDescriptor(); |
| 337 | return declaring_class_descriptor_.c_str(); |
| 338 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 342 | mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 343 | mirror::DexCache* result = dex_cache_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 344 | if (result == NULL) { |
| 345 | result = field_->GetDeclaringClass()->GetDexCache(); |
| 346 | dex_cache_ = result; |
| 347 | } |
| 348 | return result; |
| 349 | } |
| 350 | ClassLinker* GetClassLinker() { |
| 351 | ClassLinker* result = class_linker_; |
| 352 | if (result == NULL) { |
| 353 | result = Runtime::Current()->GetClassLinker(); |
| 354 | class_linker_ = result; |
| 355 | } |
| 356 | return result; |
| 357 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 358 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 359 | if (dex_file_ == NULL) { |
| 360 | dex_file_ = GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 361 | } |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 362 | return *dex_file_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | ClassLinker* class_linker_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 366 | mirror::DexCache* dex_cache_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 367 | const DexFile* dex_file_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 368 | const mirror::ArtField* field_; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 369 | std::string declaring_class_descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 370 | |
| 371 | DISALLOW_COPY_AND_ASSIGN(FieldHelper); |
| 372 | }; |
| 373 | |
| 374 | class MethodHelper { |
| 375 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 376 | MethodHelper() |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 378 | shorty_len_(0) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 379 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 380 | explicit MethodHelper(const mirror::ArtMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 381 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 382 | : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 383 | shorty_len_(0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 384 | SetMethod(m); |
| 385 | } |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 386 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 387 | MethodHelper(const mirror::ArtMethod* m, ClassLinker* l) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 388 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 389 | : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 390 | shorty_len_(0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 391 | SetMethod(m); |
| 392 | } |
| 393 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 394 | void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 395 | DCHECK(new_m != NULL); |
| 396 | if (dex_cache_ != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 397 | mirror::Class* klass = new_m->GetDeclaringClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 398 | if (klass->IsProxyClass()) { |
| 399 | dex_cache_ = NULL; |
| 400 | dex_file_ = NULL; |
| 401 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 402 | mirror::DexCache* new_m_dex_cache = klass->GetDexCache(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 403 | if (new_m_dex_cache != dex_cache_) { |
| 404 | dex_cache_ = new_m_dex_cache; |
| 405 | dex_file_ = NULL; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | SetMethod(new_m); |
| 410 | shorty_ = NULL; |
| 411 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 412 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 413 | const mirror::ArtMethod* GetMethod() const { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 414 | return method_; |
| 415 | } |
| 416 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 417 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 418 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 419 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 420 | if (dex_method_idx != DexFile::kDexNoIndex) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 421 | return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); |
| 422 | } else { |
| 423 | Runtime* runtime = Runtime::Current(); |
| 424 | if (method_ == runtime->GetResolutionMethod()) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 425 | return "<runtime internal resolution method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 426 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 427 | return "<runtime internal callee-save all registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 428 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 429 | return "<runtime internal callee-save reference registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 430 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 431 | return "<runtime internal callee-save reference and argument registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 432 | } else { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 433 | return "<unknown runtime internal method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 436 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 437 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 438 | mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 439 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 440 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 441 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 442 | return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache()); |
| 443 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 444 | |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 445 | const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 446 | const char* result = shorty_; |
| 447 | if (result == NULL) { |
| 448 | const DexFile& dex_file = GetDexFile(); |
| 449 | result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()), |
| 450 | &shorty_len_); |
| 451 | shorty_ = result; |
| 452 | } |
| 453 | return result; |
| 454 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 455 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 456 | uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 457 | if (shorty_ == NULL) { |
| 458 | GetShorty(); |
| 459 | } |
| 460 | return shorty_len_; |
| 461 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 462 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 463 | const std::string GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 464 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 465 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 466 | if (dex_method_idx != DexFile::kDexNoIndex) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 467 | return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx)); |
| 468 | } else { |
| 469 | return "<no signature>"; |
| 470 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 471 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 472 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 473 | const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 474 | const DexFile& dex_file = GetDexFile(); |
| 475 | return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex())); |
| 476 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 477 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 478 | const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 479 | const DexFile::ProtoId& proto = GetPrototype(); |
| 480 | return GetDexFile().GetProtoParameters(proto); |
| 481 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 482 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 483 | mirror::Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 484 | const DexFile& dex_file = GetDexFile(); |
| 485 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 486 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 487 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 488 | return GetClassFromTypeIdx(return_type_idx); |
| 489 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 490 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 491 | const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 492 | const DexFile& dex_file = GetDexFile(); |
| 493 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 494 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 495 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 496 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx)); |
| 497 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 498 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 499 | 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] | 500 | if (dex_pc == DexFile::kDexNoIndex) { |
| 501 | return method_->IsNative() ? -2 : -1; |
| 502 | } else { |
| 503 | const DexFile& dex_file = GetDexFile(); |
| 504 | return dex_file.GetLineNumFromPC(method_, dex_pc); |
| 505 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 506 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 507 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 508 | const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 509 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 510 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 511 | if (dex_method_idx != DexFile::kDexNoIndex) { |
| 512 | return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx)); |
| 513 | } else { |
| 514 | return "<runtime method>"; |
| 515 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 516 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 517 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 518 | const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 519 | return ClassHelper(method_->GetDeclaringClass()).GetSourceFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 520 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 521 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 522 | uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 523 | return method_->GetDeclaringClass()->GetDexClassDefIndex(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame^] | 526 | const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 527 | return GetDexFile().GetClassDef(GetClassDefIndex()); |
| 528 | } |
| 529 | |
| 530 | mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 531 | return method_->GetDeclaringClass()->GetClassLoader(); |
| 532 | } |
| 533 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 534 | bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 535 | return method_->IsStatic(); |
| 536 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 537 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 538 | bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 539 | return IsStatic() && StringPiece(GetName()) == "<clinit>"; |
| 540 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 541 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 542 | size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 543 | // "1 +" because the first in Args is the receiver. |
| 544 | // "- 1" because we don't count the return type. |
| 545 | return (IsStatic() ? 0 : 1) + GetShortyLength() - 1; |
| 546 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 547 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 548 | // Get the primitive type associated with the given parameter. |
| 549 | Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 550 | CHECK_LT(param, NumArgs()); |
| 551 | if (IsStatic()) { |
| 552 | param++; // 0th argument must skip return value at start of the shorty |
| 553 | } else if (param == 0) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 554 | return Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 555 | } |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 556 | return Primitive::GetType(GetShorty()[param]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 557 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 558 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 559 | // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods. |
| 560 | bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 561 | Primitive::Type type = GetParamPrimitiveType(param); |
| 562 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 563 | } |
| 564 | |
| 565 | // 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] | 566 | bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 567 | return GetParamPrimitiveType(param) == Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 568 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 569 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 570 | bool HasSameNameAndSignature(MethodHelper* other) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 571 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 572 | if (GetDexCache() == other->GetDexCache()) { |
| 573 | const DexFile& dex_file = GetDexFile(); |
| 574 | const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 575 | const DexFile::MethodId& other_mid = |
| 576 | dex_file.GetMethodId(other->method_->GetDexMethodIndex()); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 577 | 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] | 578 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 579 | StringPiece name(GetName()); |
| 580 | StringPiece other_name(other->GetName()); |
| 581 | return name == other_name && GetSignature() == other->GetSignature(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 582 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 583 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 584 | const DexFile::CodeItem* GetCodeItem() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 585 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 586 | return GetDexFile().GetCodeItem(method_->GetCodeItemOffset()); |
| 587 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 588 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 589 | bool IsResolvedTypeIdx(uint16_t type_idx) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 590 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 591 | return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL; |
| 592 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 593 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 594 | mirror::Class* GetClassFromTypeIdx(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 595 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 596 | mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 597 | if (type == NULL) { |
| 598 | type = GetClassLinker()->ResolveType(type_idx, method_); |
| 599 | CHECK(type != NULL || Thread::Current()->IsExceptionPending()); |
| 600 | } |
| 601 | return type; |
| 602 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 603 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 604 | const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 605 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 606 | const DexFile& dex_file = GetDexFile(); |
| 607 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)); |
| 608 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 609 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 610 | mirror::Class* GetDexCacheResolvedType(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 611 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 612 | return method_->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 613 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 614 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 615 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 616 | const DexFile* result = dex_file_; |
| 617 | if (result == NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 618 | const mirror::DexCache* dex_cache = GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 619 | result = dex_file_ = dex_cache->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 620 | } |
| 621 | return *result; |
| 622 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 623 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 624 | mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 625 | mirror::DexCache* result = dex_cache_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 626 | if (result == NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 627 | mirror::Class* klass = method_->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 628 | result = klass->GetDexCache(); |
| 629 | dex_cache_ = result; |
| 630 | } |
| 631 | return result; |
| 632 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 633 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 634 | mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 635 | mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 636 | if (UNLIKELY(s == NULL)) { |
| 637 | s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, GetDexCache()); |
| 638 | } |
| 639 | return s; |
| 640 | } |
| 641 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 642 | private: |
| 643 | // Set the method_ field, for proxy methods looking up the interface method via the resolved |
| 644 | // methods table. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 645 | void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 646 | if (method != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 647 | mirror::Class* klass = method->GetDeclaringClass(); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 648 | if (UNLIKELY(klass->IsProxyClass())) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 649 | mirror::ArtMethod* interface_method = |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 650 | method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex()); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 651 | DCHECK(interface_method != NULL); |
| 652 | DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 653 | method = interface_method; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | method_ = method; |
| 657 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 658 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 659 | ClassLinker* GetClassLinker() { |
| 660 | ClassLinker* result = class_linker_; |
| 661 | if (result == NULL) { |
| 662 | result = Runtime::Current()->GetClassLinker(); |
| 663 | class_linker_ = result; |
| 664 | } |
| 665 | return result; |
| 666 | } |
| 667 | |
| 668 | ClassLinker* class_linker_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 669 | mirror::DexCache* dex_cache_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 670 | const DexFile* dex_file_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 671 | const mirror::ArtMethod* method_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 672 | const char* shorty_; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 673 | uint32_t shorty_len_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 674 | |
| 675 | DISALLOW_COPY_AND_ASSIGN(MethodHelper); |
| 676 | }; |
| 677 | |
| 678 | } // namespace art |
| 679 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 680 | #endif // ART_RUNTIME_OBJECT_UTILS_H_ |