blob: c6e71c3ae1d05ec9a073774a8681ac00a0b6545c [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
17#ifndef ART_SRC_OBJECT_UTILS_H_
18#define ART_SRC_OBJECT_UTILS_H_
19
20#include "class_linker.h"
21#include "dex_cache.h"
22#include "dex_file.h"
Ian Rogersc2b44472011-12-14 21:17:17 -080023#include "intern_table.h"
Ian Rogers672f5202012-01-12 18:06:40 -080024#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080025#include "object.h"
26#include "runtime.h"
Ian Rogers50b35e22012-10-04 10:09:15 -070027#include "sirt_ref.h"
Elliott Hughes91250e02011-12-13 22:30:35 -080028#include "UniquePtr.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080029
30#include <string>
31
32namespace art {
33
Ian Rogers672f5202012-01-12 18:06:40 -080034class ObjectLock {
35 public:
Ian Rogers1f539342012-10-03 21:09:42 -070036 explicit ObjectLock(Thread* self, Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
37 : self_(self), obj_(object) {
Ian Rogers672f5202012-01-12 18:06:40 -080038 CHECK(object != NULL);
39 obj_->MonitorEnter(self_);
40 }
41
Ian Rogersb726dcb2012-09-05 08:57:23 -070042 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080043 obj_->MonitorExit(self_);
44 }
45
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 void Wait() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080047 return Monitor::Wait(self_, obj_, 0, 0, false);
48 }
49
Ian Rogersb726dcb2012-09-05 08:57:23 -070050 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080051 obj_->Notify();
52 }
53
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers672f5202012-01-12 18:06:40 -080055 obj_->NotifyAll();
56 }
57
58 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 Thread* const self_;
Ian Rogers672f5202012-01-12 18:06:40 -080060 Object* obj_;
61 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
62};
63
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080064class ClassHelper {
65 public:
Elliott Hughes91250e02011-12-13 22:30:35 -080066 ClassHelper(const Class* c = NULL, ClassLinker* l = NULL)
Ian Rogersb726dcb2012-09-05 08:57:23 -070067 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Elliott Hughes91250e02011-12-13 22:30:35 -080068 : class_def_(NULL),
69 class_linker_(l),
70 dex_cache_(NULL),
71 dex_file_(NULL),
72 interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070073 klass_(NULL) {
74 if (c != NULL) {
75 ChangeClass(c);
76 }
Elliott Hughes91250e02011-12-13 22:30:35 -080077 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080078
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 void ChangeClass(const Class* new_c)
Ian Rogersb726dcb2012-09-05 08:57:23 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070081 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
82 CHECK(new_c->IsClass()) << "new_c=" << new_c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080083 if (dex_cache_ != NULL) {
84 DexCache* new_c_dex_cache = new_c->GetDexCache();
85 if (new_c_dex_cache != dex_cache_) {
86 dex_cache_ = new_c_dex_cache;
87 dex_file_ = NULL;
88 }
89 }
90 klass_ = new_c;
91 interface_type_list_ = NULL;
92 class_def_ = NULL;
93 }
94
Elliott Hughes91250e02011-12-13 22:30:35 -080095 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
96 // If you need it longer, copy it into a std::string.
Ian Rogersb726dcb2012-09-05 08:57:23 -070097 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070098 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080099 if (UNLIKELY(klass_->IsArrayClass())) {
100 return GetArrayDescriptor();
101 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800102 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800103 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800104 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
105 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 } else {
107 const DexFile& dex_file = GetDexFile();
108 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
109 return dex_file.GetTypeDescriptor(type_id);
110 }
111 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800112
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 const char* GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800114 std::string result("[");
115 const Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700116 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800117 ChangeClass(klass_->GetComponentType());
118 result += GetDescriptor();
119 ChangeClass(saved_klass);
120 descriptor_ = result;
121 return descriptor_.c_str();
122 }
123
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 const DexFile::ClassDef* GetClassDef()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 const DexFile::ClassDef* result = class_def_;
127 if (result == NULL) {
128 result = GetDexFile().FindClassDef(GetDescriptor());
129 class_def_ = result;
130 }
131 return result;
132 }
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 Rogers00f7d0e2012-07-19 15:28:27 -0700160 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 Rogers6d4d9fc2011-11-30 16:24:48 -0800175 Class* interface = GetDexCache()->GetResolvedType(type_idx);
176 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();
187 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
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 Rogersad0b3a32012-04-16 14:50:24 -0700193 DexCache* dex_cache = GetDexCache();
194 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 Rogersb726dcb2012-09-05 08:57:23 -0700209 DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700210 DexCache* result = dex_cache_;
211 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
242 const DexFile::ClassDef* class_def_;
243 ClassLinker* class_linker_;
244 DexCache* dex_cache_;
245 const DexFile* dex_file_;
246 const DexFile::TypeList* interface_type_list_;
247 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800248 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249
250 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
251};
252
253class FieldHelper {
254 public:
255 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800256 explicit FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogersca190662012-06-26 15:45:57 -0700257 FieldHelper(const Field* f, ClassLinker* l)
258 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259
260 void ChangeField(const Field* new_f) {
261 DCHECK(new_f != NULL);
262 if (dex_cache_ != NULL) {
263 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
264 if (new_f_dex_cache != dex_cache_) {
265 dex_cache_ = new_f_dex_cache;
266 dex_file_ = NULL;
267 }
268 }
269 field_ = new_f;
270 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700271 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800272 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700273 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800274 const DexFile& dex_file = GetDexFile();
275 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
276 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800277 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700278 DCHECK_LT(field_index, 2U);
279 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800280 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 }
282 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800283 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700284 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800285 const DexFile& dex_file = GetDexFile();
286 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
287 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
288 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700289 return Runtime::Current()->GetInternTable()->InternStrong(GetName());
Ian Rogersc2b44472011-12-14 21:17:17 -0800290 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 Class* GetType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800293 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700294 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800295 const DexFile& dex_file = GetDexFile();
296 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
297 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
298 if (type == NULL) {
299 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
300 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
301 }
302 return type;
303 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700304 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800308 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700309 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800310 const DexFile& dex_file = GetDexFile();
311 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
312 return dex_file.GetFieldTypeDescriptor(field_id);
313 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800314 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700315 DCHECK_LT(field_index, 2U);
316 // 0 == Class[] interfaces; 1 == Class[][] throws;
317 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800318 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800322 return Primitive::GetType(GetTypeDescriptor()[0]);
323 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800325 Primitive::Type type = GetTypeAsPrimitiveType();
326 return type != Primitive::kPrimNot;
327 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 Primitive::Type type = GetTypeAsPrimitiveType();
330 return Primitive::FieldSize(type);
331 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800332
333 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
334 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800337 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800338 if (type_idx != DexFile::kDexNoIndex16) {
339 const DexFile& dex_file = GetDexFile();
340 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
341 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700342 // Most likely a proxy class.
Ian Rogersc2b44472011-12-14 21:17:17 -0800343 ClassHelper kh(field_->GetDeclaringClass());
344 declaring_class_descriptor_ = kh.GetDescriptor();
345 return declaring_class_descriptor_.c_str();
346 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800347 }
348
349 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700350 DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800351 DexCache* result = dex_cache_;
352 if (result == NULL) {
353 result = field_->GetDeclaringClass()->GetDexCache();
354 dex_cache_ = result;
355 }
356 return result;
357 }
358 ClassLinker* GetClassLinker() {
359 ClassLinker* result = class_linker_;
360 if (result == NULL) {
361 result = Runtime::Current()->GetClassLinker();
362 class_linker_ = result;
363 }
364 return result;
365 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700366 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700367 if (dex_file_ == NULL) {
368 dex_file_ = GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 }
Mathieu Chartier66f19252012-09-18 08:57:04 -0700370 return *dex_file_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 }
372
373 ClassLinker* class_linker_;
374 DexCache* dex_cache_;
375 const DexFile* dex_file_;
376 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800377 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800378
379 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
380};
381
382class MethodHelper {
383 public:
Ian Rogersca190662012-06-26 15:45:57 -0700384 MethodHelper()
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
386 shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700387
Mathieu Chartier66f19252012-09-18 08:57:04 -0700388 explicit MethodHelper(const AbstractMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700390 : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
391 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800392 SetMethod(m);
393 }
Ian Rogersca190662012-06-26 15:45:57 -0700394
Mathieu Chartier66f19252012-09-18 08:57:04 -0700395 MethodHelper(const AbstractMethod* m, ClassLinker* l)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700396 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700397 : class_linker_(l), dex_cache_(NULL), dex_file_(NULL), method_(NULL), shorty_(NULL),
398 shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800399 SetMethod(m);
400 }
401
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402 void ChangeMethod(AbstractMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 DCHECK(new_m != NULL);
404 if (dex_cache_ != NULL) {
405 Class* klass = new_m->GetDeclaringClass();
406 if (klass->IsProxyClass()) {
407 dex_cache_ = NULL;
408 dex_file_ = NULL;
409 } else {
410 DexCache* new_m_dex_cache = klass->GetDexCache();
411 if (new_m_dex_cache != dex_cache_) {
412 dex_cache_ = new_m_dex_cache;
413 dex_file_ = NULL;
414 }
415 }
416 }
417 SetMethod(new_m);
418 shorty_ = NULL;
419 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420
Ian Rogersb726dcb2012-09-05 08:57:23 -0700421 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800422 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800423 uint32_t dex_method_idx = method_->GetDexMethodIndex();
424 if (dex_method_idx != DexFile::kDexNoIndex16) {
425 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
426 } else {
427 Runtime* runtime = Runtime::Current();
428 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700429 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800430 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700431 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800432 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700433 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800434 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700435 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800436 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700437 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800438 }
439 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700441
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800444 uint32_t dex_method_idx = method_->GetDexMethodIndex();
445 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
447 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800451 const char* result = shorty_;
452 if (result == NULL) {
453 const DexFile& dex_file = GetDexFile();
454 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
455 &shorty_len_);
456 shorty_ = result;
457 }
458 return result;
459 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800462 if (shorty_ == NULL) {
463 GetShorty();
464 }
465 return shorty_len_;
466 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 const std::string GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800470 uint32_t dex_method_idx = method_->GetDexMethodIndex();
471 if (dex_method_idx != DexFile::kDexNoIndex16) {
472 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
473 } else {
474 return "<no signature>";
475 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 const DexFile::ProtoId& GetPrototype()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800480 const DexFile& dex_file = GetDexFile();
481 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484 const DexFile::TypeList* GetParameterTypeList()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800486 const DexFile::ProtoId& proto = GetPrototype();
487 return GetDexFile().GetProtoParameters(proto);
488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489
Ian Rogers50b35e22012-10-04 10:09:15 -0700490 ObjectArray<Class>* GetParameterTypes(Thread* self)
491 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 const DexFile::TypeList* params = GetParameterTypeList();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 uint32_t num_params = params == NULL ? 0 : params->Size();
Ian Rogers50b35e22012-10-04 10:09:15 -0700494 SirtRef<ObjectArray<Class> > result(self, GetClassLinker()->AllocClassArray(self, num_params));
495 if (UNLIKELY(result.get() == NULL)) {
496 CHECK(self->IsExceptionPending());
497 return NULL;
498 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800499 for (uint32_t i = 0; i < num_params; i++) {
500 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
jeffhao441d9122012-03-21 17:29:10 -0700501 if (param_type == NULL) {
502 DCHECK(Thread::Current()->IsExceptionPending());
503 return NULL;
504 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800505 result->Set(i, param_type);
506 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700507 return result.get();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700509
Ian Rogersb726dcb2012-09-05 08:57:23 -0700510 Class* GetReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800511 const DexFile& dex_file = GetDexFile();
512 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
513 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
514 uint16_t return_type_idx = proto_id.return_type_idx_;
515 return GetClassFromTypeIdx(return_type_idx);
516 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700517
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 const char* GetReturnTypeDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 const DexFile& dex_file = GetDexFile();
521 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
522 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
523 uint16_t return_type_idx = proto_id.return_type_idx_;
524 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
525 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700526
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700527 int32_t GetLineNumFromDexPC(uint32_t dex_pc)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700529 if (dex_pc == DexFile::kDexNoIndex) {
530 return method_->IsNative() ? -2 : -1;
531 } else {
532 const DexFile& dex_file = GetDexFile();
533 return dex_file.GetLineNumFromPC(method_, dex_pc);
534 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700536
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800539 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800540 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 uint16_t type_idx = klass->GetDexTypeIndex();
542 const DexFile& dex_file = GetDexFile();
543 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
544 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700546 const char* GetDeclaringClassSourceFile()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800548 const char* descriptor = GetDeclaringClassDescriptor();
549 const DexFile& dex_file = GetDexFile();
550 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800551 CHECK(dex_class_def != NULL);
552 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800553 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700554
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555 uint32_t GetClassDefIndex()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700556 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557 const char* descriptor = GetDeclaringClassDescriptor();
558 const DexFile& dex_file = GetDexFile();
559 uint32_t index;
560 CHECK(dex_file.FindClassDefIndex(descriptor, index));
561 return index;
562 }
563
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700564 ClassLoader* GetClassLoader()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700565 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566 return method_->GetDeclaringClass()->GetClassLoader();
567 }
568
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 bool IsStatic()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800571 return method_->IsStatic();
572 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573
Ian Rogersb726dcb2012-09-05 08:57:23 -0700574 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800575 return IsStatic() && StringPiece(GetName()) == "<clinit>";
576 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577
Ian Rogersb726dcb2012-09-05 08:57:23 -0700578 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800579 // "1 +" because the first in Args is the receiver.
580 // "- 1" because we don't count the return type.
581 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
582 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700583
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800584 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 bool IsParamALongOrDouble(size_t param)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700586 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800587 CHECK_LT(param, NumArgs());
588 if (IsStatic()) {
589 param++; // 0th argument must skip return value at start of the shorty
590 } else if (param == 0) {
591 return false; // this argument
592 }
593 char ch = GetShorty()[param];
594 return (ch == 'J' || ch == 'D');
595 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800597 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
Ian Rogersb726dcb2012-09-05 08:57:23 -0700598 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800599 CHECK_LT(param, NumArgs());
600 if (IsStatic()) {
601 param++; // 0th argument must skip return value at start of the shorty
602 } else if (param == 0) {
603 return true; // this argument
604 }
605 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
606 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700607
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700608 bool HasSameNameAndSignature(MethodHelper* other)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700609 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800610 if (GetDexCache() == other->GetDexCache()) {
611 const DexFile& dex_file = GetDexFile();
612 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
613 const DexFile::MethodId& other_mid =
614 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800615 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800617 StringPiece name(GetName());
618 StringPiece other_name(other->GetName());
619 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800620 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700621
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700622 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700623 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800624 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800629 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
630 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700631
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 Class* GetClassFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700633 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800634 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
635 if (type == NULL) {
636 type = GetClassLinker()->ResolveType(type_idx, method_);
637 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
638 }
639 return type;
640 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700641
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700643 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800644 const DexFile& dex_file = GetDexFile();
645 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
646 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700649 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800650 return GetDexCache()->GetResolvedType(type_idx);
651 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700652
Ian Rogersb726dcb2012-09-05 08:57:23 -0700653 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800654 const DexFile* result = dex_file_;
655 if (result == NULL) {
656 const DexCache* dex_cache = GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700657 result = dex_file_ = dex_cache->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800658 }
659 return *result;
660 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700661
Ian Rogersb726dcb2012-09-05 08:57:23 -0700662 DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 DexCache* result = dex_cache_;
664 if (result == NULL) {
665 Class* klass = method_->GetDeclaringClass();
666 result = klass->GetDexCache();
667 dex_cache_ = result;
668 }
669 return result;
670 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700671
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800672 private:
673 // Set the method_ field, for proxy methods looking up the interface method via the resolved
674 // methods table.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700675 void SetMethod(const AbstractMethod* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800677 if (method != NULL) {
678 Class* klass = method->GetDeclaringClass();
679 if (klass->IsProxyClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700680 AbstractMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800681 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
682 CHECK(interface_method != NULL);
683 CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
684 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800685 }
686 }
687 method_ = method;
688 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700689
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800690 ClassLinker* GetClassLinker() {
691 ClassLinker* result = class_linker_;
692 if (result == NULL) {
693 result = Runtime::Current()->GetClassLinker();
694 class_linker_ = result;
695 }
696 return result;
697 }
698
699 ClassLinker* class_linker_;
700 DexCache* dex_cache_;
701 const DexFile* dex_file_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700702 const AbstractMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800703 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800704 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800705
706 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
707};
708
709} // namespace art
710
711#endif // ART_SRC_OBJECT_UTILS_H_