blob: 407aa650ccbb2fd467d5c88e799da67e30106056 [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
Mathieu Chartierc528dba2013-11-26 12:00:11 -080037template <typename T>
Ian Rogers672f5202012-01-12 18:06:40 -080038class ObjectLock {
39 public:
Mathieu Chartierc528dba2013-11-26 12:00:11 -080040 explicit ObjectLock(Thread* self, const SirtRef<T>* object)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070041 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070042 : self_(self), obj_(object) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080043 CHECK(object != nullptr);
44 CHECK(object->get() != nullptr);
45 obj_->get()->MonitorEnter(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080046 }
47
Ian Rogersb726dcb2012-09-05 08:57:23 -070048 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080049 obj_->get()->MonitorExit(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080050 }
51
Ian Rogers05f30572013-02-20 12:13:11 -080052 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053 Monitor::Wait(self_, obj_->get(), 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080054 }
55
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 obj_->get()->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080058 }
59
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080061 obj_->get()->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080062 }
63
64 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 Thread* const self_;
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080066 const SirtRef<T>* const obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080067 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
68};
69
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070class ClassHelper {
71 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 explicit ClassHelper(const mirror::Class* c )
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -070074 : interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070075 klass_(NULL) {
76 if (c != NULL) {
77 ChangeClass(c);
78 }
Elliott Hughes91250e02011-12-13 22:30:35 -080079 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 void ChangeClass(const mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070083 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
Mathieu Chartier590fee92013-09-13 13:46:47 -070084 if (!new_c->IsClass()) {
85 LOG(FATAL) << "new_c=" << new_c << " cc " << new_c->GetClass() << " ccc "
86 << ((new_c->GetClass() != nullptr) ? new_c->GetClass()->GetClass() : NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 }
88 klass_ = new_c;
89 interface_type_list_ = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 }
91
Elliott Hughes91250e02011-12-13 22:30:35 -080092 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
93 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070095 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080096 if (UNLIKELY(klass_->IsArrayClass())) {
97 return GetArrayDescriptor();
98 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080099 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800100 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800101 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
102 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800103 } else {
104 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700105 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 return dex_file.GetTypeDescriptor(type_id);
107 }
108 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800109
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800111 std::string result("[");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 const mirror::Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700113 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800114 ChangeClass(klass_->GetComponentType());
115 result += GetDescriptor();
116 ChangeClass(saved_klass);
117 descriptor_ = result;
118 return descriptor_.c_str();
119 }
120
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700121 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 DCHECK(klass_ != nullptr);
123 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
124 if (class_def_idx == DexFile::kDexNoIndex16) {
125 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800129
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700131 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 if (klass_->IsPrimitive()) {
133 return 0;
134 } else if (klass_->IsArrayClass()) {
135 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800136 } else if (klass_->IsProxyClass()) {
137 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800139 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
140 if (interfaces == NULL) {
141 return 0;
142 } else {
143 return interfaces->Size();
144 }
145 }
146 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800147
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700150 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800151 DCHECK(!klass_->IsPrimitive());
152 DCHECK(!klass_->IsArrayClass());
153 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
154 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800155
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700158 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 DCHECK(!klass_->IsPrimitive());
160 if (klass_->IsArrayClass()) {
161 if (idx == 0) {
162 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
163 } else {
164 DCHECK_EQ(1U, idx);
165 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
166 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800167 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700168 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800169 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700170 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 if (interface == NULL) {
173 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
174 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
175 }
176 return interface;
177 }
178 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800179
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700181 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800182 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700183 const DexFile::ClassDef* dex_class_def = GetClassDef();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800184 CHECK(dex_class_def != NULL);
185 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800187
Ian Rogersb726dcb2012-09-05 08:57:23 -0700188 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 if (dex_cache != NULL && !klass_->IsProxyClass()) {
191 return dex_cache->GetLocation()->ToModifiedUtf8();
192 } else {
193 // Arrays and proxies are generated and have no corresponding dex file location.
194 return "generated class";
195 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800196 }
197
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700199 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 }
201
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700203 return klass_->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700204 }
205
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700207 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700208 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 const DexFile::TypeList* result = interface_type_list_;
210 if (result == NULL) {
211 const DexFile::ClassDef* class_def = GetClassDef();
212 if (class_def != NULL) {
213 result = GetDexFile().GetInterfacesList(*class_def);
214 interface_type_list_ = result;
215 }
216 }
217 return result;
218 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800219
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220 ClassLinker* GetClassLinker() ALWAYS_INLINE {
221 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 }
223
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224 const DexFile::TypeList* interface_type_list_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 const mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800226 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227
228 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
229};
230
231class FieldHelper {
232 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233 FieldHelper() : field_(NULL) {}
234 explicit FieldHelper(const mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235
Brian Carlstromea46f952013-07-30 01:26:50 -0700236 void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 DCHECK(new_f != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 field_ = new_f;
239 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700240
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800242 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700243 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800244 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700245 DCHECK_LT(field_index, 2U);
246 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800247 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700248 const DexFile& dex_file = GetDexFile();
249 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800250 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700251
Ian Rogers50239c72013-06-17 14:53:22 -0700252 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800253 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700254 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700255 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800256 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700257 const DexFile& dex_file = GetDexFile();
258 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
259 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
260 if (resolve && (type == NULL)) {
261 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
262 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
263 }
264 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700266
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800268 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700269 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800270 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700271 DCHECK_LT(field_index, 2U);
272 // 0 == Class[] interfaces; 1 == Class[][] throws;
273 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800274 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700275 const DexFile& dex_file = GetDexFile();
276 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
277 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800278 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700279
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800282 return Primitive::GetType(GetTypeDescriptor()[0]);
283 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700284
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 Primitive::Type type = GetTypeAsPrimitiveType();
287 return type != Primitive::kPrimNot;
288 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700289
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 Primitive::Type type = GetTypeAsPrimitiveType();
292 return Primitive::FieldSize(type);
293 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800294
295 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
296 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700298 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700299 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700300 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700301 DCHECK(field_->IsStatic());
302 DCHECK_LT(field_index, 2U);
303 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800304 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700305 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800306 return declaring_class_descriptor_.c_str();
307 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700308 const DexFile& dex_file = GetDexFile();
309 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
310 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 }
312
313 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700315 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700317 ClassLinker* GetClassLinker() ALWAYS_INLINE {
318 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700321 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700323 const mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800324 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325
326 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
327};
328
329class MethodHelper {
330 public:
Ian Rogersca190662012-06-26 15:45:57 -0700331 MethodHelper()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700332 : method_(NULL), shorty_(NULL),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700334
Brian Carlstromea46f952013-07-30 01:26:50 -0700335 explicit MethodHelper(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700337 : method_(NULL), shorty_(NULL), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800338 SetMethod(m);
339 }
340
Brian Carlstromea46f952013-07-30 01:26:50 -0700341 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 DCHECK(new_m != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800343 SetMethod(new_m);
344 shorty_ = NULL;
345 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700346
Brian Carlstromea46f952013-07-30 01:26:50 -0700347 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700348 return method_;
349 }
350
Ian Rogersb726dcb2012-09-05 08:57:23 -0700351 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800352 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800353 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700354 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800355 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
356 } else {
357 Runtime* runtime = Runtime::Current();
358 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700359 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700360 } else if (method_ == runtime->GetImtConflictMethod()) {
361 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800362 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700363 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800364 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700365 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800366 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700367 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800368 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700369 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800370 }
371 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800375 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800376 uint32_t dex_method_idx = method_->GetDexMethodIndex();
377 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700378 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
379 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700381
Jeff Hao9a916d32013-06-27 18:45:37 -0700382 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 const char* result = shorty_;
384 if (result == NULL) {
385 const DexFile& dex_file = GetDexFile();
386 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
387 &shorty_len_);
388 shorty_ = result;
389 }
390 return result;
391 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700392
Ian Rogersb726dcb2012-09-05 08:57:23 -0700393 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800394 if (shorty_ == NULL) {
395 GetShorty();
396 }
397 return shorty_len_;
398 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700399
Ian Rogersd91d6d62013-09-25 20:26:14 -0700400 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800402 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700403 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800404 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
405 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700406 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800407 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700409
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700410 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411 const DexFile& dex_file = GetDexFile();
412 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
413 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700415 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800416 const DexFile::ProtoId& proto = GetPrototype();
417 return GetDexFile().GetProtoParameters(proto);
418 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800420 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 const DexFile& dex_file = GetDexFile();
422 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
423 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
424 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800425 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700428 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 const DexFile& dex_file = GetDexFile();
430 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
431 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
432 uint16_t return_type_idx = proto_id.return_type_idx_;
433 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
434 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700435
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700436 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700437 if (dex_pc == DexFile::kDexNoIndex) {
438 return method_->IsNative() ? -2 : -1;
439 } else {
440 const DexFile& dex_file = GetDexFile();
441 return dex_file.GetLineNumFromPC(method_, dex_pc);
442 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700445 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700447 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700448 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700449 return "<runtime method>";
450 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700451 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
452 }
453
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700454 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
455 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800456 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700458 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
459 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460 }
461
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700462 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
463 return GetDexFile().GetClassDef(GetClassDefIndex());
464 }
465
466 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467 return method_->GetDeclaringClass()->GetClassLoader();
468 }
469
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800470 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 return method_->IsStatic();
472 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700473
Ian Rogersb726dcb2012-09-05 08:57:23 -0700474 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700475 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477
Ian Rogersb726dcb2012-09-05 08:57:23 -0700478 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800479 // "1 +" because the first in Args is the receiver.
480 // "- 1" because we don't count the return type.
481 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800484 // Get the primitive type associated with the given parameter.
485 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800486 CHECK_LT(param, NumArgs());
487 if (IsStatic()) {
488 param++; // 0th argument must skip return value at start of the shorty
489 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800490 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800491 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800492 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800495 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
496 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
497 Primitive::Type type = GetParamPrimitiveType(param);
498 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
499 }
500
501 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800503 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700506 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700508 const DexFile& dex_file = GetDexFile();
509 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800510 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 const DexFile::MethodId& other_mid =
512 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800513 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700515 const DexFile& other_dex_file = other->GetDexFile();
516 const DexFile::MethodId& other_mid =
517 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700518 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
519 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700520 return false; // Name mismatch.
521 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700522 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700524
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700525 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700526 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
528 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700531 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800532 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
533 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700534
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800535 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800538 if (type == NULL && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800539 type = GetClassLinker()->ResolveType(type_idx, method_);
540 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
541 }
542 return type;
543 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700544
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700546 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800547 const DexFile& dex_file = GetDexFile();
548 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
549 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700550
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700553 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800554 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700555
Ian Rogersb726dcb2012-09-05 08:57:23 -0700556 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700557 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800558 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700561 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700563
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
565 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700566 if (UNLIKELY(s == NULL)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700567 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
568 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700569 }
570 return s;
571 }
572
Ian Rogers83883d72013-10-21 21:07:24 -0700573 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
574 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
575 const DexFile& dexfile = GetDexFile();
576 if (&dexfile == &other_dexfile) {
577 return method_->GetDexMethodIndex();
578 }
579 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
580 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
581 const DexFile::StringId* other_descriptor =
582 other_dexfile.FindStringId(mid_declaring_class_descriptor);
583 if (other_descriptor != nullptr) {
584 const DexFile::TypeId* other_type_id =
585 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
586 if (other_type_id != nullptr) {
587 const char* mid_name = dexfile.GetMethodName(mid);
588 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
589 if (other_name != nullptr) {
590 uint16_t other_return_type_idx;
591 std::vector<uint16_t> other_param_type_idxs;
592 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
593 &other_return_type_idx,
594 &other_param_type_idxs);
595 if (success) {
596 const DexFile::ProtoId* other_sig =
597 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
598 if (other_sig != nullptr) {
599 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
600 *other_name,
601 *other_sig);
602 if (other_mid != nullptr) {
603 return other_dexfile.GetIndexForMethodId(*other_mid);
604 }
605 }
606 }
607 }
608 }
609 }
610 return DexFile::kDexNoIndex;
611 }
612
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 private:
614 // Set the method_ field, for proxy methods looking up the interface method via the resolved
615 // methods table.
Brian Carlstromea46f952013-07-30 01:26:50 -0700616 void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800617 if (method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700619 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700620 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800621 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700622 DCHECK(interface_method != NULL);
623 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800624 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800625 }
626 }
627 method_ = method;
628 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700629
Mathieu Chartier590fee92013-09-13 13:46:47 -0700630 ClassLinker* GetClassLinker() ALWAYS_INLINE {
631 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800632 }
633
Brian Carlstromea46f952013-07-30 01:26:50 -0700634 const mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800635 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800636 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800637
638 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
639};
640
641} // namespace art
642
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700643#endif // ART_RUNTIME_OBJECT_UTILS_H_