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 | |
| 17 | #ifndef ART_SRC_OBJECT_UTILS_H_ |
| 18 | #define ART_SRC_OBJECT_UTILS_H_ |
| 19 | |
| 20 | #include "class_linker.h" |
| 21 | #include "dex_cache.h" |
| 22 | #include "dex_file.h" |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 23 | #include "intern_table.h" |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 24 | #include "monitor.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 25 | #include "object.h" |
| 26 | #include "runtime.h" |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 27 | #include "sirt_ref.h" |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 28 | #include "UniquePtr.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 29 | |
| 30 | #include <string> |
| 31 | |
| 32 | namespace art { |
| 33 | |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 34 | class ObjectLock { |
| 35 | public: |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 36 | explicit ObjectLock(Thread* self, Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 37 | : self_(self), obj_(object) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 38 | CHECK(object != NULL); |
| 39 | obj_->MonitorEnter(self_); |
| 40 | } |
| 41 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 42 | ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 43 | obj_->MonitorExit(self_); |
| 44 | } |
| 45 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 46 | void Wait() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame^] | 47 | return Monitor::Wait(self_, obj_, 0, 0, false, kWaiting); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 50 | void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 51 | obj_->Notify(); |
| 52 | } |
| 53 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 54 | void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 55 | obj_->NotifyAll(); |
| 56 | } |
| 57 | |
| 58 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | Thread* const self_; |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 60 | Object* obj_; |
| 61 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
| 62 | }; |
| 63 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 64 | class ClassHelper { |
| 65 | public: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 66 | ClassHelper(const Class* c = NULL, ClassLinker* l = NULL) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 67 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 68 | : class_def_(NULL), |
| 69 | class_linker_(l), |
| 70 | dex_cache_(NULL), |
| 71 | dex_file_(NULL), |
| 72 | interface_type_list_(NULL), |
Brian Carlstrom | e77be40 | 2012-03-30 01:08:38 -0700 | [diff] [blame] | 73 | klass_(NULL) { |
| 74 | if (c != NULL) { |
| 75 | ChangeClass(c); |
| 76 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 77 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 78 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 79 | void ChangeClass(const Class* new_c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 80 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 01e076e | 2012-03-30 11:54:16 -0700 | [diff] [blame] | 81 | CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any |
| 82 | CHECK(new_c->IsClass()) << "new_c=" << new_c; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 83 | if (dex_cache_ != NULL) { |
| 84 | DexCache* new_c_dex_cache = new_c->GetDexCache(); |
| 85 | if (new_c_dex_cache != dex_cache_) { |
| 86 | dex_cache_ = new_c_dex_cache; |
| 87 | dex_file_ = NULL; |
| 88 | } |
| 89 | } |
| 90 | klass_ = new_c; |
| 91 | interface_type_list_ = NULL; |
| 92 | class_def_ = NULL; |
| 93 | } |
| 94 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 95 | // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper. |
| 96 | // If you need it longer, copy it into a std::string. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 97 | const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 98 | CHECK(klass_ != NULL); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 99 | if (UNLIKELY(klass_->IsArrayClass())) { |
| 100 | return GetArrayDescriptor(); |
| 101 | } else if (UNLIKELY(klass_->IsPrimitive())) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 102 | return Primitive::Descriptor(klass_->GetPrimitiveType()); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 103 | } else if (UNLIKELY(klass_->IsProxyClass())) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 104 | descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_); |
| 105 | return descriptor_.c_str(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 106 | } else { |
| 107 | const DexFile& dex_file = GetDexFile(); |
| 108 | const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex()); |
| 109 | return dex_file.GetTypeDescriptor(type_id); |
| 110 | } |
| 111 | } |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 112 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 113 | const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 114 | std::string result("["); |
| 115 | const Class* saved_klass = klass_; |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 116 | CHECK(saved_klass != NULL); |
Ian Rogers | a68a1cb | 2012-01-10 10:34:36 -0800 | [diff] [blame] | 117 | ChangeClass(klass_->GetComponentType()); |
| 118 | result += GetDescriptor(); |
| 119 | ChangeClass(saved_klass); |
| 120 | descriptor_ = result; |
| 121 | return descriptor_.c_str(); |
| 122 | } |
| 123 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | const DexFile::ClassDef* GetClassDef() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 125 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 126 | const DexFile::ClassDef* result = class_def_; |
| 127 | if (result == NULL) { |
| 128 | result = GetDexFile().FindClassDef(GetDescriptor()); |
| 129 | class_def_ = result; |
| 130 | } |
| 131 | return result; |
| 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 | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 160 | 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 | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 175 | Class* interface = GetDexCache()->GetResolvedType(type_idx); |
| 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(); |
| 187 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
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 | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 193 | DexCache* dex_cache = GetDexCache(); |
| 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 | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 209 | DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 210 | DexCache* result = dex_cache_; |
| 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 | |
| 242 | const DexFile::ClassDef* class_def_; |
| 243 | ClassLinker* class_linker_; |
| 244 | DexCache* dex_cache_; |
| 245 | const DexFile* dex_file_; |
| 246 | const DexFile::TypeList* interface_type_list_; |
| 247 | const Class* klass_; |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 248 | std::string descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 249 | |
| 250 | DISALLOW_COPY_AND_ASSIGN(ClassHelper); |
| 251 | }; |
| 252 | |
| 253 | class FieldHelper { |
| 254 | public: |
| 255 | FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {} |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 256 | explicit FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 257 | FieldHelper(const Field* f, ClassLinker* l) |
| 258 | : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {} |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 259 | |
| 260 | void ChangeField(const Field* new_f) { |
| 261 | DCHECK(new_f != NULL); |
| 262 | if (dex_cache_ != NULL) { |
| 263 | DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache(); |
| 264 | if (new_f_dex_cache != dex_cache_) { |
| 265 | dex_cache_ = new_f_dex_cache; |
| 266 | dex_file_ = NULL; |
| 267 | } |
| 268 | } |
| 269 | field_ = new_f; |
| 270 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 271 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 272 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 273 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 274 | const DexFile& dex_file = GetDexFile(); |
| 275 | return dex_file.GetFieldName(dex_file.GetFieldId(field_index)); |
| 276 | } else { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 277 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 278 | DCHECK_LT(field_index, 2U); |
| 279 | return field_index == 0 ? "interfaces" : "throws"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 280 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 281 | } |
| 282 | String* GetNameAsString() { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 283 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 284 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 285 | const DexFile& dex_file = GetDexFile(); |
| 286 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 287 | return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache()); |
| 288 | } else { |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 289 | return Runtime::Current()->GetInternTable()->InternStrong(GetName()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 290 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 291 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 292 | Class* GetType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 293 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 294 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 295 | const DexFile& dex_file = GetDexFile(); |
| 296 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 297 | Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_); |
| 298 | if (type == NULL) { |
| 299 | type = GetClassLinker()->ResolveType(field_id.type_idx_, field_); |
| 300 | CHECK(type != NULL || Thread::Current()->IsExceptionPending()); |
| 301 | } |
| 302 | return type; |
| 303 | } else { |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 304 | return GetClassLinker()->FindSystemClass(GetTypeDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 305 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 306 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 307 | const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 308 | uint32_t field_index = field_->GetDexFieldIndex(); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 309 | if (!field_->GetDeclaringClass()->IsProxyClass()) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 310 | const DexFile& dex_file = GetDexFile(); |
| 311 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 312 | return dex_file.GetFieldTypeDescriptor(field_id); |
| 313 | } else { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 314 | DCHECK(field_->IsStatic()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 315 | DCHECK_LT(field_index, 2U); |
| 316 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
| 317 | return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;"; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 318 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 319 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 320 | Primitive::Type GetTypeAsPrimitiveType() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 321 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 322 | return Primitive::GetType(GetTypeDescriptor()[0]); |
| 323 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 324 | bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 325 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 326 | return type != Primitive::kPrimNot; |
| 327 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 328 | size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 329 | Primitive::Type type = GetTypeAsPrimitiveType(); |
| 330 | return Primitive::FieldSize(type); |
| 331 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 332 | |
| 333 | // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper. |
| 334 | // If you need it longer, copy it into a std::string. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 335 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 336 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 337 | uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 338 | if (type_idx != DexFile::kDexNoIndex16) { |
| 339 | const DexFile& dex_file = GetDexFile(); |
| 340 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)); |
| 341 | } else { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | // Most likely a proxy class. |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 343 | ClassHelper kh(field_->GetDeclaringClass()); |
| 344 | declaring_class_descriptor_ = kh.GetDescriptor(); |
| 345 | return declaring_class_descriptor_.c_str(); |
| 346 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private: |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 350 | DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 351 | DexCache* result = dex_cache_; |
| 352 | if (result == NULL) { |
| 353 | result = field_->GetDeclaringClass()->GetDexCache(); |
| 354 | dex_cache_ = result; |
| 355 | } |
| 356 | return result; |
| 357 | } |
| 358 | ClassLinker* GetClassLinker() { |
| 359 | ClassLinker* result = class_linker_; |
| 360 | if (result == NULL) { |
| 361 | result = Runtime::Current()->GetClassLinker(); |
| 362 | class_linker_ = result; |
| 363 | } |
| 364 | return result; |
| 365 | } |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 366 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 367 | if (dex_file_ == NULL) { |
| 368 | dex_file_ = GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 369 | } |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 370 | return *dex_file_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | ClassLinker* class_linker_; |
| 374 | DexCache* dex_cache_; |
| 375 | const DexFile* dex_file_; |
| 376 | const Field* field_; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 377 | std::string declaring_class_descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 378 | |
| 379 | DISALLOW_COPY_AND_ASSIGN(FieldHelper); |
| 380 | }; |
| 381 | |
| 382 | class MethodHelper { |
| 383 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 384 | MethodHelper() |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 385 | : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 386 | shorty_len_(0) {} |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 387 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 388 | explicit MethodHelper(const AbstractMethod* m) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 389 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 390 | : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 391 | shorty_len_(0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 392 | SetMethod(m); |
| 393 | } |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 394 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 395 | MethodHelper(const AbstractMethod* m, ClassLinker* l) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 396 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 397 | : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL), |
| 398 | shorty_len_(0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 399 | SetMethod(m); |
| 400 | } |
| 401 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 402 | void ChangeMethod(AbstractMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 403 | DCHECK(new_m != NULL); |
| 404 | if (dex_cache_ != NULL) { |
| 405 | Class* klass = new_m->GetDeclaringClass(); |
| 406 | if (klass->IsProxyClass()) { |
| 407 | dex_cache_ = NULL; |
| 408 | dex_file_ = NULL; |
| 409 | } else { |
| 410 | DexCache* new_m_dex_cache = klass->GetDexCache(); |
| 411 | if (new_m_dex_cache != dex_cache_) { |
| 412 | dex_cache_ = new_m_dex_cache; |
| 413 | dex_file_ = NULL; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | SetMethod(new_m); |
| 418 | shorty_ = NULL; |
| 419 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 420 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 421 | const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 422 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 423 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 424 | if (dex_method_idx != DexFile::kDexNoIndex16) { |
| 425 | return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); |
| 426 | } else { |
| 427 | Runtime* runtime = Runtime::Current(); |
| 428 | if (method_ == runtime->GetResolutionMethod()) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 429 | return "<runtime internal resolution method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 430 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 431 | return "<runtime internal callee-save all registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 432 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 433 | return "<runtime internal callee-save reference registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 434 | } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 435 | return "<runtime internal callee-save reference and argument registers method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 436 | } else { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 437 | return "<unknown runtime internal method>"; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 440 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 441 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 442 | String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 443 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 444 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 445 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 446 | return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache()); |
| 447 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 448 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 449 | const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 450 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 451 | const char* result = shorty_; |
| 452 | if (result == NULL) { |
| 453 | const DexFile& dex_file = GetDexFile(); |
| 454 | result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()), |
| 455 | &shorty_len_); |
| 456 | shorty_ = result; |
| 457 | } |
| 458 | return result; |
| 459 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 460 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 461 | uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 462 | if (shorty_ == NULL) { |
| 463 | GetShorty(); |
| 464 | } |
| 465 | return shorty_len_; |
| 466 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 467 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 468 | const std::string GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 469 | const DexFile& dex_file = GetDexFile(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 470 | uint32_t dex_method_idx = method_->GetDexMethodIndex(); |
| 471 | if (dex_method_idx != DexFile::kDexNoIndex16) { |
| 472 | return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx)); |
| 473 | } else { |
| 474 | return "<no signature>"; |
| 475 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 476 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 477 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 478 | const DexFile::ProtoId& GetPrototype() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 479 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 480 | const DexFile& dex_file = GetDexFile(); |
| 481 | return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex())); |
| 482 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 483 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 484 | const DexFile::TypeList* GetParameterTypeList() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 485 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 486 | const DexFile::ProtoId& proto = GetPrototype(); |
| 487 | return GetDexFile().GetProtoParameters(proto); |
| 488 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 489 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 490 | ObjectArray<Class>* GetParameterTypes(Thread* self) |
| 491 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 492 | const DexFile::TypeList* params = GetParameterTypeList(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 493 | uint32_t num_params = params == NULL ? 0 : params->Size(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 494 | SirtRef<ObjectArray<Class> > result(self, GetClassLinker()->AllocClassArray(self, num_params)); |
| 495 | if (UNLIKELY(result.get() == NULL)) { |
| 496 | CHECK(self->IsExceptionPending()); |
| 497 | return NULL; |
| 498 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 499 | for (uint32_t i = 0; i < num_params; i++) { |
| 500 | Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 501 | if (param_type == NULL) { |
| 502 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 503 | return NULL; |
| 504 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 505 | result->Set(i, param_type); |
| 506 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 507 | return result.get(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 508 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 509 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 510 | Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 511 | const DexFile& dex_file = GetDexFile(); |
| 512 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 513 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 514 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 515 | return GetClassFromTypeIdx(return_type_idx); |
| 516 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 517 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 518 | const char* GetReturnTypeDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 520 | const DexFile& dex_file = GetDexFile(); |
| 521 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 522 | const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id); |
| 523 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 524 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx)); |
| 525 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 526 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 527 | int32_t GetLineNumFromDexPC(uint32_t dex_pc) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 528 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 529 | if (dex_pc == DexFile::kDexNoIndex) { |
| 530 | return method_->IsNative() ? -2 : -1; |
| 531 | } else { |
| 532 | const DexFile& dex_file = GetDexFile(); |
| 533 | return dex_file.GetLineNumFromPC(method_, dex_pc); |
| 534 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 535 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 536 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 537 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 538 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 539 | Class* klass = method_->GetDeclaringClass(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 540 | DCHECK(!klass->IsProxyClass()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 541 | uint16_t type_idx = klass->GetDexTypeIndex(); |
| 542 | const DexFile& dex_file = GetDexFile(); |
| 543 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)); |
| 544 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 545 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 546 | const char* GetDeclaringClassSourceFile() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 547 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 548 | const char* descriptor = GetDeclaringClassDescriptor(); |
| 549 | const DexFile& dex_file = GetDexFile(); |
| 550 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 551 | CHECK(dex_class_def != NULL); |
| 552 | return dex_file.GetSourceFile(*dex_class_def); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 553 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 554 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 555 | uint32_t GetClassDefIndex() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 556 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 557 | const char* descriptor = GetDeclaringClassDescriptor(); |
| 558 | const DexFile& dex_file = GetDexFile(); |
| 559 | uint32_t index; |
| 560 | CHECK(dex_file.FindClassDefIndex(descriptor, index)); |
| 561 | return index; |
| 562 | } |
| 563 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 564 | ClassLoader* GetClassLoader() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 565 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 566 | return method_->GetDeclaringClass()->GetClassLoader(); |
| 567 | } |
| 568 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 569 | bool IsStatic() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 570 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 571 | return method_->IsStatic(); |
| 572 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 573 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 574 | bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 575 | return IsStatic() && StringPiece(GetName()) == "<clinit>"; |
| 576 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 577 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 578 | size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 579 | // "1 +" because the first in Args is the receiver. |
| 580 | // "- 1" because we don't count the return type. |
| 581 | return (IsStatic() ? 0 : 1) + GetShortyLength() - 1; |
| 582 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 583 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 584 | // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 585 | bool IsParamALongOrDouble(size_t param) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 586 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 587 | CHECK_LT(param, NumArgs()); |
| 588 | if (IsStatic()) { |
| 589 | param++; // 0th argument must skip return value at start of the shorty |
| 590 | } else if (param == 0) { |
| 591 | return false; // this argument |
| 592 | } |
| 593 | char ch = GetShorty()[param]; |
| 594 | return (ch == 'J' || ch == 'D'); |
| 595 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 596 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 597 | // 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] | 598 | bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 599 | CHECK_LT(param, NumArgs()); |
| 600 | if (IsStatic()) { |
| 601 | param++; // 0th argument must skip return value at start of the shorty |
| 602 | } else if (param == 0) { |
| 603 | return true; // this argument |
| 604 | } |
| 605 | return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[') |
| 606 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 607 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 608 | bool HasSameNameAndSignature(MethodHelper* other) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 609 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 610 | if (GetDexCache() == other->GetDexCache()) { |
| 611 | const DexFile& dex_file = GetDexFile(); |
| 612 | const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex()); |
| 613 | const DexFile::MethodId& other_mid = |
| 614 | dex_file.GetMethodId(other->method_->GetDexMethodIndex()); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 615 | 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] | 616 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 617 | StringPiece name(GetName()); |
| 618 | StringPiece other_name(other->GetName()); |
| 619 | return name == other_name && GetSignature() == other->GetSignature(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 620 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 621 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 622 | const DexFile::CodeItem* GetCodeItem() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 623 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 624 | return GetDexFile().GetCodeItem(method_->GetCodeItemOffset()); |
| 625 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 626 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 627 | bool IsResolvedTypeIdx(uint16_t type_idx) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 628 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 629 | return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL; |
| 630 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 631 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 632 | Class* GetClassFromTypeIdx(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 633 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 634 | Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx); |
| 635 | if (type == NULL) { |
| 636 | type = GetClassLinker()->ResolveType(type_idx, method_); |
| 637 | CHECK(type != NULL || Thread::Current()->IsExceptionPending()); |
| 638 | } |
| 639 | return type; |
| 640 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 641 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 642 | const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 643 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 644 | const DexFile& dex_file = GetDexFile(); |
| 645 | return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx)); |
| 646 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 647 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 648 | Class* GetDexCacheResolvedType(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 649 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 650 | return method_->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 651 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 652 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 653 | const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 654 | const DexFile* result = dex_file_; |
| 655 | if (result == NULL) { |
| 656 | const DexCache* dex_cache = GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 657 | result = dex_file_ = dex_cache->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 658 | } |
| 659 | return *result; |
| 660 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 661 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 662 | DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 663 | DexCache* result = dex_cache_; |
| 664 | if (result == NULL) { |
| 665 | Class* klass = method_->GetDeclaringClass(); |
| 666 | result = klass->GetDexCache(); |
| 667 | dex_cache_ = result; |
| 668 | } |
| 669 | return result; |
| 670 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 671 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 672 | String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 673 | String* s = method_->GetDexCacheStrings()->Get(string_idx); |
| 674 | if (UNLIKELY(s == NULL)) { |
| 675 | s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, GetDexCache()); |
| 676 | } |
| 677 | return s; |
| 678 | } |
| 679 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 680 | private: |
| 681 | // Set the method_ field, for proxy methods looking up the interface method via the resolved |
| 682 | // methods table. |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 683 | void SetMethod(const AbstractMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 684 | if (method != NULL) { |
| 685 | Class* klass = method->GetDeclaringClass(); |
| 686 | if (klass->IsProxyClass()) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 687 | AbstractMethod* interface_method = |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 688 | method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex()); |
| 689 | CHECK(interface_method != NULL); |
| 690 | CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method)); |
| 691 | method = interface_method; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | method_ = method; |
| 695 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 696 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 697 | ClassLinker* GetClassLinker() { |
| 698 | ClassLinker* result = class_linker_; |
| 699 | if (result == NULL) { |
| 700 | result = Runtime::Current()->GetClassLinker(); |
| 701 | class_linker_ = result; |
| 702 | } |
| 703 | return result; |
| 704 | } |
| 705 | |
| 706 | ClassLinker* class_linker_; |
| 707 | DexCache* dex_cache_; |
| 708 | const DexFile* dex_file_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 709 | const AbstractMethod* method_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 710 | const char* shorty_; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 711 | uint32_t shorty_len_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 712 | |
| 713 | DISALLOW_COPY_AND_ASSIGN(MethodHelper); |
| 714 | }; |
| 715 | |
| 716 | } // namespace art |
| 717 | |
| 718 | #endif // ART_SRC_OBJECT_UTILS_H_ |