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