blob: e37510c228cae4d002c33238e262a0f3e4403346 [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:
Mathieu Chartier590fee92013-09-13 13:46:47 -070070 explicit ClassHelper(const mirror::Class* c )
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 : interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070073 klass_(NULL) {
74 if (c != NULL) {
75 ChangeClass(c);
76 }
Elliott Hughes91250e02011-12-13 22:30:35 -080077 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 void ChangeClass(const mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070081 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
Mathieu Chartier590fee92013-09-13 13:46:47 -070082 if (!new_c->IsClass()) {
83 LOG(FATAL) << "new_c=" << new_c << " cc " << new_c->GetClass() << " ccc "
84 << ((new_c->GetClass() != nullptr) ? new_c->GetClass()->GetClass() : NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080085 }
86 klass_ = new_c;
87 interface_type_list_ = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 }
89
Elliott Hughes91250e02011-12-13 22:30:35 -080090 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
91 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070093 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080094 if (UNLIKELY(klass_->IsArrayClass())) {
95 return GetArrayDescriptor();
96 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080097 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080098 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080099 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
100 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101 } else {
102 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700103 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800104 return dex_file.GetTypeDescriptor(type_id);
105 }
106 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800107
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800109 std::string result("[");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 const mirror::Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700111 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800112 ChangeClass(klass_->GetComponentType());
113 result += GetDescriptor();
114 ChangeClass(saved_klass);
115 descriptor_ = result;
116 return descriptor_.c_str();
117 }
118
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700119 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
120 DCHECK(klass_ != nullptr);
121 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
122 if (class_def_idx == DexFile::kDexNoIndex16) {
123 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700125 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800127
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700129 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 if (klass_->IsPrimitive()) {
131 return 0;
132 } else if (klass_->IsArrayClass()) {
133 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800134 } else if (klass_->IsProxyClass()) {
135 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800136 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
138 if (interfaces == NULL) {
139 return 0;
140 } else {
141 return interfaces->Size();
142 }
143 }
144 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800145
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700148 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149 DCHECK(!klass_->IsPrimitive());
150 DCHECK(!klass_->IsArrayClass());
151 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
152 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700156 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800157 DCHECK(!klass_->IsPrimitive());
158 if (klass_->IsArrayClass()) {
159 if (idx == 0) {
160 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
161 } else {
162 DCHECK_EQ(1U, idx);
163 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
164 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800165 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700166 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800167 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700168 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800170 if (interface == NULL) {
171 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
172 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
173 }
174 return interface;
175 }
176 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800177
Ian Rogersb726dcb2012-09-05 08:57:23 -0700178 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700179 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700181 const DexFile::ClassDef* dex_class_def = GetClassDef();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800182 CHECK(dex_class_def != NULL);
183 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800184 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800185
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 if (dex_cache != NULL && !klass_->IsProxyClass()) {
189 return dex_cache->GetLocation()->ToModifiedUtf8();
190 } else {
191 // Arrays and proxies are generated and have no corresponding dex file location.
192 return "generated class";
193 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800194 }
195
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700197 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800198 }
199
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700201 return klass_->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700202 }
203
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800204 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 const DexFile::TypeList* result = interface_type_list_;
208 if (result == NULL) {
209 const DexFile::ClassDef* class_def = GetClassDef();
210 if (class_def != NULL) {
211 result = GetDexFile().GetInterfacesList(*class_def);
212 interface_type_list_ = result;
213 }
214 }
215 return result;
216 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800217
Mathieu Chartier590fee92013-09-13 13:46:47 -0700218 ClassLinker* GetClassLinker() ALWAYS_INLINE {
219 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220 }
221
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 const DexFile::TypeList* interface_type_list_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 const mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800224 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225
226 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
227};
228
229class FieldHelper {
230 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700231 FieldHelper() : field_(NULL) {}
232 explicit FieldHelper(const mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233
Brian Carlstromea46f952013-07-30 01:26:50 -0700234 void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 DCHECK(new_f != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800236 field_ = new_f;
237 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700238
Ian Rogersb726dcb2012-09-05 08:57:23 -0700239 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800240 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700241 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800242 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700243 DCHECK_LT(field_index, 2U);
244 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800245 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700246 const DexFile& dex_file = GetDexFile();
247 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800248 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700249
Ian Rogers50239c72013-06-17 14:53:22 -0700250 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800251 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700252 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700253 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800254 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700255 const DexFile& dex_file = GetDexFile();
256 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
257 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
258 if (resolve && (type == NULL)) {
259 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
260 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
261 }
262 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700264
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800266 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700267 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800268 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700269 DCHECK_LT(field_index, 2U);
270 // 0 == Class[] interfaces; 1 == Class[][] throws;
271 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800272 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700273 const DexFile& dex_file = GetDexFile();
274 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
275 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700277
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 return Primitive::GetType(GetTypeDescriptor()[0]);
281 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700282
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 Primitive::Type type = GetTypeAsPrimitiveType();
285 return type != Primitive::kPrimNot;
286 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700287
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 Primitive::Type type = GetTypeAsPrimitiveType();
290 return Primitive::FieldSize(type);
291 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800292
293 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
294 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700297 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700298 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700299 DCHECK(field_->IsStatic());
300 DCHECK_LT(field_index, 2U);
301 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800302 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700303 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800304 return declaring_class_descriptor_.c_str();
305 }
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.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800309 }
310
311 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700313 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700315 ClassLinker* GetClassLinker() ALWAYS_INLINE {
316 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800317 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700319 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 }
Brian Carlstromea46f952013-07-30 01:26:50 -0700321 const mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800322 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323
324 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
325};
326
327class MethodHelper {
328 public:
Ian Rogersca190662012-06-26 15:45:57 -0700329 MethodHelper()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700330 : method_(NULL), shorty_(NULL),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700331 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700332
Brian Carlstromea46f952013-07-30 01:26:50 -0700333 explicit MethodHelper(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Mathieu Chartier590fee92013-09-13 13:46:47 -0700335 : method_(NULL), shorty_(NULL), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800336 SetMethod(m);
337 }
338
Brian Carlstromea46f952013-07-30 01:26:50 -0700339 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 DCHECK(new_m != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800341 SetMethod(new_m);
342 shorty_ = NULL;
343 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700344
Brian Carlstromea46f952013-07-30 01:26:50 -0700345 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700346 return method_;
347 }
348
Ian Rogersb726dcb2012-09-05 08:57:23 -0700349 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800350 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800351 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700352 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800353 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
354 } else {
355 Runtime* runtime = Runtime::Current();
356 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700357 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700358 } else if (method_ == runtime->GetImtConflictMethod()) {
359 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800360 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700361 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800362 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700363 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800364 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700365 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800366 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700367 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800368 }
369 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800370 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700371
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800374 uint32_t dex_method_idx = method_->GetDexMethodIndex();
375 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700376 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
377 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700379
Jeff Hao9a916d32013-06-27 18:45:37 -0700380 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800381 const char* result = shorty_;
382 if (result == NULL) {
383 const DexFile& dex_file = GetDexFile();
384 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
385 &shorty_len_);
386 shorty_ = result;
387 }
388 return result;
389 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800392 if (shorty_ == NULL) {
393 GetShorty();
394 }
395 return shorty_len_;
396 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397
Ian Rogersd91d6d62013-09-25 20:26:14 -0700398 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800399 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800400 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700401 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800402 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
403 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700404 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800405 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700407
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700408 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800409 const DexFile& dex_file = GetDexFile();
410 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
411 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700412
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700413 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 const DexFile::ProtoId& proto = GetPrototype();
415 return GetDexFile().GetProtoParameters(proto);
416 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700417
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 mirror::Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 const DexFile& dex_file = GetDexFile();
420 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
421 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
422 uint16_t return_type_idx = proto_id.return_type_idx_;
423 return GetClassFromTypeIdx(return_type_idx);
424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700426 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800427 const DexFile& dex_file = GetDexFile();
428 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
429 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
430 uint16_t return_type_idx = proto_id.return_type_idx_;
431 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
432 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700433
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700434 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700435 if (dex_pc == DexFile::kDexNoIndex) {
436 return method_->IsNative() ? -2 : -1;
437 } else {
438 const DexFile& dex_file = GetDexFile();
439 return dex_file.GetLineNumFromPC(method_, dex_pc);
440 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700442
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700443 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700445 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700446 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700447 return "<runtime method>";
448 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700449 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
450 }
451
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700452 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
453 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700456 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
457 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 }
459
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700460 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
461 return GetDexFile().GetClassDef(GetClassDefIndex());
462 }
463
464 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465 return method_->GetDeclaringClass()->GetClassLoader();
466 }
467
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800468 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 return method_->IsStatic();
470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471
Ian Rogersb726dcb2012-09-05 08:57:23 -0700472 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700473 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475
Ian Rogersb726dcb2012-09-05 08:57:23 -0700476 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 // "1 +" because the first in Args is the receiver.
478 // "- 1" because we don't count the return type.
479 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
480 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800482 // Get the primitive type associated with the given parameter.
483 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800484 CHECK_LT(param, NumArgs());
485 if (IsStatic()) {
486 param++; // 0th argument must skip return value at start of the shorty
487 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800488 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800489 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800490 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800491 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800493 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
494 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
495 Primitive::Type type = GetParamPrimitiveType(param);
496 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
497 }
498
499 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700500 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800501 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800502 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700503
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700506 const DexFile& dex_file = GetDexFile();
507 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 const DexFile::MethodId& other_mid =
510 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800511 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800512 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700513 const DexFile& other_dex_file = other->GetDexFile();
514 const DexFile::MethodId& other_mid =
515 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700516 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
517 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700518 return false; // Name mismatch.
519 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700520 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
526 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700527
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700529 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800530 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
531 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 if (type == NULL) {
537 type = GetClassLinker()->ResolveType(type_idx, method_);
538 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
539 }
540 return type;
541 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700542
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700543 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700544 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 const DexFile& dex_file = GetDexFile();
546 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
547 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700548
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700551 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553
Ian Rogersb726dcb2012-09-05 08:57:23 -0700554 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700555 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800556 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700559 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700561
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
563 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700564 if (UNLIKELY(s == NULL)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700565 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
566 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700567 }
568 return s;
569 }
570
Ian Rogers83883d72013-10-21 21:07:24 -0700571 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
572 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
573 const DexFile& dexfile = GetDexFile();
574 if (&dexfile == &other_dexfile) {
575 return method_->GetDexMethodIndex();
576 }
577 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
578 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
579 const DexFile::StringId* other_descriptor =
580 other_dexfile.FindStringId(mid_declaring_class_descriptor);
581 if (other_descriptor != nullptr) {
582 const DexFile::TypeId* other_type_id =
583 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
584 if (other_type_id != nullptr) {
585 const char* mid_name = dexfile.GetMethodName(mid);
586 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
587 if (other_name != nullptr) {
588 uint16_t other_return_type_idx;
589 std::vector<uint16_t> other_param_type_idxs;
590 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
591 &other_return_type_idx,
592 &other_param_type_idxs);
593 if (success) {
594 const DexFile::ProtoId* other_sig =
595 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
596 if (other_sig != nullptr) {
597 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
598 *other_name,
599 *other_sig);
600 if (other_mid != nullptr) {
601 return other_dexfile.GetIndexForMethodId(*other_mid);
602 }
603 }
604 }
605 }
606 }
607 }
608 return DexFile::kDexNoIndex;
609 }
610
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800611 private:
612 // Set the method_ field, for proxy methods looking up the interface method via the resolved
613 // methods table.
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800615 if (method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800616 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700617 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700618 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800619 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700620 DCHECK(interface_method != NULL);
621 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800622 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 }
624 }
625 method_ = method;
626 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627
Mathieu Chartier590fee92013-09-13 13:46:47 -0700628 ClassLinker* GetClassLinker() ALWAYS_INLINE {
629 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800630 }
631
Brian Carlstromea46f952013-07-30 01:26:50 -0700632 const mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800634 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800635
636 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
637};
638
639} // namespace art
640
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700641#endif // ART_RUNTIME_OBJECT_UTILS_H_