blob: dd2bd4fafe70fd36852d36155c11f038de5394d0 [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 Rogers98379392014-02-24 16:53:16 -080020#include "class_linker.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"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080031#include "sirt_ref-inl.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:
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 explicit ClassHelper(mirror::Class* c )
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 : interface_type_list_(nullptr), klass_(nullptr) {
75 if (c != nullptr) {
Brian Carlstrome77be402012-03-30 01:08:38 -070076 ChangeClass(c);
77 }
Elliott Hughes91250e02011-12-13 22:30:35 -080078 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080079
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 void ChangeClass(mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 CHECK(new_c != nullptr) << "klass_=" << klass_; // Log what we were changing from if any
Mathieu Chartier590fee92013-09-13 13:46:47 -070083 if (!new_c->IsClass()) {
84 LOG(FATAL) << "new_c=" << new_c << " cc " << new_c->GetClass() << " ccc "
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 << ((new_c->GetClass() != nullptr) ? new_c->GetClass()->GetClass() : nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080086 }
87 klass_ = new_c;
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 interface_type_list_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080089 }
90
Elliott Hughes91250e02011-12-13 22:30:35 -080091 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
92 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070093 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 CHECK(klass_ != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080095 if (UNLIKELY(klass_->IsArrayClass())) {
96 return GetArrayDescriptor();
97 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080098 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080099 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800100 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
101 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800102 } else {
103 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700104 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105 return dex_file.GetTypeDescriptor(type_id);
106 }
107 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800108
Ian Rogersb726dcb2012-09-05 08:57:23 -0700109 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800110 std::string result("[");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800111 mirror::Class* saved_klass = klass_;
112 CHECK(saved_klass != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800113 ChangeClass(klass_->GetComponentType());
114 result += GetDescriptor();
115 ChangeClass(saved_klass);
116 descriptor_ = result;
117 return descriptor_.c_str();
118 }
119
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700120 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
121 DCHECK(klass_ != nullptr);
122 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
123 if (class_def_idx == DexFile::kDexNoIndex16) {
124 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800128
Ian Rogersb726dcb2012-09-05 08:57:23 -0700129 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 if (klass_->IsPrimitive()) {
132 return 0;
133 } else if (klass_->IsArrayClass()) {
134 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800135 } else if (klass_->IsProxyClass()) {
136 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 if (interfaces == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 return 0;
141 } else {
142 return interfaces->Size();
143 }
144 }
145 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800146
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800150 DCHECK(!klass_->IsPrimitive());
151 DCHECK(!klass_->IsArrayClass());
152 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
153 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800158 DCHECK(!klass_->IsPrimitive());
159 if (klass_->IsArrayClass()) {
160 if (idx == 0) {
Ian Rogers98379392014-02-24 16:53:16 -0800161 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800162 } else {
163 DCHECK_EQ(1U, idx);
Ian Rogers98379392014-02-24 16:53:16 -0800164 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/io/Serializable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800166 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700167 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800168 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700169 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171 if (interface == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173 CHECK(interface != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 }
175 return interface;
176 }
177 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800178
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700180 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700182 const DexFile::ClassDef* dex_class_def = GetClassDef();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183 CHECK(dex_class_def != nullptr);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800184 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800186
Ian Rogersb726dcb2012-09-05 08:57:23 -0700187 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800189 if (dex_cache != nullptr && !klass_->IsProxyClass()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 return dex_cache->GetLocation()->ToModifiedUtf8();
191 } else {
192 // Arrays and proxies are generated and have no corresponding dex file location.
193 return "generated class";
194 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800195 }
196
Ian Rogersb726dcb2012-09-05 08:57:23 -0700197 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700198 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 }
200
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700202 return klass_->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 }
204
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800208 const DexFile::TypeList* result = interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800209 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 const DexFile::ClassDef* class_def = GetClassDef();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800211 if (class_def != nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 result = GetDexFile().GetInterfacesList(*class_def);
213 interface_type_list_ = result;
214 }
215 }
216 return result;
217 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800218
Mathieu Chartier590fee92013-09-13 13:46:47 -0700219 ClassLinker* GetClassLinker() ALWAYS_INLINE {
220 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 }
222
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 const DexFile::TypeList* interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224 mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800225 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800226
227 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
228};
229
230class FieldHelper {
231 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232 FieldHelper() : field_(nullptr) {}
233 explicit FieldHelper(mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234
Ian Rogersef7d42f2014-01-06 12:55:46 -0800235 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
236 DCHECK(new_f != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 field_ = new_f;
238 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700239
Ian Rogersb726dcb2012-09-05 08:57:23 -0700240 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800241 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700242 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800243 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700244 DCHECK_LT(field_index, 2U);
245 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800246 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700247 const DexFile& dex_file = GetDexFile();
248 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700250
Ian Rogers50239c72013-06-17 14:53:22 -0700251 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800252 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700253 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers98379392014-02-24 16:53:16 -0800254 return GetClassLinker()->FindSystemClass(Thread::Current(), GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700256 const DexFile& dex_file = GetDexFile();
257 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
258 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800259 if (resolve && (type == nullptr)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700260 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800261 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700262 }
263 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700265
Ian Rogersb726dcb2012-09-05 08:57:23 -0700266 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800267 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700268 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800269 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700270 DCHECK_LT(field_index, 2U);
271 // 0 == Class[] interfaces; 1 == Class[][] throws;
272 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800273 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700274 const DexFile& dex_file = GetDexFile();
275 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
276 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700278
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 return Primitive::GetType(GetTypeDescriptor()[0]);
282 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700283
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 Primitive::Type type = GetTypeAsPrimitiveType();
286 return type != Primitive::kPrimNot;
287 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700288
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800290 Primitive::Type type = GetTypeAsPrimitiveType();
291 return Primitive::FieldSize(type);
292 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800293
294 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
295 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700298 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700299 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700300 DCHECK(field_->IsStatic());
301 DCHECK_LT(field_index, 2U);
302 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800303 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700304 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800305 return declaring_class_descriptor_.c_str();
306 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700307 const DexFile& dex_file = GetDexFile();
308 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
309 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 }
311
312 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700314 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700316 ClassLinker* GetClassLinker() ALWAYS_INLINE {
317 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700319 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700320 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800322 mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800323 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324
325 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
326};
327
328class MethodHelper {
329 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330 MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700331
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 explicit MethodHelper(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 : method_(nullptr), shorty_(nullptr), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800335 SetMethod(m);
336 }
337
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800342 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700343
Brian Carlstromea46f952013-07-30 01:26:50 -0700344 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700345 return method_;
346 }
347
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800349 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800350 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700351 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800352 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
353 } else {
354 Runtime* runtime = Runtime::Current();
355 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700356 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700357 } else if (method_ == runtime->GetImtConflictMethod()) {
358 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800359 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700360 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800361 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700362 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800363 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700364 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800365 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700366 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800367 }
368 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800373 uint32_t dex_method_idx = method_->GetDexMethodIndex();
374 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
376 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800377 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700378
Jeff Hao9a916d32013-06-27 18:45:37 -0700379 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800380 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800381 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 const DexFile& dex_file = GetDexFile();
383 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
384 &shorty_len_);
385 shorty_ = result;
386 }
387 return result;
388 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700389
Ian Rogersb726dcb2012-09-05 08:57:23 -0700390 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800392 GetShorty();
393 }
394 return shorty_len_;
395 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396
Ian Rogersd91d6d62013-09-25 20:26:14 -0700397 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800399 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700400 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800401 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
402 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700403 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800404 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800405 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700406
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700407 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 const DexFile& dex_file = GetDexFile();
409 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
410 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700411
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700412 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800413 const DexFile::ProtoId& proto = GetPrototype();
414 return GetDexFile().GetProtoParameters(proto);
415 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700416
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800417 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 const DexFile& dex_file = GetDexFile();
419 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
420 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
421 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800422 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700425 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 const DexFile& dex_file = GetDexFile();
427 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
428 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
429 uint16_t return_type_idx = proto_id.return_type_idx_;
430 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700432
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700433 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700434 if (dex_pc == DexFile::kDexNoIndex) {
435 return method_->IsNative() ? -2 : -1;
436 } else {
437 const DexFile& dex_file = GetDexFile();
438 return dex_file.GetLineNumFromPC(method_, dex_pc);
439 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700441
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700442 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700444 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700445 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700446 return "<runtime method>";
447 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700448 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
449 }
450
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700451 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
452 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800453 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700455 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
456 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 }
458
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700459 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
460 return GetDexFile().GetClassDef(GetClassDefIndex());
461 }
462
463 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 return method_->GetDeclaringClass()->GetClassLoader();
465 }
466
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800467 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468 return method_->IsStatic();
469 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700472 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700474
Ian Rogersb726dcb2012-09-05 08:57:23 -0700475 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 // "1 +" because the first in Args is the receiver.
477 // "- 1" because we don't count the return type.
478 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
479 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700480
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800481 // Get the primitive type associated with the given parameter.
482 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800483 CHECK_LT(param, NumArgs());
484 if (IsStatic()) {
485 param++; // 0th argument must skip return value at start of the shorty
486 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800487 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800488 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800489 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800492 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
493 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
494 Primitive::Type type = GetParamPrimitiveType(param);
495 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
496 }
497
498 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800500 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800501 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700502
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700504 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700505 const DexFile& dex_file = GetDexFile();
506 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 const DexFile::MethodId& other_mid =
509 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800510 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700512 const DexFile& other_dex_file = other->GetDexFile();
513 const DexFile::MethodId& other_mid =
514 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700515 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
516 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700517 return false; // Name mismatch.
518 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700519 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700521
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700523 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
525 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700527 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800530 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700531
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800532 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800535 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 }
539 return type;
540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700541
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800544 const DexFile& dex_file = GetDexFile();
545 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
546 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700547
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700549 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700550 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800551 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552
Ian Rogersb726dcb2012-09-05 08:57:23 -0700553 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700554 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700558 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700560
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
562 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 if (UNLIKELY(s == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700564 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
565 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700566 }
567 return s;
568 }
569
Ian Rogers83883d72013-10-21 21:07:24 -0700570 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
572 const DexFile& dexfile = GetDexFile();
573 if (&dexfile == &other_dexfile) {
574 return method_->GetDexMethodIndex();
575 }
576 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
577 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
578 const DexFile::StringId* other_descriptor =
579 other_dexfile.FindStringId(mid_declaring_class_descriptor);
580 if (other_descriptor != nullptr) {
581 const DexFile::TypeId* other_type_id =
582 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
583 if (other_type_id != nullptr) {
584 const char* mid_name = dexfile.GetMethodName(mid);
585 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
586 if (other_name != nullptr) {
587 uint16_t other_return_type_idx;
588 std::vector<uint16_t> other_param_type_idxs;
589 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
590 &other_return_type_idx,
591 &other_param_type_idxs);
592 if (success) {
593 const DexFile::ProtoId* other_sig =
594 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
595 if (other_sig != nullptr) {
596 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
597 *other_name,
598 *other_sig);
599 if (other_mid != nullptr) {
600 return other_dexfile.GetIndexForMethodId(*other_mid);
601 }
602 }
603 }
604 }
605 }
606 }
607 return DexFile::kDexNoIndex;
608 }
609
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000610 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
611 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
612 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
613 uint32_t name_and_signature_idx)
614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
615 const DexFile& dexfile = GetDexFile();
616 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
617 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
618 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
619 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
620 if (&dexfile == &other_dexfile) {
621 return method_->GetDexMethodIndex();
622 }
623 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
624 const DexFile::StringId* other_descriptor =
625 other_dexfile.FindStringId(mid_declaring_class_descriptor);
626 if (other_descriptor != nullptr) {
627 const DexFile::TypeId* other_type_id =
628 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
629 if (other_type_id != nullptr) {
630 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
631 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
632 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
633 if (other_mid != nullptr) {
634 return other_dexfile.GetIndexForMethodId(*other_mid);
635 }
636 }
637 }
638 return DexFile::kDexNoIndex;
639 }
640
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800641 private:
642 // Set the method_ field, for proxy methods looking up the interface method via the resolved
643 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800644 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
645 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800646 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700647 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700648 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800649 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800650 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700651 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800652 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800653 }
654 }
655 method_ = method;
656 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700657
Mathieu Chartier590fee92013-09-13 13:46:47 -0700658 ClassLinker* GetClassLinker() ALWAYS_INLINE {
659 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800660 }
661
Ian Rogersef7d42f2014-01-06 12:55:46 -0800662 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800663 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800664 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800665
666 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
667};
668
669} // namespace art
670
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700671#endif // ART_RUNTIME_OBJECT_UTILS_H_