blob: b1e8c099b7024ab8db16cee4984459e4fa581413 [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 Chartierdb2633c2014-05-16 09:59:29 -070041 ObjectLock(Thread* self, Handle<T> object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070042 : self_(self), obj_(object) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070043 CHECK(object.Get() != nullptr);
44 obj_->MonitorEnter(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080045 }
46
Ian Rogersb726dcb2012-09-05 08:57:23 -070047 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070048 obj_->MonitorExit(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080049 }
50
Ian Rogers05f30572013-02-20 12:13:11 -080051 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070052 Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080053 }
54
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070056 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080057 }
58
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070060 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080061 }
62
63 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 Thread* const self_;
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070065 Handle<T> const obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080066 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
67};
68
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080069class ClassHelper {
70 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 explicit ClassHelper(mirror::Class* c )
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 : interface_type_list_(nullptr), klass_(nullptr) {
74 if (c != nullptr) {
Brian Carlstrome77be402012-03-30 01:08:38 -070075 ChangeClass(c);
76 }
Elliott Hughes91250e02011-12-13 22:30:35 -080077 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 void ChangeClass(mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 CHECK(new_c != nullptr) << "klass_=" << klass_; // Log what we were changing from if any
Mathieu Chartier590fee92013-09-13 13:46:47 -070082 if (!new_c->IsClass()) {
83 LOG(FATAL) << "new_c=" << new_c << " cc " << new_c->GetClass() << " ccc "
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 << ((new_c->GetClass() != nullptr) ? new_c->GetClass()->GetClass() : nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080085 }
86 klass_ = new_c;
Ian Rogersef7d42f2014-01-06 12:55:46 -080087 interface_type_list_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 }
89
Elliott Hughes91250e02011-12-13 22:30:35 -080090 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
91 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070092 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 CHECK(klass_ != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080094 if (UNLIKELY(klass_->IsArrayClass())) {
95 return GetArrayDescriptor();
96 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080097 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080098 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080099 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
100 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101 } else {
102 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700103 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800104 return dex_file.GetTypeDescriptor(type_id);
105 }
106 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800107
Ian Rogersb726dcb2012-09-05 08:57:23 -0700108 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800109 std::string result("[");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110 mirror::Class* saved_klass = klass_;
111 CHECK(saved_klass != nullptr);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800112 ChangeClass(klass_->GetComponentType());
113 result += GetDescriptor();
114 ChangeClass(saved_klass);
115 descriptor_ = result;
116 return descriptor_.c_str();
117 }
118
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700119 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
120 DCHECK(klass_ != nullptr);
121 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
122 if (class_def_idx == DexFile::kDexNoIndex16) {
123 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700125 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800127
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 if (klass_->IsPrimitive()) {
131 return 0;
132 } else if (klass_->IsArrayClass()) {
133 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800134 } else if (klass_->IsProxyClass()) {
Sebastien Hertz80989a62014-04-01 14:39:44 +0200135 mirror::SynthesizedProxyClass* proxyClass = reinterpret_cast<mirror::SynthesizedProxyClass*>(klass_);
136 mirror::ObjectArray<mirror::Class>* interfaces = proxyClass->GetInterfaces();
137 return interfaces != nullptr ? interfaces->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800139 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 if (interfaces == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800141 return 0;
142 } else {
143 return interfaces->Size();
144 }
145 }
146 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800147
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800150 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800151 DCHECK(!klass_->IsPrimitive());
152 DCHECK(!klass_->IsArrayClass());
153 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
154 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800155
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800158 DCHECK(klass_ != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 DCHECK(!klass_->IsPrimitive());
160 if (klass_->IsArrayClass()) {
161 if (idx == 0) {
Ian Rogers98379392014-02-24 16:53:16 -0800162 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/lang/Cloneable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 } else {
164 DCHECK_EQ(1U, idx);
Ian Rogers98379392014-02-24 16:53:16 -0800165 return GetClassLinker()->FindSystemClass(Thread::Current(), "Ljava/io/Serializable;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800167 } else if (klass_->IsProxyClass()) {
Sebastien Hertz80989a62014-04-01 14:39:44 +0200168 mirror::SynthesizedProxyClass* proxyClass = reinterpret_cast<mirror::SynthesizedProxyClass*>(klass_);
169 mirror::ObjectArray<mirror::Class>* interfaces = proxyClass->GetInterfaces();
170 DCHECK(interfaces != nullptr);
171 return interfaces->Get(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700173 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800175 if (interface == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177 CHECK(interface != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 }
179 return interface;
180 }
181 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800182
Ian Rogersb726dcb2012-09-05 08:57:23 -0700183 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700184 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700186 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertzb7054ba2014-03-13 11:52:31 +0100187 CHECK(dex_class_def != nullptr) << "No class def for class " << PrettyClass(klass_);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800188 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800190
Ian Rogersb726dcb2012-09-05 08:57:23 -0700191 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193 if (dex_cache != nullptr && !klass_->IsProxyClass()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 return dex_cache->GetLocation()->ToModifiedUtf8();
195 } else {
196 // Arrays and proxies are generated and have no corresponding dex file location.
197 return "generated class";
198 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 }
200
Ian Rogersb726dcb2012-09-05 08:57:23 -0700201 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700202 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 }
204
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206 return klass_->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
208
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700210 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 const DexFile::TypeList* result = interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 const DexFile::ClassDef* class_def = GetClassDef();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800215 if (class_def != nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 result = GetDexFile().GetInterfacesList(*class_def);
217 interface_type_list_ = result;
218 }
219 }
220 return result;
221 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800222
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 ClassLinker* GetClassLinker() ALWAYS_INLINE {
224 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 }
226
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 const DexFile::TypeList* interface_type_list_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228 mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800229 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230
231 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
232};
233
234class FieldHelper {
235 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800236 FieldHelper() : field_(nullptr) {}
237 explicit FieldHelper(mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238
Ian Rogersef7d42f2014-01-06 12:55:46 -0800239 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
240 DCHECK(new_f != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 field_ = new_f;
242 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700243
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800245 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700246 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800247 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700248 DCHECK_LT(field_index, 2U);
249 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800250 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700251 const DexFile& dex_file = GetDexFile();
252 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800253 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700254
Ian Rogers50239c72013-06-17 14:53:22 -0700255 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800256 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700257 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers98379392014-02-24 16:53:16 -0800258 return GetClassLinker()->FindSystemClass(Thread::Current(), GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700260 const DexFile& dex_file = GetDexFile();
261 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
262 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263 if (resolve && (type == nullptr)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700264 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700266 }
267 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700269
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800271 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700272 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800273 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700274 DCHECK_LT(field_index, 2U);
275 // 0 == Class[] interfaces; 1 == Class[][] throws;
276 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800277 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700278 const DexFile& dex_file = GetDexFile();
279 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
280 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700282
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 return Primitive::GetType(GetTypeDescriptor()[0]);
286 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700287
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 Primitive::Type type = GetTypeAsPrimitiveType();
290 return type != Primitive::kPrimNot;
291 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700292
Ian Rogersb726dcb2012-09-05 08:57:23 -0700293 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 Primitive::Type type = GetTypeAsPrimitiveType();
295 return Primitive::FieldSize(type);
296 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800297
298 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
299 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700300 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700302 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700303 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 DCHECK(field_->IsStatic());
305 DCHECK_LT(field_index, 2U);
306 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800307 ClassHelper kh(field_->GetDeclaringClass());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700308 declaring_class_descriptor_ = kh.GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800309 return declaring_class_descriptor_.c_str();
310 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700311 const DexFile& dex_file = GetDexFile();
312 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
313 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 }
315
316 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700318 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700320 ClassLinker* GetClassLinker() ALWAYS_INLINE {
321 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700324 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326 mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800327 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800328
329 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
330};
331
332class MethodHelper {
333 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334 MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336 explicit MethodHelper(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338 : method_(nullptr), shorty_(nullptr), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800339 SetMethod(m);
340 }
341
Brian Carlstromea46f952013-07-30 01:26:50 -0700342 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800344 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800345 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800346 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700347
Ian Rogers53b8b092014-03-13 23:45:53 -0700348 mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700349 return method_;
350 }
351
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800353 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800354 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700355 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800356 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
357 } else {
358 Runtime* runtime = Runtime::Current();
359 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700360 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700361 } else if (method_ == runtime->GetImtConflictMethod()) {
362 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800363 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700364 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800365 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700366 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800367 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700368 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800369 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700370 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800371 }
372 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700374
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800376 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800377 uint32_t dex_method_idx = method_->GetDexMethodIndex();
378 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700379 StackHandleScope<1> hs(Thread::Current());
380 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700383
Jeff Hao9a916d32013-06-27 18:45:37 -0700384 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800385 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800386 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800387 const DexFile& dex_file = GetDexFile();
388 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
389 &shorty_len_);
390 shorty_ = result;
391 }
392 return result;
393 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700394
Ian Rogersb726dcb2012-09-05 08:57:23 -0700395 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800396 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800397 GetShorty();
398 }
399 return shorty_len_;
400 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700402 // Counts the number of references in the parameter list of the corresponding method.
403 // Note: Thus does _not_ include "this" for non-static methods.
404 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
405 const char* shorty = GetShorty();
406 uint32_t refs = 0;
407 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
408 if (shorty[i] == 'L') {
409 refs++;
410 }
411 }
412
413 return refs;
414 }
415
Ian Rogersd91d6d62013-09-25 20:26:14 -0700416 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800417 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800418 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700419 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800420 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
421 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700422 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800423 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700426 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800427 const DexFile& dex_file = GetDexFile();
428 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
429 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700431 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800432 const DexFile::ProtoId& proto = GetPrototype();
433 return GetDexFile().GetProtoParameters(proto);
434 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700435
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800436 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 const DexFile& dex_file = GetDexFile();
438 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
439 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
440 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800441 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700443
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700444 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 const DexFile& dex_file = GetDexFile();
446 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
447 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
448 uint16_t return_type_idx = proto_id.return_type_idx_;
449 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
450 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700452 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700453 if (dex_pc == DexFile::kDexNoIndex) {
454 return method_->IsNative() ? -2 : -1;
455 } else {
456 const DexFile& dex_file = GetDexFile();
457 return dex_file.GetLineNumFromPC(method_, dex_pc);
458 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800459 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700461 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800462 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700463 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700464 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700465 return "<runtime method>";
466 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700467 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
468 }
469
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700470 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
471 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800472 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700473
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700474 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
475 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 }
477
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700478 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
479 return GetDexFile().GetClassDef(GetClassDefIndex());
480 }
481
482 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 return method_->GetDeclaringClass()->GetClassLoader();
484 }
485
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800486 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 return method_->IsStatic();
488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700491 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800495 // "1 +" because the first in Args is the receiver.
496 // "- 1" because we don't count the return type.
497 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
498 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800500 // Get the primitive type associated with the given parameter.
501 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800502 CHECK_LT(param, NumArgs());
503 if (IsStatic()) {
504 param++; // 0th argument must skip return value at start of the shorty
505 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800506 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800508 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700510
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800511 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
512 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
513 Primitive::Type type = GetParamPrimitiveType(param);
514 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
515 }
516
517 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700518 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800519 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700521
Ian Rogers151f2212014-05-06 11:27:27 -0700522 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700523 const DexFile& dex_file = GetDexFile();
524 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800525 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800526 const DexFile::MethodId& other_mid =
527 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800528 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800529 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700530 const DexFile& other_dex_file = other->GetDexFile();
531 const DexFile::MethodId& other_mid =
532 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700533 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
534 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700535 return false; // Name mismatch.
536 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700537 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700539
Ian Rogers151f2212014-05-06 11:27:27 -0700540 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
542 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
543 return false;
544 }
545 const DexFile::TypeList* types = GetParameterTypeList();
546 const DexFile::TypeList* other_types = other->GetParameterTypeList();
547 if (types == nullptr) {
548 return (other_types == nullptr) || (other_types->Size() == 0);
549 } else if (UNLIKELY(other_types == nullptr)) {
550 return types->Size() == 0;
551 }
552 uint32_t num_types = types->Size();
553 if (UNLIKELY(num_types != other_types->Size())) {
554 return false;
555 }
556 for (uint32_t i = 0; i < num_types; ++i) {
557 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
558 mirror::Class* other_param_type =
559 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
560 if (UNLIKELY(param_type != other_param_type)) {
561 return false;
562 }
563 }
564 return true;
565 }
566
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700567 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700568 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800569 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
570 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700571
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700572 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800574 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800575 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700576
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800577 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800580 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800581 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800582 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800583 }
584 return type;
585 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700586
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700587 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800589 const DexFile& dex_file = GetDexFile();
590 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
591 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700592
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700594 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700595 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800596 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700597
Ian Rogersb726dcb2012-09-05 08:57:23 -0700598 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700599 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800600 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700601
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700603 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700604 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700605
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
607 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800608 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700609 StackHandleScope<1> hs(Thread::Current());
610 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700611 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700612 }
613 return s;
614 }
615
Ian Rogers83883d72013-10-21 21:07:24 -0700616 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
617 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
618 const DexFile& dexfile = GetDexFile();
619 if (&dexfile == &other_dexfile) {
620 return method_->GetDexMethodIndex();
621 }
622 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
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 char* mid_name = dexfile.GetMethodName(mid);
631 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
632 if (other_name != nullptr) {
633 uint16_t other_return_type_idx;
634 std::vector<uint16_t> other_param_type_idxs;
635 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
636 &other_return_type_idx,
637 &other_param_type_idxs);
638 if (success) {
639 const DexFile::ProtoId* other_sig =
640 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
641 if (other_sig != nullptr) {
642 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
643 *other_name,
644 *other_sig);
645 if (other_mid != nullptr) {
646 return other_dexfile.GetIndexForMethodId(*other_mid);
647 }
648 }
649 }
650 }
651 }
652 }
653 return DexFile::kDexNoIndex;
654 }
655
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000656 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
657 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
658 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
659 uint32_t name_and_signature_idx)
660 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
661 const DexFile& dexfile = GetDexFile();
662 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
663 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
664 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
665 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
666 if (&dexfile == &other_dexfile) {
667 return method_->GetDexMethodIndex();
668 }
669 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
670 const DexFile::StringId* other_descriptor =
671 other_dexfile.FindStringId(mid_declaring_class_descriptor);
672 if (other_descriptor != nullptr) {
673 const DexFile::TypeId* other_type_id =
674 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
675 if (other_type_id != nullptr) {
676 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
677 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
678 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
679 if (other_mid != nullptr) {
680 return other_dexfile.GetIndexForMethodId(*other_mid);
681 }
682 }
683 }
684 return DexFile::kDexNoIndex;
685 }
686
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800687 private:
688 // Set the method_ field, for proxy methods looking up the interface method via the resolved
689 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800690 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
691 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800692 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700693 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700694 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800695 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800696 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700697 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800698 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800699 }
700 }
701 method_ = method;
702 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700703
Mathieu Chartier590fee92013-09-13 13:46:47 -0700704 ClassLinker* GetClassLinker() ALWAYS_INLINE {
705 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800706 }
707
Ian Rogersef7d42f2014-01-06 12:55:46 -0800708 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800709 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800710 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800711
712 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
713};
714
715} // namespace art
716
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700717#endif // ART_RUNTIME_OBJECT_UTILS_H_