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