blob: 6ee3016179f35cbda5e14f11ac515379074c864d [file] [log] [blame]
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_OBJECT_UTILS_H_
18#define ART_RUNTIME_OBJECT_UTILS_H_
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080019
Ian Rogersd8274bc2013-05-15 15:54:45 -070020#include "class_linker-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "dex_file.h"
Ian Rogers672f5202012-01-12 18:06:40 -080022#include "monitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.h"
24#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
26#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/iftable.h"
28#include "mirror/string.h"
29
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "runtime.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070031#include "sirt_ref.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032
33#include <string>
34
35namespace art {
36
Ian Rogers672f5202012-01-12 18:06:40 -080037class ObjectLock {
38 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039 explicit ObjectLock(Thread* self, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070040 : self_(self), obj_(object) {
Ian Rogers672f5202012-01-12 18:06:40 -080041 CHECK(object != NULL);
42 obj_->MonitorEnter(self_);
43 }
44
Ian Rogersb726dcb2012-09-05 08:57:23 -070045 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080046 obj_->MonitorExit(self_);
47 }
48
Ian Rogers05f30572013-02-20 12:13:11 -080049 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
50 Monitor::Wait(self_, obj_, 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080051 }
52
Ian Rogersb726dcb2012-09-05 08:57:23 -070053 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080054 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080055 }
56
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers05f30572013-02-20 12:13:11 -080058 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080059 }
60
61 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 Thread* const self_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Object* obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080064 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
65};
66
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067class ClassHelper {
68 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 ClassHelper(const mirror::Class* c = NULL, ClassLinker* l = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers8b2c0b92013-09-19 02:56:49 -070071 : class_linker_(l),
Elliott Hughes91250e02011-12-13 22:30:35 -080072 dex_cache_(NULL),
73 dex_file_(NULL),
74 interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070075 klass_(NULL) {
76 if (c != NULL) {
77 ChangeClass(c);
78 }
Elliott Hughes91250e02011-12-13 22:30:35 -080079 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 void ChangeClass(const mirror::Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070083 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
84 CHECK(new_c->IsClass()) << "new_c=" << new_c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080085 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 mirror::DexCache* new_c_dex_cache = new_c->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 if (new_c_dex_cache != dex_cache_) {
88 dex_cache_ = new_c_dex_cache;
89 dex_file_ = NULL;
90 }
91 }
92 klass_ = new_c;
93 interface_type_list_ = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080094 }
95
Elliott Hughes91250e02011-12-13 22:30:35 -080096 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
97 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070098 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070099 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800100 if (UNLIKELY(klass_->IsArrayClass())) {
101 return GetArrayDescriptor();
102 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800103 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800104 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800105 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
106 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800107 } else {
108 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700109 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110 return dex_file.GetTypeDescriptor(type_id);
111 }
112 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800113
Ian Rogersb726dcb2012-09-05 08:57:23 -0700114 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800115 std::string result("[");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 const mirror::Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700117 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800118 ChangeClass(klass_->GetComponentType());
119 result += GetDescriptor();
120 ChangeClass(saved_klass);
121 descriptor_ = result;
122 return descriptor_.c_str();
123 }
124
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700125 const DexFile::ClassDef* GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126 DCHECK(klass_ != nullptr);
127 uint16_t class_def_idx = klass_->GetDexClassDefIndex();
128 if (class_def_idx == DexFile::kDexNoIndex16) {
129 return nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 return &GetDexFile().GetClassDef(class_def_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800133
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 uint32_t NumDirectInterfaces() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700135 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800136 if (klass_->IsPrimitive()) {
137 return 0;
138 } else if (klass_->IsArrayClass()) {
139 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800140 } else if (klass_->IsProxyClass()) {
141 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800142 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
144 if (interfaces == NULL) {
145 return 0;
146 } else {
147 return interfaces->Size();
148 }
149 }
150 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800151
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 uint16_t GetDirectInterfaceTypeIdx(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700154 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800155 DCHECK(!klass_->IsPrimitive());
156 DCHECK(!klass_->IsArrayClass());
157 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
158 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800159
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 mirror::Class* GetDirectInterface(uint32_t idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700162 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 DCHECK(!klass_->IsPrimitive());
164 if (klass_->IsArrayClass()) {
165 if (idx == 0) {
166 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
167 } else {
168 DCHECK_EQ(1U, idx);
169 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
170 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800171 } else if (klass_->IsProxyClass()) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700172 return klass_->GetIfTable()->GetInterface(idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800173 } else {
Ian Rogersd24e2642012-06-06 21:21:43 -0700174 uint16_t type_idx = GetDirectInterfaceTypeIdx(idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::Class* interface = GetDexCache()->GetResolvedType(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 if (interface == NULL) {
177 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
178 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
179 }
180 return interface;
181 }
182 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800183
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 const char* GetSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes95572412011-12-13 18:14:20 -0800185 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700187 const DexFile::ClassDef* dex_class_def = GetClassDef();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800188 CHECK(dex_class_def != NULL);
189 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800191
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 std::string GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 mirror::DexCache* dex_cache = GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 if (dex_cache != NULL && !klass_->IsProxyClass()) {
195 return dex_cache->GetLocation()->ToModifiedUtf8();
196 } else {
197 // Arrays and proxies are generated and have no corresponding dex file location.
198 return "generated class";
199 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 }
201
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700203 if (dex_file_ == NULL) {
204 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700206 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 }
208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
210 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 if (result == NULL) {
212 DCHECK(klass_ != NULL);
213 result = klass_->GetDexCache();
214 dex_cache_ = result;
215 }
216 return result;
217 }
218
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800219 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 const DexFile::TypeList* GetInterfaceTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 const DexFile::TypeList* result = interface_type_list_;
223 if (result == NULL) {
224 const DexFile::ClassDef* class_def = GetClassDef();
225 if (class_def != NULL) {
226 result = GetDexFile().GetInterfacesList(*class_def);
227 interface_type_list_ = result;
228 }
229 }
230 return result;
231 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800232
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 ClassLinker* GetClassLinker() {
234 ClassLinker* result = class_linker_;
235 if (result == NULL) {
236 result = Runtime::Current()->GetClassLinker();
237 class_linker_ = result;
238 }
239 return result;
240 }
241
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 const DexFile* dex_file_;
245 const DexFile::TypeList* interface_type_list_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 const mirror::Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800247 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800248
249 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
250};
251
252class FieldHelper {
253 public:
254 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Brian Carlstromea46f952013-07-30 01:26:50 -0700255 explicit FieldHelper(const mirror::ArtField* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
256 FieldHelper(const mirror::ArtField* f, ClassLinker* l)
Ian Rogersca190662012-06-26 15:45:57 -0700257 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258
Brian Carlstromea46f952013-07-30 01:26:50 -0700259 void ChangeField(const mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 DCHECK(new_f != NULL);
261 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 mirror::DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800263 if (new_f_dex_cache != dex_cache_) {
264 dex_cache_ = new_f_dex_cache;
265 dex_file_ = NULL;
266 }
267 }
268 field_ = new_f;
269 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800271 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700272 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800273 const DexFile& dex_file = GetDexFile();
274 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
275 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800276 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700277 DCHECK_LT(field_index, 2U);
278 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800279 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800280 }
Ian Rogers50239c72013-06-17 14:53:22 -0700281 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800282 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700283 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800284 const DexFile& dex_file = GetDexFile();
285 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogers50239c72013-06-17 14:53:22 -0700287 if (resolve && (type == NULL)) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800288 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
289 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
290 }
291 return type;
292 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700293 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800297 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700298 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800299 const DexFile& dex_file = GetDexFile();
300 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
301 return dex_file.GetFieldTypeDescriptor(field_id);
302 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800303 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700304 DCHECK_LT(field_index, 2U);
305 // 0 == Class[] interfaces; 1 == Class[][] throws;
306 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800307 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800311 return Primitive::GetType(GetTypeDescriptor()[0]);
312 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 Primitive::Type type = GetTypeAsPrimitiveType();
315 return type != Primitive::kPrimNot;
316 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800318 Primitive::Type type = GetTypeAsPrimitiveType();
319 return Primitive::FieldSize(type);
320 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800321
322 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
323 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700326 uint32_t field_index = field_->GetDexFieldIndex();
327 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800328 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700329 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
330 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersc2b44472011-12-14 21:17:17 -0800331 } else {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700332 DCHECK(field_->IsStatic());
333 DCHECK_LT(field_index, 2U);
334 // 0 == Class[] interfaces; 1 == Class[][] throws;
Ian Rogersc2b44472011-12-14 21:17:17 -0800335 ClassHelper kh(field_->GetDeclaringClass());
336 declaring_class_descriptor_ = kh.GetDescriptor();
337 return declaring_class_descriptor_.c_str();
338 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800339 }
340
341 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
343 mirror::DexCache* result = dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800344 if (result == NULL) {
345 result = field_->GetDeclaringClass()->GetDexCache();
346 dex_cache_ = result;
347 }
348 return result;
349 }
350 ClassLinker* GetClassLinker() {
351 ClassLinker* result = class_linker_;
352 if (result == NULL) {
353 result = Runtime::Current()->GetClassLinker();
354 class_linker_ = result;
355 }
356 return result;
357 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700358 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700359 if (dex_file_ == NULL) {
360 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800361 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700362 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 }
364
365 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800367 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 const mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800369 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800370
371 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
372};
373
374class MethodHelper {
375 public:
Ian Rogersca190662012-06-26 15:45:57 -0700376 MethodHelper()
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
378 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700379
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 explicit MethodHelper(const mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700382 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
383 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800384 SetMethod(m);
385 }
Ian Rogersca190662012-06-26 15:45:57 -0700386
Brian Carlstromea46f952013-07-30 01:26:50 -0700387 MethodHelper(const mirror::ArtMethod* m, ClassLinker* l)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700389 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
390 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800391 SetMethod(m);
392 }
393
Brian Carlstromea46f952013-07-30 01:26:50 -0700394 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395 DCHECK(new_m != NULL);
396 if (dex_cache_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 mirror::Class* klass = new_m->GetDeclaringClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800398 if (klass->IsProxyClass()) {
399 dex_cache_ = NULL;
400 dex_file_ = NULL;
401 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 mirror::DexCache* new_m_dex_cache = klass->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 if (new_m_dex_cache != dex_cache_) {
404 dex_cache_ = new_m_dex_cache;
405 dex_file_ = NULL;
406 }
407 }
408 }
409 SetMethod(new_m);
410 shorty_ = NULL;
411 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700412
Brian Carlstromea46f952013-07-30 01:26:50 -0700413 const mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700414 return method_;
415 }
416
Ian Rogersb726dcb2012-09-05 08:57:23 -0700417 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800419 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700420 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800421 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
422 } else {
423 Runtime* runtime = Runtime::Current();
424 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700425 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800426 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700427 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800428 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700429 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800430 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700431 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800432 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700433 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800434 }
435 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800436 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800439 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800440 uint32_t dex_method_idx = method_->GetDexMethodIndex();
441 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
443 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444
Jeff Hao9a916d32013-06-27 18:45:37 -0700445 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 const char* result = shorty_;
447 if (result == NULL) {
448 const DexFile& dex_file = GetDexFile();
449 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
450 &shorty_len_);
451 shorty_ = result;
452 }
453 return result;
454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455
Ian Rogersb726dcb2012-09-05 08:57:23 -0700456 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800457 if (shorty_ == NULL) {
458 GetShorty();
459 }
460 return shorty_len_;
461 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700462
Ian Rogersb726dcb2012-09-05 08:57:23 -0700463 const std::string GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800464 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800465 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700466 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800467 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
468 } else {
469 return "<no signature>";
470 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700472
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700473 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 const DexFile& dex_file = GetDexFile();
475 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700478 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800479 const DexFile::ProtoId& proto = GetPrototype();
480 return GetDexFile().GetProtoParameters(proto);
481 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 mirror::Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800484 const DexFile& dex_file = GetDexFile();
485 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
486 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
487 uint16_t return_type_idx = proto_id.return_type_idx_;
488 return GetClassFromTypeIdx(return_type_idx);
489 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700490
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700491 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 const DexFile& dex_file = GetDexFile();
493 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
494 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
495 uint16_t return_type_idx = proto_id.return_type_idx_;
496 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
497 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700499 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700500 if (dex_pc == DexFile::kDexNoIndex) {
501 return method_->IsNative() ? -2 : -1;
502 } else {
503 const DexFile& dex_file = GetDexFile();
504 return dex_file.GetLineNumFromPC(method_, dex_pc);
505 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800506 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700508 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700510 uint32_t dex_method_idx = method_->GetDexMethodIndex();
511 if (dex_method_idx != DexFile::kDexNoIndex) {
512 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
513 } else {
514 return "<runtime method>";
515 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800516 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700517
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700518 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
519 return ClassHelper(method_->GetDeclaringClass()).GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700521
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700522 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
523 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700524 }
525
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700526 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
527 return GetDexFile().GetClassDef(GetClassDefIndex());
528 }
529
530 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700531 return method_->GetDeclaringClass()->GetClassLoader();
532 }
533
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800534 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 return method_->IsStatic();
536 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537
Ian Rogersb726dcb2012-09-05 08:57:23 -0700538 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800539 return IsStatic() && StringPiece(GetName()) == "<clinit>";
540 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700541
Ian Rogersb726dcb2012-09-05 08:57:23 -0700542 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800543 // "1 +" because the first in Args is the receiver.
544 // "- 1" because we don't count the return type.
545 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
546 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700547
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800548 // Get the primitive type associated with the given parameter.
549 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 CHECK_LT(param, NumArgs());
551 if (IsStatic()) {
552 param++; // 0th argument must skip return value at start of the shorty
553 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800554 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800556 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800557 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800559 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
560 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
561 Primitive::Type type = GetParamPrimitiveType(param);
562 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
563 }
564
565 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700566 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800567 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800568 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700569
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700571 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800572 if (GetDexCache() == other->GetDexCache()) {
573 const DexFile& dex_file = GetDexFile();
574 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
575 const DexFile::MethodId& other_mid =
576 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800577 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800578 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800579 StringPiece name(GetName());
580 StringPiece other_name(other->GetName());
581 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800582 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700583
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700584 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700585 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800586 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
587 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700588
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700589 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800591 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
592 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700593
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700595 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800597 if (type == NULL) {
598 type = GetClassLinker()->ResolveType(type_idx, method_);
599 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
600 }
601 return type;
602 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700603
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700605 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800606 const DexFile& dex_file = GetDexFile();
607 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
608 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700609
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700612 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614
Ian Rogersb726dcb2012-09-05 08:57:23 -0700615 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616 const DexFile* result = dex_file_;
617 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 const mirror::DexCache* dex_cache = GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700619 result = dex_file_ = dex_cache->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 }
621 return *result;
622 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700623
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
625 mirror::DexCache* result = dex_cache_;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626 if (result == NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 mirror::Class* klass = method_->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700628 result = klass->GetDexCache();
629 dex_cache_ = result;
630 }
631 return result;
632 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700633
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
635 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700636 if (UNLIKELY(s == NULL)) {
637 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, GetDexCache());
638 }
639 return s;
640 }
641
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800642 private:
643 // Set the method_ field, for proxy methods looking up the interface method via the resolved
644 // methods table.
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 void SetMethod(const mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800646 if (method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700648 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800650 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700651 DCHECK(interface_method != NULL);
652 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800653 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800654 }
655 }
656 method_ = method;
657 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700658
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800659 ClassLinker* GetClassLinker() {
660 ClassLinker* result = class_linker_;
661 if (result == NULL) {
662 result = Runtime::Current()->GetClassLinker();
663 class_linker_ = result;
664 }
665 return result;
666 }
667
668 ClassLinker* class_linker_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 mirror::DexCache* dex_cache_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800670 const DexFile* dex_file_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700671 const mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800672 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800673 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800674
675 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
676};
677
678} // namespace art
679
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700680#endif // ART_RUNTIME_OBJECT_UTILS_H_