blob: 9e107a46d5f3acec59063343680ff69f5ec19e7c [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 Rogers2dd0e2c2013-01-24 12:42:14 -080039 explicit ObjectLock(Thread* self, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070040 : self_(self), obj_(object) {
Ian Rogers672f5202012-01-12 18:06:40 -080041 CHECK(object != NULL);
42 obj_->MonitorEnter(self_);
43 }
44
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080046 obj_->MonitorExit(self_);
47 }
48
Ian Rogers05f30572013-02-20 12:13:11 -080049 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
50 Monitor::Wait(self_, obj_, 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080051 }
52
Ian Rogersb726dcb2012-09-05 08:57:23 -070053 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080054 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080055 }
56
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080058 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080059 }
60
61 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 Thread* const self_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Object* obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080064 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
65};
66
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067class ClassHelper {
68 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 ClassHelper(const mirror::Class* c = NULL, ClassLinker* l = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers8b2c0b92013-09-19 02:56:49 -070071 : class_linker_(l),
Elliott Hughes91250e02011-12-13 22:30:35 -080072 dex_cache_(NULL),
73 dex_file_(NULL),
74 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
84 CHECK(new_c->IsClass()) << "new_c=" << new_c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080085 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 mirror::DexCache* new_c_dex_cache = new_c->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 if (new_c_dex_cache != dex_cache_) {
88 dex_cache_ = new_c_dex_cache;
89 dex_file_ = NULL;
90 }
91 }
92 klass_ = new_c;
93 interface_type_list_ = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080094 }
95
Elliott Hughes91250e02011-12-13 22:30:35 -080096 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
97 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070099 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800100 if (UNLIKELY(klass_->IsArrayClass())) {
101 return GetArrayDescriptor();
102 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800103 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800104 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800105 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
106 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800107 } else {
108 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700109 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110 return dex_file.GetTypeDescriptor(type_id);
111 }
112 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800113
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700114 StringPiece GetDescriptorAsStringPiece() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 CHECK(klass_ != NULL);
116 if (UNLIKELY(klass_->IsArrayClass() || klass_->IsPrimitive() || klass_->IsProxyClass())) {
117 return StringPiece(GetDescriptor());
118 } else {
119 const DexFile& dex_file = GetDexFile();
120 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
121 return dex_file.StringDataAsStringPieceByIdx(type_id.descriptor_idx_);
122 }
123 }
124
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800126 std::string result("[");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 const mirror::Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700128 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800129 ChangeClass(klass_->GetComponentType());
130 result += GetDescriptor();
131 ChangeClass(saved_klass);
132 descriptor_ = result;
133 return descriptor_.c_str();
134 }
135
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700136 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
137 DCHECK(klass_ != nullptr);
138 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
139 if (class_def_idx == DexFile::kDexNoIndex16) {
140 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800141 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700142 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800144
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700146 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800147 if (klass_->IsPrimitive()) {
148 return 0;
149 } else if (klass_->IsArrayClass()) {
150 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800151 } else if (klass_->IsProxyClass()) {
152 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
155 if (interfaces == NULL) {
156 return 0;
157 } else {
158 return interfaces->Size();
159 }
160 }
161 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800162
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700165 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 DCHECK(!klass_->IsPrimitive());
167 DCHECK(!klass_->IsArrayClass());
168 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
169 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700173 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 DCHECK(!klass_->IsPrimitive());
175 if (klass_->IsArrayClass()) {
176 if (idx == 0) {
177 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
178 } else {
179 DCHECK_EQ(1U, idx);
180 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
181 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800182 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700183 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800184 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700185 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 if (interface == NULL) {
188 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
189 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
190 }
191 return interface;
192 }
193 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800194
Ian Rogersb726dcb2012-09-05 08:57:23 -0700195 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700196 std::string descriptor(GetDescriptorAsStringPiece().as_string());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 const DexFile::ClassDef* dex_class_def = GetClassDef();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800199 CHECK(dex_class_def != NULL);
200 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800202
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 if (dex_cache != NULL && !klass_->IsProxyClass()) {
206 return dex_cache->GetLocation()->ToModifiedUtf8();
207 } else {
208 // Arrays and proxies are generated and have no corresponding dex file location.
209 return "generated class";
210 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 }
212
Ian Rogersb726dcb2012-09-05 08:57:23 -0700213 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700214 if (dex_file_ == NULL) {
215 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700217 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 }
219
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
221 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 if (result == NULL) {
223 DCHECK(klass_ != NULL);
224 result = klass_->GetDexCache();
225 dex_cache_ = result;
226 }
227 return result;
228 }
229
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700231 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 const DexFile::TypeList* result = interface_type_list_;
234 if (result == NULL) {
235 const DexFile::ClassDef* class_def = GetClassDef();
236 if (class_def != NULL) {
237 result = GetDexFile().GetInterfacesList(*class_def);
238 interface_type_list_ = result;
239 }
240 }
241 return result;
242 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800243
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 ClassLinker* GetClassLinker() {
245 ClassLinker* result = class_linker_;
246 if (result == NULL) {
247 result = Runtime::Current()->GetClassLinker();
248 class_linker_ = result;
249 }
250 return result;
251 }
252
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 const DexFile* dex_file_;
256 const DexFile::TypeList* interface_type_list_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 const mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800258 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259
260 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
261};
262
263class FieldHelper {
264 public:
265 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Brian Carlstromea46f952013-07-30 01:26:50 -0700266 explicit FieldHelper(const mirror::ArtField* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
267 FieldHelper(const mirror::ArtField* f, ClassLinker* l)
Ian Rogersca190662012-06-26 15:45:57 -0700268 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269
Brian Carlstromea46f952013-07-30 01:26:50 -0700270 void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 DCHECK(new_f != NULL);
272 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 mirror::DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 if (new_f_dex_cache != dex_cache_) {
275 dex_cache_ = new_f_dex_cache;
276 dex_file_ = NULL;
277 }
278 }
279 field_ = new_f;
280 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700281
Ian Rogersb726dcb2012-09-05 08:57:23 -0700282 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800283 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700284 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800285 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700286 DCHECK_LT(field_index, 2U);
287 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800288 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700289 const DexFile& dex_file = GetDexFile();
290 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700292
293 StringPiece GetNameAsStringPiece() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
294 uint32_t field_index = field_->GetDexFieldIndex();
295 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
296 return StringPiece(GetName());
297 }
298 const DexFile& dex_file = GetDexFile();
299 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
300 return dex_file.StringDataAsStringPieceByIdx(field_id.name_idx_);
301 }
302
Ian Rogers50239c72013-06-17 14:53:22 -0700303 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800304 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700305 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700306 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 }
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 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
311 if (resolve && (type == NULL)) {
312 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
313 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
314 }
315 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700317
Ian Rogersb726dcb2012-09-05 08:57:23 -0700318 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800319 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700320 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800321 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700322 DCHECK_LT(field_index, 2U);
323 // 0 == Class[] interfaces; 1 == Class[][] throws;
324 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800325 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700326 const DexFile& dex_file = GetDexFile();
327 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
328 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700330
331 StringPiece GetTypeDescriptorAsStringPiece() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
332 uint32_t field_index = field_->GetDexFieldIndex();
333 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
334 return StringPiece(GetTypeDescriptor());
335 }
336 const DexFile& dex_file = GetDexFile();
337 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
338 const DexFile::TypeId& type_id = dex_file.GetTypeId(field_id.type_idx_);
339 return dex_file.StringDataAsStringPieceByIdx(type_id.descriptor_idx_);
340 }
341
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700343 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800344 return Primitive::GetType(GetTypeDescriptor()[0]);
345 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700346
Ian Rogersb726dcb2012-09-05 08:57:23 -0700347 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800348 Primitive::Type type = GetTypeAsPrimitiveType();
349 return type != Primitive::kPrimNot;
350 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700351
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800353 Primitive::Type type = GetTypeAsPrimitiveType();
354 return Primitive::FieldSize(type);
355 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800356
357 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
358 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700360 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700361 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700362 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700363 DCHECK(field_->IsStatic());
364 DCHECK_LT(field_index, 2U);
365 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800366 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700367 declaring_class_descriptor_ = kh.GetDescriptorAsStringPiece().as_string();
Ian Rogersc2b44472011-12-14 21:17:17 -0800368 return declaring_class_descriptor_.c_str();
369 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700370 const DexFile& dex_file = GetDexFile();
371 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
372 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 }
374
375 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
377 mirror::DexCache* result = dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378 if (result == NULL) {
379 result = field_->GetDeclaringClass()->GetDexCache();
380 dex_cache_ = result;
381 }
382 return result;
383 }
384 ClassLinker* GetClassLinker() {
385 ClassLinker* result = class_linker_;
386 if (result == NULL) {
387 result = Runtime::Current()->GetClassLinker();
388 class_linker_ = result;
389 }
390 return result;
391 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700392 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700393 if (dex_file_ == NULL) {
394 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700396 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800397 }
398
399 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800401 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700402 const mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800403 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800404
405 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
406};
407
408class MethodHelper {
409 public:
Ian Rogersca190662012-06-26 15:45:57 -0700410 MethodHelper()
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700411 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
412 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700413
Brian Carlstromea46f952013-07-30 01:26:50 -0700414 explicit MethodHelper(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700416 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
417 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 SetMethod(m);
419 }
Ian Rogersca190662012-06-26 15:45:57 -0700420
Brian Carlstromea46f952013-07-30 01:26:50 -0700421 MethodHelper(const mirror::ArtMethod* m, ClassLinker* l)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700423 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
424 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800425 SetMethod(m);
426 }
427
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 DCHECK(new_m != NULL);
430 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 mirror::Class* klass = new_m->GetDeclaringClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432 if (klass->IsProxyClass()) {
433 dex_cache_ = NULL;
434 dex_file_ = NULL;
435 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 mirror::DexCache* new_m_dex_cache = klass->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 if (new_m_dex_cache != dex_cache_) {
438 dex_cache_ = new_m_dex_cache;
439 dex_file_ = NULL;
440 }
441 }
442 }
443 SetMethod(new_m);
444 shorty_ = NULL;
445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700448 return method_;
449 }
450
Ian Rogersb726dcb2012-09-05 08:57:23 -0700451 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800452 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800453 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700454 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800455 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
456 } else {
457 Runtime* runtime = Runtime::Current();
458 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700459 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800460 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700461 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800462 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700463 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800464 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700465 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800466 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700467 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800468 }
469 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700472 StringPiece GetNameAsStringPiece() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
473 const DexFile& dex_file = GetDexFile();
474 uint32_t dex_method_idx = method_->GetDexMethodIndex();
475 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
476 return StringPiece(GetName());
477 }
478 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
479 return dex_file.StringDataAsStringPieceByIdx(method_id.name_idx_);
480 }
481
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800483 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800484 uint32_t dex_method_idx = method_->GetDexMethodIndex();
485 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800486 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
487 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700488
Jeff Hao9a916d32013-06-27 18:45:37 -0700489 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800490 const char* result = shorty_;
491 if (result == NULL) {
492 const DexFile& dex_file = GetDexFile();
493 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
494 &shorty_len_);
495 shorty_ = result;
496 }
497 return result;
498 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499
Ian Rogersb726dcb2012-09-05 08:57:23 -0700500 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800501 if (shorty_ == NULL) {
502 GetShorty();
503 }
504 return shorty_len_;
505 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700506
Ian Rogersb726dcb2012-09-05 08:57:23 -0700507 const std::string GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800509 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700510 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800511 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
512 } else {
513 return "<no signature>";
514 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800515 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700516
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700517 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800518 const DexFile& dex_file = GetDexFile();
519 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
520 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700521
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700522 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800523 const DexFile::ProtoId& proto = GetPrototype();
524 return GetDexFile().GetProtoParameters(proto);
525 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 mirror::Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 const DexFile& dex_file = GetDexFile();
529 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
530 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
531 uint16_t return_type_idx = proto_id.return_type_idx_;
532 return GetClassFromTypeIdx(return_type_idx);
533 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700534
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700535 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 const DexFile& dex_file = GetDexFile();
537 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
538 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
539 uint16_t return_type_idx = proto_id.return_type_idx_;
540 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
541 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700542
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700543 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 if (dex_pc == DexFile::kDexNoIndex) {
545 return method_->IsNative() ? -2 : -1;
546 } else {
547 const DexFile& dex_file = GetDexFile();
548 return dex_file.GetLineNumFromPC(method_, dex_pc);
549 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700551
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700552 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800553 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700554 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700555 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700556 return "<runtime method>";
557 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700558 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
559 }
560
561 StringPiece GetDeclaringClassDescriptorAsStringPiece()
562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
563 const DexFile& dex_file = GetDexFile();
564 uint32_t dex_method_idx = method_->GetDexMethodIndex();
565 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
566 return StringPiece("<runtime method>");
567 }
568 const DexFile::MethodId& mid = dex_file.GetMethodId(dex_method_idx);
569 const DexFile::TypeId& type_id = dex_file.GetTypeId(mid.class_idx_);
570 return dex_file.StringDataAsStringPieceByIdx(type_id.descriptor_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800571 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700572
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700573 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
574 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800575 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700576
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700577 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
578 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700579 }
580
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700581 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
582 return GetDexFile().GetClassDef(GetClassDefIndex());
583 }
584
585 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700586 return method_->GetDeclaringClass()->GetClassLoader();
587 }
588
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800589 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800590 return method_->IsStatic();
591 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592
Ian Rogersb726dcb2012-09-05 08:57:23 -0700593 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700594 return IsStatic() && GetNameAsStringPiece() == "<clinit>";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800595 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596
Ian Rogersb726dcb2012-09-05 08:57:23 -0700597 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 // "1 +" because the first in Args is the receiver.
599 // "- 1" because we don't count the return type.
600 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
601 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700602
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800603 // Get the primitive type associated with the given parameter.
604 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800605 CHECK_LT(param, NumArgs());
606 if (IsStatic()) {
607 param++; // 0th argument must skip return value at start of the shorty
608 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800609 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800610 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800611 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800612 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700613
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800614 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
615 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
616 Primitive::Type type = GetParamPrimitiveType(param);
617 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
618 }
619
620 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700621 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800622 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700624
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700626 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700627 const DexFile& dex_file = GetDexFile();
628 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800629 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800630 const DexFile::MethodId& other_mid =
631 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800632 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700634 const DexFile& other_dex_file = other->GetDexFile();
635 const DexFile::MethodId& other_mid =
636 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
637 if (dex_file.StringDataAsStringPieceByIdx(mid.name_idx_) !=
638 other_dex_file.StringDataAsStringPieceByIdx(other_mid.name_idx_)) {
639 return false; // Name mismatch.
640 }
641 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(mid);
642 const DexFile::ProtoId& other_proto_id = other_dex_file.GetMethodPrototype(other_mid);
643 if (dex_file.StringDataAsStringPieceByIdx(proto_id.shorty_idx_) !=
644 other_dex_file.StringDataAsStringPieceByIdx(other_proto_id.shorty_idx_)) {
645 return false; // Shorty mismatch.
646 }
647 const DexFile::TypeId& return_type_id = dex_file.GetTypeId(proto_id.return_type_idx_);
648 const DexFile::TypeId& other_return_type_id =
649 other_dex_file.GetTypeId(other_proto_id.return_type_idx_);
650 if (dex_file.StringDataAsStringPieceByIdx(return_type_id.descriptor_idx_) !=
651 other_dex_file.StringDataAsStringPieceByIdx(other_return_type_id.descriptor_idx_)) {
652 return false; // Return type mismatch.
653 }
654 const DexFile::TypeList* params = dex_file.GetProtoParameters(proto_id);
655 const DexFile::TypeList* other_params = other_dex_file.GetProtoParameters(other_proto_id);
656 if (params == nullptr) {
657 return other_params == nullptr; // Check both lists are empty.
658 }
659 if (other_params == nullptr) {
660 return false; // Parameter list size mismatch.
661 }
662 uint32_t params_size = params->Size();
663 uint32_t other_params_size = other_params->Size();
664 if (params_size != other_params_size) {
665 return false; // Parameter list size mismatch.
666 }
667 for (uint32_t i = 0; i < params_size; ++i) {
668 const DexFile::TypeId& param_id = dex_file.GetTypeId(params->GetTypeItem(i).type_idx_);
669 const DexFile::TypeId& other_param_id =
670 other_dex_file.GetTypeId(other_params->GetTypeItem(i).type_idx_);
671 if (dex_file.StringDataAsStringPieceByIdx(param_id.descriptor_idx_) !=
672 other_dex_file.StringDataAsStringPieceByIdx(other_param_id.descriptor_idx_)) {
673 return false; // Parameter type mismatch.
674 }
675 }
676 return true;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800677 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700678
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700680 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800681 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
682 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700683
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700684 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800686 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
687 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700688
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800689 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700690 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800691 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800692 if (type == NULL) {
693 type = GetClassLinker()->ResolveType(type_idx, method_);
694 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
695 }
696 return type;
697 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700698
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800701 const DexFile& dex_file = GetDexFile();
702 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
703 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700704
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800705 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700706 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700707 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800708 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700709
Ian Rogersb726dcb2012-09-05 08:57:23 -0700710 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800711 const DexFile* result = dex_file_;
712 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800713 const mirror::DexCache* dex_cache = GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700714 result = dex_file_ = dex_cache->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800715 }
716 return *result;
717 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700718
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800719 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
720 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700721 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 mirror::Class* klass = method_->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700723 result = klass->GetDexCache();
724 dex_cache_ = result;
725 }
726 return result;
727 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700728
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800729 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
730 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700731 if (UNLIKELY(s == NULL)) {
732 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, GetDexCache());
733 }
734 return s;
735 }
736
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800737 private:
738 // Set the method_ field, for proxy methods looking up the interface method via the resolved
739 // methods table.
Brian Carlstromea46f952013-07-30 01:26:50 -0700740 void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800741 if (method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800742 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700743 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700744 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800745 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700746 DCHECK(interface_method != NULL);
747 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800748 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800749 }
750 }
751 method_ = method;
752 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700753
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800754 ClassLinker* GetClassLinker() {
755 ClassLinker* result = class_linker_;
756 if (result == NULL) {
757 result = Runtime::Current()->GetClassLinker();
758 class_linker_ = result;
759 }
760 return result;
761 }
762
763 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800764 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800765 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700766 const mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800768 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800769
770 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
771};
772
773} // namespace art
774
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700775#endif // ART_RUNTIME_OBJECT_UTILS_H_