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