blob: 63801d3a3f815bb989981738bb7a98dc0a00f597 [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()) {
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100136 return klass_->GetIfTable()->Count();
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();
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100183 CHECK(dex_class_def != nullptr) << "No class def for class " << PrettyClass(klass_);
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
Ian Rogers53b8b092014-03-13 23:45:53 -0700344 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
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700397 // Counts the number of references in the parameter list of the corresponding method.
398 // Note: Thus does _not_ include "this" for non-static methods.
399 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
400 const char* shorty = GetShorty();
401 uint32_t refs = 0;
402 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
403 if (shorty[i] == 'L') {
404 refs++;
405 }
406 }
407
408 return refs;
409 }
410
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800413 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700414 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800415 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
416 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700417 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800418 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700421 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 const DexFile& dex_file = GetDexFile();
423 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700426 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800427 const DexFile::ProtoId& proto = GetPrototype();
428 return GetDexFile().GetProtoParameters(proto);
429 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800431 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432 const DexFile& dex_file = GetDexFile();
433 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
434 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
435 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800436 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700439 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 const DexFile& dex_file = GetDexFile();
441 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
442 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
443 uint16_t return_type_idx = proto_id.return_type_idx_;
444 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700447 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700448 if (dex_pc == DexFile::kDexNoIndex) {
449 return method_->IsNative() ? -2 : -1;
450 } else {
451 const DexFile& dex_file = GetDexFile();
452 return dex_file.GetLineNumFromPC(method_, dex_pc);
453 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700456 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800457 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700458 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700459 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700460 return "<runtime method>";
461 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700462 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
463 }
464
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700465 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
466 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800467 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700468
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700469 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
470 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 }
472
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700473 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
474 return GetDexFile().GetClassDef(GetClassDefIndex());
475 }
476
477 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 return method_->GetDeclaringClass()->GetClassLoader();
479 }
480
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800481 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800482 return method_->IsStatic();
483 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700486 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700488
Ian Rogersb726dcb2012-09-05 08:57:23 -0700489 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800490 // "1 +" because the first in Args is the receiver.
491 // "- 1" because we don't count the return type.
492 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
493 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800495 // Get the primitive type associated with the given parameter.
496 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800497 CHECK_LT(param, NumArgs());
498 if (IsStatic()) {
499 param++; // 0th argument must skip return value at start of the shorty
500 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800501 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800502 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800503 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800506 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
507 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
508 Primitive::Type type = GetParamPrimitiveType(param);
509 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
510 }
511
512 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700513 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800514 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800515 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700516
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700517 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700519 const DexFile& dex_file = GetDexFile();
520 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 const DexFile::MethodId& other_mid =
523 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800524 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700526 const DexFile& other_dex_file = other->GetDexFile();
527 const DexFile::MethodId& other_mid =
528 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700529 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
530 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700531 return false; // Name mismatch.
532 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700533 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800534 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700535
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700537 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
539 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700540
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800543 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800544 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800546 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800549 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800551 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 }
553 return type;
554 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700555
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700556 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700557 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800558 const DexFile& dex_file = GetDexFile();
559 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
560 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700563 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700564 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800565 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700568 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800569 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700570
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700572 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700574
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
576 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800577 if (UNLIKELY(s == nullptr)) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700578 SirtRef<mirror::DexCache> dex_cache(Thread::Current(), GetDexCache());
579 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700580 }
581 return s;
582 }
583
Ian Rogers83883d72013-10-21 21:07:24 -0700584 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
586 const DexFile& dexfile = GetDexFile();
587 if (&dexfile == &other_dexfile) {
588 return method_->GetDexMethodIndex();
589 }
590 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
591 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
592 const DexFile::StringId* other_descriptor =
593 other_dexfile.FindStringId(mid_declaring_class_descriptor);
594 if (other_descriptor != nullptr) {
595 const DexFile::TypeId* other_type_id =
596 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
597 if (other_type_id != nullptr) {
598 const char* mid_name = dexfile.GetMethodName(mid);
599 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
600 if (other_name != nullptr) {
601 uint16_t other_return_type_idx;
602 std::vector<uint16_t> other_param_type_idxs;
603 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
604 &other_return_type_idx,
605 &other_param_type_idxs);
606 if (success) {
607 const DexFile::ProtoId* other_sig =
608 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
609 if (other_sig != nullptr) {
610 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
611 *other_name,
612 *other_sig);
613 if (other_mid != nullptr) {
614 return other_dexfile.GetIndexForMethodId(*other_mid);
615 }
616 }
617 }
618 }
619 }
620 }
621 return DexFile::kDexNoIndex;
622 }
623
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000624 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
625 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
626 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
627 uint32_t name_and_signature_idx)
628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
629 const DexFile& dexfile = GetDexFile();
630 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
631 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
632 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
633 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
634 if (&dexfile == &other_dexfile) {
635 return method_->GetDexMethodIndex();
636 }
637 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
638 const DexFile::StringId* other_descriptor =
639 other_dexfile.FindStringId(mid_declaring_class_descriptor);
640 if (other_descriptor != nullptr) {
641 const DexFile::TypeId* other_type_id =
642 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
643 if (other_type_id != nullptr) {
644 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
645 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
646 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
647 if (other_mid != nullptr) {
648 return other_dexfile.GetIndexForMethodId(*other_mid);
649 }
650 }
651 }
652 return DexFile::kDexNoIndex;
653 }
654
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800655 private:
656 // Set the method_ field, for proxy methods looking up the interface method via the resolved
657 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800658 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
659 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700661 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800663 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800664 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700665 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800666 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800667 }
668 }
669 method_ = method;
670 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700671
Mathieu Chartier590fee92013-09-13 13:46:47 -0700672 ClassLinker* GetClassLinker() ALWAYS_INLINE {
673 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800674 }
675
Ian Rogersef7d42f2014-01-06 12:55:46 -0800676 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800677 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800678 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800679
680 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
681};
682
683} // namespace art
684
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700685#endif // ART_RUNTIME_OBJECT_UTILS_H_