blob: b0a4ce56fea938025d8c6a3c083e1983a6e790b0 [file] [log] [blame]
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_OBJECT_UTILS_H_
18#define ART_RUNTIME_OBJECT_UTILS_H_
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080019
Ian Rogersd8274bc2013-05-15 15:54:45 -070020#include "class_linker-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "dex_file.h"
Ian Rogers672f5202012-01-12 18:06:40 -080022#include "monitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.h"
24#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
26#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/iftable.h"
28#include "mirror/string.h"
29
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "runtime.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070031#include "sirt_ref.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032
33#include <string>
34
35namespace art {
36
Ian Rogers672f5202012-01-12 18:06:40 -080037class ObjectLock {
38 public:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070039 explicit ObjectLock(Thread* self, mirror::Object* object)
40 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070041 : self_(self), obj_(object) {
Ian Rogers672f5202012-01-12 18:06:40 -080042 CHECK(object != NULL);
43 obj_->MonitorEnter(self_);
44 }
45
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080047 obj_->MonitorExit(self_);
48 }
49
Ian Rogers05f30572013-02-20 12:13:11 -080050 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
51 Monitor::Wait(self_, obj_, 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080052 }
53
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080055 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080056 }
57
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080059 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080060 }
61
62 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 Thread* const self_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 mirror::Object* obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080065 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
66};
67
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080068class ClassHelper {
69 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 ClassHelper(const mirror::Class* c = NULL, ClassLinker* l = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers8b2c0b92013-09-19 02:56:49 -070072 : class_linker_(l),
Elliott Hughes91250e02011-12-13 22:30:35 -080073 dex_cache_(NULL),
74 dex_file_(NULL),
75 interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070076 klass_(NULL) {
77 if (c != NULL) {
78 ChangeClass(c);
79 }
Elliott Hughes91250e02011-12-13 22:30:35 -080080 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080081
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 void ChangeClass(const mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070083 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070084 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
85 CHECK(new_c->IsClass()) << "new_c=" << new_c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080086 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mirror::DexCache* new_c_dex_cache = new_c->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 if (new_c_dex_cache != dex_cache_) {
89 dex_cache_ = new_c_dex_cache;
90 dex_file_ = NULL;
91 }
92 }
93 klass_ = new_c;
94 interface_type_list_ = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080095 }
96
Elliott Hughes91250e02011-12-13 22:30:35 -080097 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
98 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070099 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700100 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800101 if (UNLIKELY(klass_->IsArrayClass())) {
102 return GetArrayDescriptor();
103 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800104 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800105 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800106 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
107 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800108 } else {
109 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700110 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800111 return dex_file.GetTypeDescriptor(type_id);
112 }
113 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800114
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800116 std::string result("[");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 const mirror::Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700118 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800119 ChangeClass(klass_->GetComponentType());
120 result += GetDescriptor();
121 ChangeClass(saved_klass);
122 descriptor_ = result;
123 return descriptor_.c_str();
124 }
125
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127 DCHECK(klass_ != nullptr);
128 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
129 if (class_def_idx == DexFile::kDexNoIndex16) {
130 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700132 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800134
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700136 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 if (klass_->IsPrimitive()) {
138 return 0;
139 } else if (klass_->IsArrayClass()) {
140 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800141 } else if (klass_->IsProxyClass()) {
142 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800144 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
145 if (interfaces == NULL) {
146 return 0;
147 } else {
148 return interfaces->Size();
149 }
150 }
151 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800152
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700155 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 DCHECK(!klass_->IsPrimitive());
157 DCHECK(!klass_->IsArrayClass());
158 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
159 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800160
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700163 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800164 DCHECK(!klass_->IsPrimitive());
165 if (klass_->IsArrayClass()) {
166 if (idx == 0) {
167 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
168 } else {
169 DCHECK_EQ(1U, idx);
170 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
171 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800172 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700173 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700175 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 if (interface == NULL) {
178 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
179 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
180 }
181 return interface;
182 }
183 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800184
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700186 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700188 const DexFile::ClassDef* dex_class_def = GetClassDef();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800189 CHECK(dex_class_def != NULL);
190 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800192
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 if (dex_cache != NULL && !klass_->IsProxyClass()) {
196 return dex_cache->GetLocation()->ToModifiedUtf8();
197 } else {
198 // Arrays and proxies are generated and have no corresponding dex file location.
199 return "generated class";
200 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 }
202
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700204 if (dex_file_ == NULL) {
205 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700207 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800208 }
209
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
211 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 if (result == NULL) {
213 DCHECK(klass_ != NULL);
214 result = klass_->GetDexCache();
215 dex_cache_ = result;
216 }
217 return result;
218 }
219
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 const DexFile::TypeList* result = interface_type_list_;
224 if (result == NULL) {
225 const DexFile::ClassDef* class_def = GetClassDef();
226 if (class_def != NULL) {
227 result = GetDexFile().GetInterfacesList(*class_def);
228 interface_type_list_ = result;
229 }
230 }
231 return result;
232 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800233
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 ClassLinker* GetClassLinker() {
235 ClassLinker* result = class_linker_;
236 if (result == NULL) {
237 result = Runtime::Current()->GetClassLinker();
238 class_linker_ = result;
239 }
240 return result;
241 }
242
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 const DexFile* dex_file_;
246 const DexFile::TypeList* interface_type_list_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 const mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800248 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249
250 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
251};
252
253class FieldHelper {
254 public:
255 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Brian Carlstromea46f952013-07-30 01:26:50 -0700256 explicit FieldHelper(const mirror::ArtField* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
257 FieldHelper(const mirror::ArtField* f, ClassLinker* l)
Ian Rogersca190662012-06-26 15:45:57 -0700258 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259
Brian Carlstromea46f952013-07-30 01:26:50 -0700260 void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 DCHECK(new_f != NULL);
262 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 mirror::DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 if (new_f_dex_cache != dex_cache_) {
265 dex_cache_ = new_f_dex_cache;
266 dex_file_ = NULL;
267 }
268 }
269 field_ = new_f;
270 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700271
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800273 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700274 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800275 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700276 DCHECK_LT(field_index, 2U);
277 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800278 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700279 const DexFile& dex_file = GetDexFile();
280 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700282
Ian Rogers50239c72013-06-17 14:53:22 -0700283 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800284 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700285 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700286 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700288 const DexFile& dex_file = GetDexFile();
289 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
290 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
291 if (resolve && (type == NULL)) {
292 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
293 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
294 }
295 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700297
Ian Rogersb726dcb2012-09-05 08:57:23 -0700298 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800299 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700300 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800301 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700302 DCHECK_LT(field_index, 2U);
303 // 0 == Class[] interfaces; 1 == Class[][] throws;
304 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800305 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700306 const DexFile& dex_file = GetDexFile();
307 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
308 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800309 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700310
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700312 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 return Primitive::GetType(GetTypeDescriptor()[0]);
314 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700315
Ian Rogersb726dcb2012-09-05 08:57:23 -0700316 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 Primitive::Type type = GetTypeAsPrimitiveType();
318 return type != Primitive::kPrimNot;
319 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700320
Ian Rogersb726dcb2012-09-05 08:57:23 -0700321 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 Primitive::Type type = GetTypeAsPrimitiveType();
323 return Primitive::FieldSize(type);
324 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800325
326 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
327 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700330 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700331 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700332 DCHECK(field_->IsStatic());
333 DCHECK_LT(field_index, 2U);
334 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800335 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700336 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800337 return declaring_class_descriptor_.c_str();
338 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700339 const DexFile& dex_file = GetDexFile();
340 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
341 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 }
343
344 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
346 mirror::DexCache* result = dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800347 if (result == NULL) {
348 result = field_->GetDeclaringClass()->GetDexCache();
349 dex_cache_ = result;
350 }
351 return result;
352 }
353 ClassLinker* GetClassLinker() {
354 ClassLinker* result = class_linker_;
355 if (result == NULL) {
356 result = Runtime::Current()->GetClassLinker();
357 class_linker_ = result;
358 }
359 return result;
360 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700361 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700362 if (dex_file_ == NULL) {
363 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700365 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 }
367
368 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800370 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700371 const mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800372 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373
374 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
375};
376
377class MethodHelper {
378 public:
Ian Rogersca190662012-06-26 15:45:57 -0700379 MethodHelper()
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
381 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 explicit MethodHelper(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700384 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700385 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
386 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 SetMethod(m);
388 }
Ian Rogersca190662012-06-26 15:45:57 -0700389
Brian Carlstromea46f952013-07-30 01:26:50 -0700390 MethodHelper(const mirror::ArtMethod* m, ClassLinker* l)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700392 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
393 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800394 SetMethod(m);
395 }
396
Brian Carlstromea46f952013-07-30 01:26:50 -0700397 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 DCHECK(new_m != NULL);
399 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 mirror::Class* klass = new_m->GetDeclaringClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 if (klass->IsProxyClass()) {
402 dex_cache_ = NULL;
403 dex_file_ = NULL;
404 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 mirror::DexCache* new_m_dex_cache = klass->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 if (new_m_dex_cache != dex_cache_) {
407 dex_cache_ = new_m_dex_cache;
408 dex_file_ = NULL;
409 }
410 }
411 }
412 SetMethod(new_m);
413 shorty_ = NULL;
414 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700415
Brian Carlstromea46f952013-07-30 01:26:50 -0700416 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700417 return method_;
418 }
419
Ian Rogersb726dcb2012-09-05 08:57:23 -0700420 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800422 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700423 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800424 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
425 } else {
426 Runtime* runtime = Runtime::Current();
427 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700428 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800429 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700430 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800431 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700432 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800433 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700434 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800435 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700436 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800437 }
438 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700440
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800443 uint32_t dex_method_idx = method_->GetDexMethodIndex();
444 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700447
Jeff Hao9a916d32013-06-27 18:45:37 -0700448 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800449 const char* result = shorty_;
450 if (result == NULL) {
451 const DexFile& dex_file = GetDexFile();
452 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
453 &shorty_len_);
454 shorty_ = result;
455 }
456 return result;
457 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800460 if (shorty_ == NULL) {
461 GetShorty();
462 }
463 return shorty_len_;
464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800467 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800468 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700469 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800470 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
471 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700472 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800473 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700476 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 const DexFile& dex_file = GetDexFile();
478 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
479 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700481 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800482 const DexFile::ProtoId& proto = GetPrototype();
483 return GetDexFile().GetProtoParameters(proto);
484 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 mirror::Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 const DexFile& dex_file = GetDexFile();
488 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
489 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
490 uint16_t return_type_idx = proto_id.return_type_idx_;
491 return GetClassFromTypeIdx(return_type_idx);
492 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700494 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800495 const DexFile& dex_file = GetDexFile();
496 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
497 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
498 uint16_t return_type_idx = proto_id.return_type_idx_;
499 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
500 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700501
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700502 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700503 if (dex_pc == DexFile::kDexNoIndex) {
504 return method_->IsNative() ? -2 : -1;
505 } else {
506 const DexFile& dex_file = GetDexFile();
507 return dex_file.GetLineNumFromPC(method_, dex_pc);
508 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700510
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700511 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800512 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700513 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700514 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700515 return "<runtime method>";
516 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700517 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
518 }
519
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700520 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
521 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700523
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700524 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
525 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526 }
527
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700528 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
529 return GetDexFile().GetClassDef(GetClassDefIndex());
530 }
531
532 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700533 return method_->GetDeclaringClass()->GetClassLoader();
534 }
535
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800536 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800537 return method_->IsStatic();
538 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700539
Ian Rogersb726dcb2012-09-05 08:57:23 -0700540 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700541 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800542 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700543
Ian Rogersb726dcb2012-09-05 08:57:23 -0700544 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 // "1 +" because the first in Args is the receiver.
546 // "- 1" because we don't count the return type.
547 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
548 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700549
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800550 // Get the primitive type associated with the given parameter.
551 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 CHECK_LT(param, NumArgs());
553 if (IsStatic()) {
554 param++; // 0th argument must skip return value at start of the shorty
555 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800556 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800557 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800558 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800559 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800561 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
562 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
563 Primitive::Type type = GetParamPrimitiveType(param);
564 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
565 }
566
567 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700568 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800569 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800570 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700572 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700574 const DexFile& dex_file = GetDexFile();
575 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800576 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800577 const DexFile::MethodId& other_mid =
578 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800579 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800580 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700581 const DexFile& other_dex_file = other->GetDexFile();
582 const DexFile::MethodId& other_mid =
583 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700584 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
585 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700586 return false; // Name mismatch.
587 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700588 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800589 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700591 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700592 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800593 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
594 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700595
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800598 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
599 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700600
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800603 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800604 if (type == NULL) {
605 type = GetClassLinker()->ResolveType(type_idx, method_);
606 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
607 }
608 return type;
609 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700610
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700611 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 const DexFile& dex_file = GetDexFile();
614 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
615 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700616
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700619 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700621
Ian Rogersb726dcb2012-09-05 08:57:23 -0700622 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 const DexFile* result = dex_file_;
624 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 const mirror::DexCache* dex_cache = GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700626 result = dex_file_ = dex_cache->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800627 }
628 return *result;
629 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
632 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700633 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 mirror::Class* klass = method_->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700635 result = klass->GetDexCache();
636 dex_cache_ = result;
637 }
638 return result;
639 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700640
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
642 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700643 if (UNLIKELY(s == NULL)) {
644 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, GetDexCache());
645 }
646 return s;
647 }
648
Ian Rogers83883d72013-10-21 21:07:24 -0700649 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
651 const DexFile& dexfile = GetDexFile();
652 if (&dexfile == &other_dexfile) {
653 return method_->GetDexMethodIndex();
654 }
655 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
656 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
657 const DexFile::StringId* other_descriptor =
658 other_dexfile.FindStringId(mid_declaring_class_descriptor);
659 if (other_descriptor != nullptr) {
660 const DexFile::TypeId* other_type_id =
661 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
662 if (other_type_id != nullptr) {
663 const char* mid_name = dexfile.GetMethodName(mid);
664 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
665 if (other_name != nullptr) {
666 uint16_t other_return_type_idx;
667 std::vector<uint16_t> other_param_type_idxs;
668 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
669 &other_return_type_idx,
670 &other_param_type_idxs);
671 if (success) {
672 const DexFile::ProtoId* other_sig =
673 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
674 if (other_sig != nullptr) {
675 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
676 *other_name,
677 *other_sig);
678 if (other_mid != nullptr) {
679 return other_dexfile.GetIndexForMethodId(*other_mid);
680 }
681 }
682 }
683 }
684 }
685 }
686 return DexFile::kDexNoIndex;
687 }
688
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689 private:
690 // Set the method_ field, for proxy methods looking up the interface method via the resolved
691 // methods table.
Brian Carlstromea46f952013-07-30 01:26:50 -0700692 void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800693 if (method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700695 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800697 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700698 DCHECK(interface_method != NULL);
699 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800700 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800701 }
702 }
703 method_ = method;
704 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700705
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800706 ClassLinker* GetClassLinker() {
707 ClassLinker* result = class_linker_;
708 if (result == NULL) {
709 result = Runtime::Current()->GetClassLinker();
710 class_linker_ = result;
711 }
712 return result;
713 }
714
715 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800716 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800717 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700718 const mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800719 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800720 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800721
722 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
723};
724
725} // namespace art
726
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700727#endif // ART_RUNTIME_OBJECT_UTILS_H_