blob: 0dd6ca1134d33aafa393916821e9a1f26fb06210 [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"
Sebastien Hertz80989a62014-04-01 14:39:44 +020028#include "mirror/proxy.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/string.h"
30
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032#include "handle_scope-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033
34#include <string>
35
36namespace art {
37
Mathieu Chartierc528dba2013-11-26 12:00:11 -080038template <typename T>
Ian Rogers672f5202012-01-12 18:06:40 -080039class ObjectLock {
40 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070041 explicit ObjectLock(Thread* self, const Handle<T>* object)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070042 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070043 : self_(self), obj_(object) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080044 CHECK(object != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 CHECK(object->Get() != nullptr);
46 obj_->Get()->MonitorEnter(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080047 }
48
Ian Rogersb726dcb2012-09-05 08:57:23 -070049 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050 obj_->Get()->MonitorExit(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080051 }
52
Ian Rogers05f30572013-02-20 12:13:11 -080053 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070054 Monitor::Wait(self_, obj_->Get(), 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080055 }
56
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070058 obj_->Get()->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080059 }
60
Ian Rogersb726dcb2012-09-05 08:57:23 -070061 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070062 obj_->Get()->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080063 }
64
65 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 Thread* const self_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070067 const Handle<T>* const obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080068 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
69};
70
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080071class ClassHelper {
72 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 explicit ClassHelper(mirror::Class* c )
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 : interface_type_list_(nullptr), klass_(nullptr) {
76 if (c != nullptr) {
Brian Carlstrome77be402012-03-30 01:08:38 -070077 ChangeClass(c);
78 }
Elliott Hughes91250e02011-12-13 22:30:35 -080079 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 void ChangeClass(mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 CHECK(new_c != nullptr) << "klass_=" << klass_; // Log what we were changing from if any
Mathieu Chartier590fee92013-09-13 13:46:47 -070084 if (!new_c->IsClass()) {
85 LOG(FATAL) << "new_c=" << new_c << " cc " << new_c->GetClass() << " ccc "
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 << ((new_c->GetClass() != nullptr) ? new_c->GetClass()->GetClass() : nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 }
88 klass_ = new_c;
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 interface_type_list_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080090 }
91
Elliott Hughes91250e02011-12-13 22:30:35 -080092 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
93 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070094 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080095 CHECK(klass_ != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080096 if (UNLIKELY(klass_->IsArrayClass())) {
97 return GetArrayDescriptor();
98 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080099 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800100 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800101 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
102 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800103 } else {
104 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700105 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 return dex_file.GetTypeDescriptor(type_id);
107 }
108 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800109
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800111 std::string result("[");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800112 mirror::Class* saved_klass = klass_;
113 CHECK(saved_klass != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800114 ChangeClass(klass_->GetComponentType());
115 result += GetDescriptor();
116 ChangeClass(saved_klass);
117 descriptor_ = result;
118 return descriptor_.c_str();
119 }
120
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700121 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 DCHECK(klass_ != nullptr);
123 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
124 if (class_def_idx == DexFile::kDexNoIndex16) {
125 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800129
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 if (klass_->IsPrimitive()) {
133 return 0;
134 } else if (klass_->IsArrayClass()) {
135 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800136 } else if (klass_->IsProxyClass()) {
Sebastien Hertz80989a62014-04-01 14:39:44 +0200137 mirror::SynthesizedProxyClass* proxyClass = reinterpret_cast<mirror::SynthesizedProxyClass*>(klass_);
138 mirror::ObjectArray<mirror::Class>* interfaces = proxyClass->GetInterfaces();
139 return interfaces != nullptr ? interfaces->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800141 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 if (interfaces == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 return 0;
144 } else {
145 return interfaces->Size();
146 }
147 }
148 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800149
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 DCHECK(!klass_->IsPrimitive());
154 DCHECK(!klass_->IsArrayClass());
155 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
156 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800161 DCHECK(!klass_->IsPrimitive());
162 if (klass_->IsArrayClass()) {
163 if (idx == 0) {
Ian Rogers98379392014-02-24 16:53:16 -0800164 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800165 } else {
166 DCHECK_EQ(1U, idx);
Ian Rogers98379392014-02-24 16:53:16 -0800167 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/io/Serializable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800168 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800169 } else if (klass_->IsProxyClass()) {
Sebastien Hertz80989a62014-04-01 14:39:44 +0200170 mirror::SynthesizedProxyClass* proxyClass = reinterpret_cast<mirror::SynthesizedProxyClass*>(klass_);
171 mirror::ObjectArray<mirror::Class>* interfaces = proxyClass->GetInterfaces();
172 DCHECK(interfaces != nullptr);
173 return interfaces->Get(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700175 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177 if (interface == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 CHECK(interface != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 }
181 return interface;
182 }
183 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800184
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700186 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700188 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100189 CHECK(dex_class_def != nullptr) << "No class def for class " << PrettyClass(klass_);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800190 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800192
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 if (dex_cache != nullptr && !klass_->IsProxyClass()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 return dex_cache->GetLocation()->ToModifiedUtf8();
197 } else {
198 // Arrays and proxies are generated and have no corresponding dex file location.
199 return "generated class";
200 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 }
202
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700204 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 }
206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208 return klass_->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700209 }
210
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 const DexFile::TypeList* result = interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 const DexFile::ClassDef* class_def = GetClassDef();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217 if (class_def != nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 result = GetDexFile().GetInterfacesList(*class_def);
219 interface_type_list_ = result;
220 }
221 }
222 return result;
223 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800224
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 ClassLinker* GetClassLinker() ALWAYS_INLINE {
226 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 }
228
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800229 const DexFile::TypeList* interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800230 mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800231 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800232
233 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
234};
235
236class FieldHelper {
237 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800238 FieldHelper() : field_(nullptr) {}
239 explicit FieldHelper(mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240
Ian Rogersef7d42f2014-01-06 12:55:46 -0800241 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
242 DCHECK(new_f != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 field_ = new_f;
244 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700245
Ian Rogersb726dcb2012-09-05 08:57:23 -0700246 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800247 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700248 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800249 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700250 DCHECK_LT(field_index, 2U);
251 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800252 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700253 const DexFile& dex_file = GetDexFile();
254 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800255 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700256
Ian Rogers50239c72013-06-17 14:53:22 -0700257 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800258 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700259 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers98379392014-02-24 16:53:16 -0800260 return GetClassLinker()->FindSystemClass(Thread::Current(), GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700262 const DexFile& dex_file = GetDexFile();
263 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
264 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 if (resolve && (type == nullptr)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700266 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700268 }
269 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700271
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800273 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700274 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800275 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700276 DCHECK_LT(field_index, 2U);
277 // 0 == Class[] interfaces; 1 == Class[][] throws;
278 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800279 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700280 const DexFile& dex_file = GetDexFile();
281 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
282 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700284
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 return Primitive::GetType(GetTypeDescriptor()[0]);
288 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700289
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 Primitive::Type type = GetTypeAsPrimitiveType();
292 return type != Primitive::kPrimNot;
293 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700294
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 Primitive::Type type = GetTypeAsPrimitiveType();
297 return Primitive::FieldSize(type);
298 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800299
300 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
301 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700305 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 DCHECK(field_->IsStatic());
307 DCHECK_LT(field_index, 2U);
308 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800309 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700310 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800311 return declaring_class_descriptor_.c_str();
312 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 const DexFile& dex_file = GetDexFile();
314 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
315 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 }
317
318 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700320 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700322 ClassLinker* GetClassLinker() ALWAYS_INLINE {
323 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700326 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800327 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800329 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800330
331 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
332};
333
334class MethodHelper {
335 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336 MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700337
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338 explicit MethodHelper(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 : method_(nullptr), shorty_(nullptr), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800341 SetMethod(m);
342 }
343
Brian Carlstromea46f952013-07-30 01:26:50 -0700344 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800345 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800346 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800347 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800348 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700349
Ian Rogers53b8b092014-03-13 23:45:53 -0700350 mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700351 return method_;
352 }
353
Ian Rogersb726dcb2012-09-05 08:57:23 -0700354 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800355 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800356 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700357 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800358 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
359 } else {
360 Runtime* runtime = Runtime::Current();
361 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700362 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700363 } else if (method_ == runtime->GetImtConflictMethod()) {
364 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800365 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700366 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800367 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700368 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800369 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700370 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800371 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700372 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800373 }
374 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800375 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700376
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800379 uint32_t dex_method_idx = method_->GetDexMethodIndex();
380 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700381 StackHandleScope<1> hs(Thread::Current());
382 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700383 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385
Jeff Hao9a916d32013-06-27 18:45:37 -0700386 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800388 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800389 const DexFile& dex_file = GetDexFile();
390 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
391 &shorty_len_);
392 shorty_ = result;
393 }
394 return result;
395 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396
Ian Rogersb726dcb2012-09-05 08:57:23 -0700397 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800399 GetShorty();
400 }
401 return shorty_len_;
402 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700404 // Counts the number of references in the parameter list of the corresponding method.
405 // Note: Thus does _not_ include "this" for non-static methods.
406 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
407 const char* shorty = GetShorty();
408 uint32_t refs = 0;
409 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
410 if (shorty[i] == 'L') {
411 refs++;
412 }
413 }
414
415 return refs;
416 }
417
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800420 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700421 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800422 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
423 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700424 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800425 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700428 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 const DexFile& dex_file = GetDexFile();
430 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
431 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700432
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700433 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434 const DexFile::ProtoId& proto = GetPrototype();
435 return GetDexFile().GetProtoParameters(proto);
436 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800438 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 const DexFile& dex_file = GetDexFile();
440 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
441 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
442 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800443 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700445
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700446 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 const DexFile& dex_file = GetDexFile();
448 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
449 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
450 uint16_t return_type_idx = proto_id.return_type_idx_;
451 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
452 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700453
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700454 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700455 if (dex_pc == DexFile::kDexNoIndex) {
456 return method_->IsNative() ? -2 : -1;
457 } else {
458 const DexFile& dex_file = GetDexFile();
459 return dex_file.GetLineNumFromPC(method_, dex_pc);
460 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800461 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700462
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700463 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800464 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700465 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700466 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700467 return "<runtime method>";
468 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700469 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
470 }
471
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700472 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
473 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700476 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
477 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 }
479
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700480 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
481 return GetDexFile().GetClassDef(GetClassDefIndex());
482 }
483
484 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700485 return method_->GetDeclaringClass()->GetClassLoader();
486 }
487
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800488 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800489 return method_->IsStatic();
490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491
Ian Rogersb726dcb2012-09-05 08:57:23 -0700492 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700493 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495
Ian Rogersb726dcb2012-09-05 08:57:23 -0700496 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800497 // "1 +" because the first in Args is the receiver.
498 // "- 1" because we don't count the return type.
499 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
500 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700501
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800502 // Get the primitive type associated with the given parameter.
503 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 CHECK_LT(param, NumArgs());
505 if (IsStatic()) {
506 param++; // 0th argument must skip return value at start of the shorty
507 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800508 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800510 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700512
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800513 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
514 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
515 Primitive::Type type = GetParamPrimitiveType(param);
516 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
517 }
518
519 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700520 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800521 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800522 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700523
Ian Rogers151f2212014-05-06 11:27:27 -0700524 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700525 const DexFile& dex_file = GetDexFile();
526 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 const DexFile::MethodId& other_mid =
529 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800530 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700532 const DexFile& other_dex_file = other->GetDexFile();
533 const DexFile::MethodId& other_mid =
534 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700535 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
536 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700537 return false; // Name mismatch.
538 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700539 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700541
Ian Rogers151f2212014-05-06 11:27:27 -0700542 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
544 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
545 return false;
546 }
547 const DexFile::TypeList* types = GetParameterTypeList();
548 const DexFile::TypeList* other_types = other->GetParameterTypeList();
549 if (types == nullptr) {
550 return (other_types == nullptr) || (other_types->Size() == 0);
551 } else if (UNLIKELY(other_types == nullptr)) {
552 return types->Size() == 0;
553 }
554 uint32_t num_types = types->Size();
555 if (UNLIKELY(num_types != other_types->Size())) {
556 return false;
557 }
558 for (uint32_t i = 0; i < num_types; ++i) {
559 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
560 mirror::Class* other_param_type =
561 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
562 if (UNLIKELY(param_type != other_param_type)) {
563 return false;
564 }
565 }
566 return true;
567 }
568
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800571 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
572 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700574 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800576 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800577 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700578
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800579 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800582 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800583 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800584 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800585 }
586 return type;
587 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700588
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 const DexFile& dex_file = GetDexFile();
592 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
593 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700594
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700596 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700597 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700599
Ian Rogersb726dcb2012-09-05 08:57:23 -0700600 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700601 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800602 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700603
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700605 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700606 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700607
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
609 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800610 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700611 StackHandleScope<1> hs(Thread::Current());
612 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700613 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700614 }
615 return s;
616 }
617
Ian Rogers83883d72013-10-21 21:07:24 -0700618 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
619 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
620 const DexFile& dexfile = GetDexFile();
621 if (&dexfile == &other_dexfile) {
622 return method_->GetDexMethodIndex();
623 }
624 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
625 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
626 const DexFile::StringId* other_descriptor =
627 other_dexfile.FindStringId(mid_declaring_class_descriptor);
628 if (other_descriptor != nullptr) {
629 const DexFile::TypeId* other_type_id =
630 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
631 if (other_type_id != nullptr) {
632 const char* mid_name = dexfile.GetMethodName(mid);
633 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
634 if (other_name != nullptr) {
635 uint16_t other_return_type_idx;
636 std::vector<uint16_t> other_param_type_idxs;
637 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
638 &other_return_type_idx,
639 &other_param_type_idxs);
640 if (success) {
641 const DexFile::ProtoId* other_sig =
642 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
643 if (other_sig != nullptr) {
644 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
645 *other_name,
646 *other_sig);
647 if (other_mid != nullptr) {
648 return other_dexfile.GetIndexForMethodId(*other_mid);
649 }
650 }
651 }
652 }
653 }
654 }
655 return DexFile::kDexNoIndex;
656 }
657
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000658 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
659 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
660 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
661 uint32_t name_and_signature_idx)
662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
663 const DexFile& dexfile = GetDexFile();
664 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
665 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
666 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
667 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
668 if (&dexfile == &other_dexfile) {
669 return method_->GetDexMethodIndex();
670 }
671 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
672 const DexFile::StringId* other_descriptor =
673 other_dexfile.FindStringId(mid_declaring_class_descriptor);
674 if (other_descriptor != nullptr) {
675 const DexFile::TypeId* other_type_id =
676 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
677 if (other_type_id != nullptr) {
678 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
679 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
680 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
681 if (other_mid != nullptr) {
682 return other_dexfile.GetIndexForMethodId(*other_mid);
683 }
684 }
685 }
686 return DexFile::kDexNoIndex;
687 }
688
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689 private:
690 // Set the method_ field, for proxy methods looking up the interface method via the resolved
691 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800692 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
693 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700695 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800697 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800698 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700699 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800700 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800701 }
702 }
703 method_ = method;
704 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700705
Mathieu Chartier590fee92013-09-13 13:46:47 -0700706 ClassLinker* GetClassLinker() ALWAYS_INLINE {
707 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800708 }
709
Ian Rogersef7d42f2014-01-06 12:55:46 -0800710 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800711 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800712 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800713
714 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
715};
716
717} // namespace art
718
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700719#endif // ART_RUNTIME_OBJECT_UTILS_H_